javamavenintellij-ideacompilationmulti-module

Maven doesn't find imported class from another module but Intellij does


I have multi-module maven project. The acceptance-tests module has dependency from api module in pom.xml (Replacing real company name by xxx to keep confidentiality). I am trying to import some classes from api module in my acceptance-tests.

Here is my pom.xml dependency of acceptance-tests module:

        <dependency>
            <artifactId>xxx-api</artifactId>
            <groupId>com.xxx</groupId>
            <version>${xxx.api.version}</version>
        </dependency>

The api module separately is being installed and packaged (mvn install, mvn package) by maven without any issue. The jar file is being created in my local .m2.

However, when I try to compile the acceptance-tests module, I get a compilation error saying that the the classes cannot be imported because the package is not found.

Here is the actual error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project xxx-acceptance-tests: Compilation failure: Compilation failure: 
[ERROR] /Users/andranik_chorokhyan/mydisk/Projects/XXX/automation/xxx-project-test-automation/xxx-acceptance-tests/src/main/java/com/xxx/xxx/utilities/api/ApiPayloadUtils.java:[17,38] package com.xxx.domain.dto does not exist
[ERROR] /Users/andranik_chorokhyan/mydisk/Projects/XXX/automation/xxx-project-test-automation/xxx-acceptance-tests/src/main/java/com/xxx/xxx/utilities/api/ApiPayloadUtils.java:[18,38] package com.xxx.domain.dto does not exist
[ERROR]   symbol:   class MappingData
[ERROR]   location: class com.xxx.utilities.api.ApiPayloadUtils

One more interesting fact is that there is no error visible in Intellij IDEA. No red underline, no compilation error, no problem with navigating to the appropriate imported file. And in reality, the com.xxx.domain.dto package does exist and the MappingData class as well.

I removed whole xxx directory from my local .m2 repository and executed mvn clean dependency:resolve command. It succeeded as well.

Does anybody know what's the problem here and how it can be solved? Thanks in advance!


Solution

  • Finally I have found the solution. Thanks JF Meier and khmarbaise for hints.

    It appeared Maven doesn't allow dependency from executable jar. This was my case. My api module was an executable Spring Boot application and not reusable library.

    So, the solution was the following:

    1. It was necessary to find the Application.java file in api module.
    2. Add maven-jar-plugin with exclusion of the Application.java file and specification of some classifier
    3. Making dependency in acceptance-tests module from the above specified classifier instead of standard jar

    Plugin specification in api module below:

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                            <configuration>
                                <classifier>qa</classifier>
                                <excludes>
                                    <exclude>**/Application*</exclude>
                                </excludes>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    

    Dependency in acceptance-tests module below:

            <dependency>
                <artifactId>xxx-api</artifactId>
                <groupId>com.xxx</groupId>
                <version>${api.version}</version>
                <classifier>qa</classifier>
            </dependency>