Maven compilation error: cannot find symbol

BojanSM picture BojanSM · Aug 13, 2015 · Viewed 16k times · Source

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.

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.

This is piece of pom of third module:

<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?

Answer

Joydip Datta picture Joydip Datta · Aug 13, 2015

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.

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.

<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.

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

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