通过maven从测试类运行main方法
我的pom.xml中有以下内容:通过maven从测试类运行main方法,maven,maven-plugin,Maven,Maven Plugin,我的pom.xml中有以下内容: <build> ... <plugins> ... <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</versio
<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>
我想:
-Dexec.classpathScope=“test”
,但我无法确定如何配置插件以在测试类路径中查看exec:java
。有没有办法给这个插件贴上标签,这样我就可以通过这个标签来调用它,而不仅仅是说“运行exec:java中的任何东西”-javaagent
属性。我在pom.xml中定义了这个属性,测试用例正在使用它。我的“自定义”插件会获取这些属性吗?还是我需要做些什么才能将它们拉进来
下定义的属性
我得到以下任何一个选项的例外:
[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>
这种方法的优点是:
- 这个概要文件的全部目的是运行“特殊代码”,这些代码不应该被部署,但需要使用src和testsrc中的代码。
- 我注意到Michal的建议,这本身就是一个模块。如果这个代码库还没有很好地建立(大的,旧的,复杂的),我会认真考虑这个。
- 它留出了空间,以防需要复制,因此对于使用
mvn-e exec:exec运行的内容没有“竞争”
- 我可以使用pom中已经存在的变量指定java代理、log4j和许多其他配置
- 我可以使用
-Darg1=“bar”
在作者编辑之后,问题本身的最终解决方案。谢谢Michal,我会在早上第一件事尝试。不过,我想解释一下我使用Maven的理由。这是一个预先存在的项目,在Maven中已经有很多配置。我的代码需要很多这样的配置,加上它使用src目录中的类,但我的代码也是一种特殊情况,永远不应该部署(因此它在test目录中)。我不想使用不同的工具,因为那样我就必须弄清楚如何复制已经存在的所有配置。对于这样的情况,测试代码库非常复杂,并且可能独立使用(无论出于何种原因),我真的建议使用单独的Maven模块(例如,
我卓越的项目测试支持
),其中所有测试内容(junit
,mockito
,支持类,构建器等)被放入src/main/java
树中。这样的模块可以在其他(业务)模块中用作测试范围的依赖项。我已经更新了我的问题,以显示到目前为止我所做的尝试。有关更新的说明,请致电mvn-e exec:exec-Prun importer
,而不是mvn exec:java…
非常感谢您-根据您的建议,我终于找到了解决我遇到的所有问题的方法。:)
<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)
<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>
<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
<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>
mvn -e exec:java -Pmy-first-profile