gpt4 book ai didi

maven - 通过 Maven 从测试类运行 main 方法

转载 作者:行者123 更新时间:2023-12-03 00:23:08 36 4
gpt4 key购买 nike

我的 pom.xml 中有以下内容:

<build>
...
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.myorg.MyClass</mainClass>
</configuration>
</plugin>
</plugins>
</build>

类(class)com.myorg.MyClass位于我的测试源目录中,我可以使用以下命令运行它:

mvn -e exec:java -Dexec.classpathScope="test"

我想要:

  1. 避免使用 -Dexec.classpathScope="test" ,但我无法弄清楚如何配置该插件以在测试类路径中查找。
  2. 为其他类编写更多插件(每个类都有不同的配置),但现在我只能运行 exec:java 。有没有办法标记这个插件,以便我通过该标签调用它,而不是仅仅说“运行 exec:java 中的任何内容”?
  3. 拉入 -javaagent属性(property)。我在 pom.xml 中定义了这个属性,测试用例正在使用它。我的“自定义”插件会获取这些属性还是我需要采取任何措施来引入它们?

这里是属性,直接在 <project> 下定义.

<properties>
<spring.version>3.2.6.RELEASE</spring.version>
<atomikos.version>3.9.2</atomikos.version>
<loadTimeWeaverArgLine>-javaagent:"${settings.localRepository}/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar"</loadTimeWeaverArgLine>
</properties>

尝试配置文件

根据@Michal的建议(https://stackoverflow.com/a/30839824/257233),这是我尝试过的:

<profile>
<id>run-importer</id>
<properties>
<loadTimeWeaverArgLine>-javaagent:"${settings.localRepository}/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar"</loadTimeWeaverArgLine>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<executable>java</executable>
<!--
None of these three options work.
<commandlineArgs>-javaagent:C:/Users/robbram/.m2/repository/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar</commandlineArgs>
<commandlineArgs>${loadTimeWeaverArgLine}</commandlineArgs>
<commandlineArgs>-javaagent:"${settings.localRepository}/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar"</commandlineArgs>
<argLine>${loadTimeWeaverArgLine}</argLine>
-->
<classpathScope>test</classpathScope>
<mainClass>com.myorg.MyClass</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</profile>

我运行它:

mvn -e exec:java -Prun-importer 

使用任一选项时,我遇到以下异常:

[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project TOLTAT-Model: An exception occured while executing the Java class. null: InvocationTargetException: Error creating bean with name 'loadTimeWeaver' defined in class org.springframework.context.annotation.LoadTimeWeavingConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.instrument.classloading.LoadTimeWeaver org.springframework.context.annotation.LoadTimeWeavingConfiguration.loadTimeWeaver()] threw exception; nested exception is java.lang.IllegalStateException: ClassLoader [java.net.URLClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project TOLTAT-Model: An exception occured while executing the Java class. null
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:216)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)

我可以确认主类com.myorg.MyClass正在被执行。我可以看到它的其他输出。我还可以确认loadTimeWeaverArgLine适用于该 POM 的其他部分。它已成功用于集成测试:

<profile>
<id>integration-tests</id>
<build>
<plugins>
<!-- Integration tests require additional loadtime Spring argument -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<forkMode>once</forkMode>
<argLine> ${loadTimeWeaverArgLine}</argLine>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>

使用配置文件和 mvn exec:exec 的尝试 2

更新(2015 年 6 月 17 日星期三,下午 12:11:17):关注 Michal's latest comment我现在可以按我想要的方式工作了:

<profile>
<id>run-importer</id>
<properties>
<loadTimeWeaverArg>-javaagent:"${settings.localRepository}/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar"</loadTimeWeaverArg>
<log4JConfigArg>-Dlog4j.configuration=file:${project.build.directory}/path/to/log4j.properties</log4JConfigArg>
<mainClassArg>com.myorg.MyClass</mainClassArg>
<arg1>foo</arg1>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<classpathScope>test</classpathScope>
<arguments>
<argument>${log4JConfigArg}</argument>
<argument>${loadTimeWeaverArg}</argument>
<argument>-classpath</argument>
<classpath />
<argument>${mainClassArg}</argument>
<argument>${arg1}</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</profile>

我运行它:

mvn -e exec:exec -Prun-importer

这种方法的优点:

  • 此配置文件的全部目的是运行永远不应该部署但需要使用 src 中的代码并测试 src 的“特殊代码”。
    • 我注意到 Michal 的建议,即这本身应该成为一个模块。如果这个代码库还没有那么完善(大、旧、复杂),我会认真考虑这一点。
  • 它留出了空间,以防需要重复,因此对于 mvn -e exec:exec 运行的内容不存在“竞争” .
  • 我可以使用 pom 中已存在的变量来指定 java 代理、log4j 和许多其他配置。
  • 我可以使用 -Darg1="bar" 覆盖命令行上的任何参数。

最佳答案

我不确定您实际上要通过此完成什么,因为它对 Maven 的使用相当生硬。然而,所有这些东西在技术上都是可行的:

1/

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.myorg.MyClass</mainClass>
<classpathScope>test</classpathScope>
</configuration>
</plugin>

2/

您可以使用配置文件,并将不同的插件配置放在不同的配置文件中。然后你调用:

mvn -e exec:java -Pmy-first-profile

不过我觉得这真的很奇怪。我的建议是重新考虑您的案例,并可能选择另一种方法或工具。

3/

经过作者编辑后问题本身的最终解决方案。

关于maven - 通过 Maven 从测试类运行 main 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30839272/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com