1

I know this question has been asked numerous times but the methods I've read just doesn't work for me. I tried this but it still won't work.

I have a subproject(A) that depends on another subproject(B).

Both subprojects are contained in a parent directory with a parent pom.xml (declares A and B as modules) within their respective subdirectories.

Compiling and installing the project works fine with the maven-assembly-plugin but when I try to test A, it doesn't recognize the classes from B. I have tried installing it first and then testing but it still won't find the classes. What am I missing?

Error:

 [ERROR] Failed to execute goal
 org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile
 (default-testCompile) on project (A)

[ERROR] TestService.java:[8,17] cannot find symbol 

[ERROR]   symbol:   class **Model** (the class I'm referencing in B)

[ERROR]   location: class **TestService** 
(test in A that tests the class in ../service/src/main/java/Service.java)

Edit:
/project
--/service (this depends on model;this is also what I want to test)
---/src
----/main
-----/java
------/Service.java
----/test
-----/java
------/TestService.java

--/model (independent)
---/src
----/main
-----/java
------/Model.java

--/entry (this depends on service; entry point of the whole project)
--pom.xml (parent pom)

Each of the three projects have their own pom.xml inside.

/model/pom.xml contains no dependencies and no plugins

here's parent: parent/pom.xml

  ...
  <modules>
    <module>entry</module>
    <module>service</module>
    <module>model</module>
  </modules>

here's entry:

 /service/pom.xml
        ...
    <parent>
        <groupId>com.some.project</groupId>
        <artifactId>project</artifactId>
        <version>xx</version>
    </parent>
    <artifactId>entry</artifactId>
    <packaging>jar</packaging>
    <version>xx</version>
    <name>entry</name>
    <build>
     ...
     <!--assembly plugin is declared here-->
    </build>
    <dependencies>
      <dependency>
      <groupId>com.some.project</groupId>
      <artifactId>service</artifactId>
      <version>xx</version>
      </dependency>
    </dependencies>

here's service:

/service/pom.xml
    ...
<parent>
    <groupId>com.some.project</groupId>
    <artifactId>project</artifactId>
    <version>xx</version>
</parent>
<artifactId>service</artifactId>
<packaging>jar</packaging>
<version>xx</version>
<name>service</name>
<dependencies>
  <dependency>
  <groupId>com.some.project</groupId>
  <artifactId>model</artifactId>
  <version>xx</version>
  </dependency>

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>
</dependencies>
ReconditusNeumen
  • 121
  • 1
  • 2
  • 12

1 Answers1

0

Maven assembly plugin is used usually when you want to package the already compiled sources, so its kind of not related here.

If you have two projects, A and B and B has to depend on A, you have to define in B's pom.xml a dependency (trivial stuff):

<dependency>
  <groupId>YOUR_GROUP_ID</groupId>
  <artifactId>A</artifactId>
  <version>YOUR_VERSION</version>
</dependency>

This will instruct maven to setup classpath during the build.

Now, Depending on artifact type of A and B maven can decide on some steps after the compilation, for example, if B is a WAR, A will be included in WEB-INF/lib folder of B because of such a dependency.

But in general, if A & B are jars, maven won't use this information only for the compilation/tests and not for packaging.

Now, maven has different classpaths for different phases: in particuler, one for compilation, and one for unit tests.

So if you want to specify that the dependency is required only for tests but should not be considered as a "compile" dependency, then define the scope:

<dependency>
  <groupId>YOUR_GROUP_ID</groupId>
  <artifactId>A</artifactId>
  <version>YOUR_VERSION</version>
  <scope>test</scope>
</dependency>

If you don't specify any scope, maven will conclude that the dependency is required both for compilation and for tests and maybe even for packaging as I've explained earlier.

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • I have added it as a dependency but the Test Classes for B still won't find the classes in A. I have tried using the "compile" scope and "test" scope. – ReconditusNeumen Jul 25 '18 at 04:02
  • Any chance the the class from A you refer in B resides in "src/test/java" and not in "src/main/java"? Can you use classes from A's src/main/java ? – Mark Bramnik Jul 25 '18 at 04:23
  • The class from A that I'm referring to in B is in "src/main/java". It's a model class that B needs (B is a service class) and so if I want to test B, I need to instantiate the class in A. (Again for clarity in this case, B depends on A; B is a service, A is a model) – ReconditusNeumen Jul 25 '18 at 04:47
  • In this case, it's a use case that is totally supported by maven, so the problem must by purely technical with some wrong definitions in pom.xmls. I'm affraid, without actual pom-s and a sample file of service and model it won't be easy to help you out here – Mark Bramnik Jul 25 '18 at 04:50
  • Did you run Maven from the parent project or in project A? – jensp Jul 25 '18 at 05:17
  • @MarkBramnik I've now included the pom.xml along with some other sub-projects that might be of concern. – ReconditusNeumen Jul 25 '18 at 05:21
  • @jensp I tried it with both and it still won't test. It can package and install if I remove the tests though. – ReconditusNeumen Jul 25 '18 at 05:22