【发布时间】:2023-04-01 23:56:02
【问题描述】:
我的 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。有没有办法给这个插件贴上标签,这样我就可以通过这个标签来调用它,而不仅仅是说“运行 exec:java 中的任何内容”? - 拉入
-javaagent属性。我在我的 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 maven-plugin