java - Maven编译错误: cannot find symbol

标签 java maven-3 maven-compiler-plugin

真的不知道用什么不同的方式来作为问题的标题...

我有 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

我错过了什么?

最佳答案

添加依赖项时,测试类(src/test 中的类)不会自动添加到类路径中。仅包含 src/main 中的类。

要同时添加对测试类的依赖,您需要通过在依赖部分将类型指定为 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 不会将测试类打包到 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>

关于java - Maven编译错误: cannot find symbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31987641/

相关文章:

Java:具有输出 HTML 但没有 HTML 实体的 XSLT

Tomcat 10 的 Maven 插件

Maven Pom.xml问题

spring-boot - 在运行 Karate 测试之前运行位于另一个项目上的 Spring Boot 应用程序?

eclipse - 最佳启动 JSF 2.2 原型(prototype)

java - Maven,如何与编译一起运行测试编译

java - 如何 'getConstructor',其中构造函数签名包含java数组

java - 在Java中递归调用方法时如何修改final变量

java - 将时间 int 转换为分钟 秒 小时

maven-3 - 使用原型(prototype) :generate 在 Maven 命令行中过滤原型(prototype)