2

I have a setup like the following:

IntelliJ setup

Where I have two modules: modulea and moduleb, in this case, moduleb has a dependency to modulea defined as:

<dependency>
    <groupId>org.example</groupId>
    <artifactId>module-a</artifactId>
    <version>1.0-SNAPSHOT</version>
    <type>test-jar</type>
</dependency>  

This allows me to use ClassInTestA in ClassInSourceB without any issues while developing:

Using class in test folder in the source folder of another project

However, when I try to build the project, this error prevents IntelliJ to complete the build:

Compilation error

I have come across similar questions in SO:

However, none of the proposed solutions has been able to help my case. I have created an MVCE that is available here as zip and in GitHub.

The real-world project I'm working is neo4j, which follows this structure. Moreover, compilations using mvn install/package work without any issue, the problem appears when working inside IntelliJ.

io_exception
  • 154
  • 1
  • 13
  • What is the intention of `ClassInTestA` and `ClassInSourceB`? Why does `ClassInSourceB` need to extend from `ClassInTestA`? – domids Jul 26 '18 at 08:12
  • That is just a simplification of the structure followed by neo4j, a major Java project. In the real code there are several classes like that, for different purposes. – io_exception Jul 26 '18 at 08:14

3 Answers3

1

In general, it makes sense to "open" a new project by building it first outside of IntelliJ with mvn clean package and then import it by just "open"ing the parent module. This worked for me:

enter image description here

And even after a rebuild:

enter image description here

If you don't want to reimport your project by deleting all IDEA folders and files and use the described way above, you can try to build the project via the Maven toolbar (clean and package on the parent module) and then use the "Reimport all Maven projects" button:

enter image description here

At least sometimes this works for me, but honestly not always.

Marcus K.
  • 980
  • 11
  • 26
  • By building it first outside you mean doing `mvn install` or `mvn package`? – io_exception Jul 26 '18 at 08:08
  • Yes, exactly. And as a side note: I almost never use `mvn install` and prefer `mvn package`. Why "pollute" my local repository with "work in progress" snapshots if I don't have to? :-) – Marcus K. Jul 26 '18 at 08:29
  • Perfect, I'll try that with both my toy project and neo4j (doing `mvn package/install` takes almost 14 minutes in my machine!) and I'll let you know how it went. Thanks. – io_exception Jul 26 '18 at 08:31
1

Test classes aren't packed in the final artifact. To share the test classes you'll have to use the jar maven-jar-plugin in modulea:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <executions>
            <execution>
                <goals>
                    <goal>test-jar</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

and add a dependency in moduleb's pom.xml:

<dependency>
    <groupId>org.example</groupId>
    <artifactId>module-a</artifactId>
    <version>1.0-SNAPSHOT</version>
    <classifier>tests</classifier>
</dependency> 
Shmulik Klein
  • 3,754
  • 19
  • 34
  • The second code block seems to work on my toy project, I previously tested adding both `classifier` and `type` tags, now, using only `classifier` the project compiles. I'll try in the bigger, more complex project and I'll get back to you. Thanks. – io_exception Jul 26 '18 at 08:22
0

Test sources are not included during the compile phase

See Apache Maven Compiler Plugin:

compiler:compile is bound to the compile phase and is used to compile the main source files.

I think main sources should not depend on test sources. Test sources are only for testing the main sources. You could place ClassInTestA under module-a/src/main/java.

domids
  • 515
  • 5
  • 21
  • Yes, I have read several comments that state the same as you: "main sources should not depend on test sources", however, the project I'm working on has the same structure (and apparently it works in that way for all the other developers) and I find it hard to change the whole codebase. – io_exception Jul 26 '18 at 08:25