java Maven 编译错误:找不到符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31987641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:28:01  来源:igfitidea点击:

Maven compilation error: cannot find symbol

javamaven-3maven-compiler-plugin

提问by BojanSM

Really didn't know what to put differently as a title of a question...

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

I have 3 maven modules. First one is parent module and it just wraps child modules. Nothing fancy. In the second module I have test class which is abstract and has two methods.

我有 3 个 Maven 模块。第一个是父模块,它只是包装子模块。没有什么花哨。在第二个模块中,我有测试类,它是抽象的并且有两个方法。

In third module I have test class which inherits abstract class from second module.

在第三个模块中,我有一个从第二个模块继承抽象类的测试类。

When I try to build this with maven I am getting compilation error which says that it can not find a symbol which, is that abstract class from second module. What's interesting that I don't get any compilation error in eclipse.

当我尝试用 maven 构建它时,我收到编译错误,说它找不到一个符号,它是来自第二个模块的抽象类。有趣的是,我在 Eclipse 中没有遇到任何编译错误。

This is piece of pom of third module:

这是第三个模块的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>

This is error that I am getting:

这是我收到的错误:

[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

What am I missing?

我错过了什么?

回答by Joydip Datta

The test classes (classes inside src/test) are not added to the class path automatically when you add a dependency. Only the classes those are in src/main are included.

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

To add dependency on test classes as well you need to specify it explicitly by specifying type as test-jar in the dependency section.This should be the dependency defined in the pom.xml of module 3.

要添加对测试类的依赖,您需要通过在依赖部分中将类型指定为 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>

It is also a good idea to make sure test-jar is generated by the SecondModule. Otherwise anybody who needs to compile ThirdModule would need to compile SecondModule as well. By default maven does not package test classes in to a jar. To tell maven to do this, add the goals: jar and test-jar to maven-jar-plugin executions. This way both the original jar and the test jar would be generated.

确保由 SecondModule 生成 test-jar 也是一个好主意。否则任何需要编译 ThirdModule 的人也需要编译 SecondModule。默认情况下,maven 不会将测试类打包到 jar 中。要告诉 maven 执行此操作,请将目标:jar 和 test-jar 添加到 maven-jar-plugin executions。这样,原始 jar 和测试 jar 都会生成。

Here is the outline pom.xml for second module that illustrates this.

这是说明这一点的第二个模块的大纲 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>