真的不知道该怎么把它作为一个问题的标题......
我有3个maven模块。第一个是父模块,它只包装子模块。没有什么花哨。在第二个模块中,我有一个抽象的测试类,有两种方法。
在第三个模块中,我有一个从第二个模块继承抽象类的测试类。
当我尝试用maven构建它时,我收到编译错误,说它找不到符号,这是第二个模块的抽象类。有趣的是,我在eclipse中没有得到任何编译错误。
这是第三个模块的pom:
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>SecondModule</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<!-- to generate the MANIFEST-FILE of the bundle -->
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Import-Package>*</Import-Package>
<Export-Package></Export-Package>
<Embed-Dependency>SecondModule</Embed-Dependency>
</instructions>
</configuration>
</plugin>
</plugins>
这是我得到的错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project ThirdModule: Compilation failure: Compilation failure:
[ERROR] D:/workspace/project/ThirdModule/src/test/java/org/rrrrrrr/ssssss/thirdmodule/ConcreteTest.java:[7,56] cannot find symbol
[ERROR] symbol: class AbstractTest
[ERROR] location: package org.rrrrrrr.ssssss.secondmodule
我错过了什么?
答案 0 :(得分:5)
添加依赖项时,测试类(src / test中的类)不会自动添加到类路径中。只包括src / main中的类。
要添加对测试类的依赖性,您还需要通过在依赖项部分中将type指定为test-jar来明确指定它。这应该是模块3的pom.xml中定义的依赖项。
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>SecondModule</artifactId>
<version>${project.version}</version>
<type>test-jar</type> <!-- add dependency to test jar -->
</dependency>
确保测试jar由SecondModule生成也是一个好主意。否则,任何需要编译ThirdModule的人都需要编译SecondModule。默认情况下,maven不会将测试类打包到jar中。要告诉maven这样做,添加目标:jar和test-jar到maven-jar-plugin执行。这样就可以生成原始jar和测试罐。
这是第二个模块的概要pom.xml,用于说明这一点。
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>jar</goal>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>