Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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!

like image 956
Andranik Chorokhyan Avatar asked Apr 21 '20 10:04

Andranik Chorokhyan


People also ask

How do I import a class from another package in IntelliJ?

IntelliJ IDEA suggests to import single classes by default. You can change the settings to import entire packages instead. In the Settings/Preferences dialog ( Ctrl+Alt+S ), select Editor | Code Style | Java | Imports. Clear the Use single class import checkbox, and apply the changes.

How does IntelliJ recognize Maven project?

In the Project tool window, right-click your project and select Add Framework Support. In the dialog that opens, select Maven from the options on the left and click OK. IntelliJ IDEA adds a default POM to the project and generates the standard Maven layout in Project tool window.

How do I create a multi module Maven project in IntelliJ?

In the Project tool window, right-click the project folder and select New | Module. Alternatively, from the main menu, select File| New | Module to open the New Module wizard. If you used main menu to add a module then the process of adding a module is the same as Creating a new Maven project.


2 Answers

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>
like image 144
Andranik Chorokhyan Avatar answered Sep 30 '22 19:09

Andranik Chorokhyan


I was also getting symbol not found errors while compiling with maven, and the solution is for spring boot 2 you need to configure plugin as below, classifier exec

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <classifier>exec</classifier>
        </configuration>
    </plugin>

If you are working with spring boot 1

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </execution>
        </executions>
    </plugin>
like image 31
ozkanpakdil Avatar answered Sep 30 '22 20:09

ozkanpakdil