Search code examples
javamavenmaven-compiler-pluginmaven-module

My maven module cannot find class in my other maven module though I am setting it as dependency


I have two maven modules. One named tests and one ws-ejb.

In the tests pom.xml I have set the ws-ejb to be a dependency so I can use the EJB's in my tests.
This is a shortened version of the pom.xml of my tests maven module:

<parent>
   <artifactId>myproj</artifactId>
   <groupId>myproj</groupId>
   <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>tests</artifactId>

<dependencies>
  <dependency>
     <groupId>myproj</groupId>
     <artifactId>ws-ejb</artifactId>
     <version>1.0-SNAPSHOT</version>
     <scope>test</scope> 
     <type>war</type>
   <dependencies>
<dependency>
...
<!-- other dependencies in the file: junit and javax.ejb -->

<build>
  <plugins>
     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <executions>
           <execution>
              <id>tests</id>
              <phase>integration-test</phase>
              <goals>
                 <goal>test</goal>
              </goals>
           </execution>
        </executions>
      </plugin>        
   </plugins>
 </build>

But when I run my test I get a compilation error stating that my bean could not be found, but I have it as a dependency and my IDE does not complain about the missing bean, but maven does:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:testCompile (default-testCompile) on project tests: Compilation failure: Compilation failure:
[ERROR] /home/r/projects/myproj/trunk/tests/src/test/java/com/myproj/MyTest.java [3,19]package com.myproj.beans does not exist

The package com.myproj.beans does exist in the maven module ws-ejb that I have set as a dependency in the tests module. So what is wrong?

EDIT

This is MyTest.java located in the tests maven module under src/test/java/com/myproj/MyTest.java

package com.myproj;

import com.myproj.beans.MyBean; // compilation error here. If I remove this line it works and the test is run! 
//MyBean is located at `ws-ejb` maven module under src/main/java/com.myproj.beans
import javax.ejb.EJB;
import org.junit.Test;

public class MyTest {
    @Test
    public void test() {System.out.println("Print something...");}
}

Solution

  • Since you did not post any example code i can only guess. But my guess would be that your test-code is not in the test-folder in your project. So what does this mean? this means that maven just tries to compile and/or run your test-project and this causes the exception. Try removing the scope attribute from your dependency and try it again. The test-scope will only work if you use the test-folder with for example JUnit. If your code is in the "main" folder this would not work as the dependency is not accessible at runtime. If something is unclear or you have some information that i did not get please comment :)
    Kind regards
    EDIT: To expand a little bit on this: I would strongly advice writing the test-code for a given artifact in the artifact it self as tests. In that way you will get a maven-build failure in the artifact that has the error, if the tests fail. To see how this works you could just add a JUnit test for a specific class (most IDEs will support you with that)
    EDIT 2: Ok, i did a quick googlesearch: this article could provide an answer on how to use the classes. I hope this does the trick :)