我的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>
类com.myorg.MyClass
位于我的测试源目录中,我可以使用以下命令运行它:
mvn -e exec:java -Dexec.classpathScope="test"
我想:
-Dexec.classpathScope="test"
,但我无法弄清楚如何配置该插件在测试类路径中查看。exec:java
。有没有办法标记这个插件,以便我通过该标签调用它,而不是只是说&#34;运行exec中的任何内容:java&#34;?-javaagent
属性。我在我的pom.xml中定义了这个属性,测试用例正在使用它。我的&#34;定制&#34;插件获取这些属性还是我需要做任何事情来吸引它们?这是属性,直接在<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>
Upodate(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
这种方法的优点:
mvn -e exec:exec
运行的内容。-Darg1="bar"
答案 0 :(得分:3)
我不确定你实际上要完成什么,因为它非常不习惯使用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 /
作者编辑后问题本身的最终解决方案。