Andranik Chorokhyan
Andranik Chorokhyan

Reputation: 567

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!

Upvotes: 12

Views: 11583

Answers (6)

Sahil Sheikh
Sahil Sheikh

Reputation: 1

remove spring boot plugin and add this plugin.

        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.11.0</version>  <!-- Use the latest version -->
        <configuration>
            <source>21</source> 
            <target>21</target>
        </configuration>
    </plugin>

Upvotes: 0

Vadim
Vadim

Reputation: 31

In my case, it all solved after I wrapped all plugins in the root .pom file in pluginManagement tag.

Upvotes: 0

Oleksandr Semenchenko
Oleksandr Semenchenko

Reputation: 81

The spring-boot-maven-plugin when you install/deploy repackages your project and places it into a local/remote repository, this jar file cannot be used as a library, according to the documentation you should configure the plugin to install/deploy only the original jar file.

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>repackage</id>
            <goals>
                <goal>repackage</goal>
            </goals>
            <configuration>
                <attach>false</attach>
            </configuration>
        </execution>
    </executions>
</plugin>

Upvotes: 4

Spring-boot-maven-plugin should be removed from the pom.xml inside the library, because there is no need to build an executable jar for a Library project.

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

see more details in multi-module

Upvotes: 1

ozkanpakdil
ozkanpakdil

Reputation: 4612

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>

Upvotes: 15

Andranik Chorokhyan
Andranik Chorokhyan

Reputation: 567

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>

Upvotes: 8

Related Questions