Maven compilation error: cannot find symbol
Maven compilation error: cannot find symbol
真的不知道该用什么不同的方式作为问题的标题...
我有 3 个 Maven 模块。第一个是 parent 模块,它只包装 child 模块。没有什么花哨。在第二个模块中,我有测试 class,它是抽象的,有两个方法。
在第三个模块中,我有测试 class,它继承了第二个模块的抽象 class。
当我尝试使用 maven 构建它时,出现编译错误,提示它无法找到一个符号,该符号是来自第二个模块的抽象 class。有趣的是,我在 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
我错过了什么?
添加依赖项时,测试 classes(classes inside src/test)不会自动添加到 class 路径。仅包括 src/main 中的 classes。
要添加对测试 classes 的依赖,您还需要通过在依赖部分将类型指定为 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>
确保 test-jar 由 SecondModule 生成也是一个好主意。否则任何需要编译 ThirdModule 的人也需要编译 SecondModule。默认情况下,maven 不会将 test classes 打包到 jar 中。要告诉 maven 这样做,请将目标:jar 和 test-jar 添加到 maven-jar-plugin 执行中。这样就会生成原始jar和测试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>
真的不知道该用什么不同的方式作为问题的标题...
我有 3 个 Maven 模块。第一个是 parent 模块,它只包装 child 模块。没有什么花哨。在第二个模块中,我有测试 class,它是抽象的,有两个方法。
在第三个模块中,我有测试 class,它继承了第二个模块的抽象 class。
当我尝试使用 maven 构建它时,出现编译错误,提示它无法找到一个符号,该符号是来自第二个模块的抽象 class。有趣的是,我在 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
我错过了什么?
添加依赖项时,测试 classes(classes inside src/test)不会自动添加到 class 路径。仅包括 src/main 中的 classes。
要添加对测试 classes 的依赖,您还需要通过在依赖部分将类型指定为 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>
确保 test-jar 由 SecondModule 生成也是一个好主意。否则任何需要编译 ThirdModule 的人也需要编译 SecondModule。默认情况下,maven 不会将 test classes 打包到 jar 中。要告诉 maven 这样做,请将目标:jar 和 test-jar 添加到 maven-jar-plugin 执行中。这样就会生成原始jar和测试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>