0

I have two Maven projects A and B, where project B is nested in project A. The structure looks like the following:

Project A:
    src/test/java:
        DependencyClass.java
    Project B:
        src/test/java:
            MyTest.java
        pom.xml
    pom.xml

I'm trying to import DependencyClass.java (project A) into MyTest.java (project B), and be able to invoke the methods in DependencyClass.java from MyTest.java. But when I tried to package project B, it failed at the compile phase:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project : Compilation failure:
[ERROR] MyTest.java:[7,41] package XXX does not exist
[ERROR] MyTest.java:[17,9] cannot find symbol
[ERROR] symbol:   class DependencyClass.java

I have found some answers such as this and this, and I have done the following:

  1. added the maven-jar-plugin with a goal of "test-jar" in the pom.xml of project A

  2. added the dependency to project A in project B's pom.xml, and declared type as "test-jar" and scope as "test".

Eclipse is able to resolve DependencyClass.java without giving errors, but it just failed when I tried to build with Maven. Any suggestion or idea on what I might be missing? Thanks in advance

Edit: I have also tried adding another dependency in project B's pom.xml, with the following scope and type:

<scope>compile</scope>
<type>jar</type>

But I got more errors when building project B - now it even starts to complain about jUnit and other built-in classes.

fittaoee
  • 147
  • 1
  • 5
  • 14
  • Have you installed the project projectA-test.jar in your repository ?? – nhatnq Sep 28 '21 at 05:11
  • @nhatnq Yes I have run "mvn clean install" and installed the test jar file successfully. – fittaoee Sep 28 '21 at 05:22
  • It would be good if you could post your pom content (especially the one for the project B). – Piotr Michalczyk Sep 28 '21 at 15:34
  • Thank you both. I have resolved my error. The fix was to remove the tag. So now in project B's pom.xml, I declared two dependencies of project A, one with jar and another with test-jar, and both don't have declared. – fittaoee Sep 28 '21 at 20:35

1 Answers1

1

I have resolved my error. The fix was to remove the <scope> tag. So now in project B's pom.xml, I declared two dependencies of project A, one with <type>jar</type> and another with <type>test-jar</type>, and both don't have <scope> declared.

In project A's pom.xml, the execution goal of the "maven-jar-plugin" should specify both "jar" and "test-jar".

bad_coder
  • 11,289
  • 20
  • 44
  • 72
fittaoee
  • 147
  • 1
  • 5
  • 14