Java 在多模块maven项目中运行exec maven插件时出现问题
我有一个多模块Maven项目,有一个主pom和许多子目录,每个子目录都包含引用主pom的项目(和pom.xml文件)Java 在多模块maven项目中运行exec maven插件时出现问题,java,maven,exec-maven-plugin,Java,Maven,Exec Maven Plugin,我有一个多模块Maven项目,有一个主pom和许多子目录,每个子目录都包含引用主pom的项目(和pom.xml文件) <execution> <id>execute-test</id> <phase>pre-integration-test</phase> <goals>
<execution>
<id>execute-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.mycompany.Testclass</mainClass>
<classpathScope>test</classpathScope>
</configuration>
</execution>
</executions>
</plugin>
在projectA中包含对exec maven插件的调用,该插件执行java目标并成功调用驻留在projectA中某处的测试类(projectA/test/com/mycompany/Testclass.java)。插件声明如下:
org.codehaus.mojo
execmaven插件
1.2.1
<execution>
<id>execute-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.mycompany.Testclass</mainClass>
<classpathScope>test</classpathScope>
</configuration>
</execution>
</executions>
</plugin>
完成此操作后,我成功加载了该类,但在com.google.gson.JsonSyntaxException上获得了ClassNotFoundException
<execution>
<id>execute-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.mycompany.Testclass</mainClass>
<classpathScope>test</classpathScope>
</configuration>
</execution>
</executions>
</plugin>
这与我从命令行运行该类时遇到的错误相同,如下所示
<execution>
<id>execute-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.mycompany.Testclass</mainClass>
<classpathScope>test</classpathScope>
</configuration>
</execution>
</executions>
</plugin>
java-classpath“projectA/target/projectA-6.6.1-bSOURCE-tests.jar;projectA/target/projectA-6.6.1-bSOURCE.jar”com.mycompany.Testclass
<execution>
<id>execute-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.mycompany.Testclass</mainClass>
<classpathScope>test</classpathScope>
</configuration>
</execution>
</executions>
</plugin>
在projectB中,我想我想使用exec maven插件的java目标,而不是exec目标,但无论如何,我必须能够指定类路径
<execution>
<id>execute-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.mycompany.Testclass</mainClass>
<classpathScope>test</classpathScope>
</configuration>
</execution>
</executions>
</plugin>
你知道我该怎么做吗
<execution>
<id>execute-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.mycompany.Testclass</mainClass>
<classpathScope>test</classpathScope>
</configuration>
</execution>
</executions>
</plugin>
谢谢。这是我自己想出来的,我正在发布我的答案,以防其他人需要它,或者有人想质疑它(或者提出更好的建议)
<execution>
<id>execute-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.mycompany.Testclass</mainClass>
<classpathScope>test</classpathScope>
</configuration>
</execution>
</executions>
</plugin>
我所做的是在下属项目projectB中添加一个依赖项,如下所示:
<execution>
<id>execute-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.mycompany.Testclass</mainClass>
<classpathScope>test</classpathScope>
</configuration>
</execution>
</executions>
</plugin>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>projectA</artifactId>
<version>6.6-SNAPSHOT</version>
<scope>test</scope>
<classifier>tests</classifier>
</dependency>
com.mycompany
项目A
6.6-1
测试
测验
我相信关键是元素,它指示maven从包含projectA中测试类的jar文件中获取依赖类
<execution>
<id>execute-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.mycompany.Testclass</mainClass>
<classpathScope>test</classpathScope>
</configuration>
</execution>
</executions>
</plugin>