Cannot compile with maven while importing a local project as dependency

Valrog :

I'm creating a common library that will be used accross several Spring application. I've extracted the common classes I want to share between these application into a new Spring application named "Common". I've used mvn install on this application to put it in my local maven repository. On my main project, I've added the maven dependency, with the same version, in the pom.xml.

Now, if I try to launch mvn compile on my main project, I got this error :

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project recruitment: Compilation failure: Compilation failure:
[ERROR] /D:/[...]/application/src/main/java/com/example/application/service/Service.java:[8,37] package com.example.common.exception does not exist

Indicating that my common library is not present and thus my application cannot find package coming from it.

If I try to launch my Spring application from the main (using Spring Boot), it launchs perfectly and behaves like before.

I'm using IntelliJ on Windows, maven is bundled with IntelliJ with the default installation. If I check in my local maven repository I can find the common library jar. IntelliJ does not show any error on my imports, it recognizes my library.

There seem to be some conflict between IntelliJ, which read the pom.xml to find libraries imported for my application, and maven that use the same pom.xml to compile my code.

I used mvn compile -X to have more information and the funny part is that maven is using the common library jar from the local repository :

[INFO] Changes detected - recompiling the module!
[DEBUG] Classpath:
[DEBUG]  D:\[...]\application\target\classes
[DEBUG]  C:\[...]\.m2\repository\com\example\common\0.0.1\common-0.0.1.jar
  • Do you have any idea why I can't compile my project with maven while I can launch it as a Spring application ?

Here is my dependency in my pom.xml :

<dependency>
    <groupId>com.example</groupId>
    <artifactId>common</artifactId>
    <version>0.0.1</version>
    <scope>compile</scope>
</dependency>

And the common library definition :

<groupId>com.example</groupId>
<artifactId>common</artifactId>
<version>0.0.1</version>
<name>common</name>
<description>Common project with generic technical modules</description>
Valrog :

Okay, that's embarassing... I just found the solution ! It has been two days looking for a solution to this tricky situation and I just found what was missing, or rather what was too much.

This bad boy :

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

The Spring boot maven plugin is what makes my Spring boot application launchable from maven, and it seems that it affects the way the common jar library is made, or the way it is imported.

I don't really understand why exactly it affects my import, but I deleted the plugin above in my common library pom.xml, I reinstalled in my local repository and I launch mvn install again on my main application... And it now works !

I think that launching via Spring boot used a little bit of Spring boot magic to make it works that the maven way lacks.

If someone understands it better and can explain it, that would be great and the accepted answer I think.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=418271&siteId=1