Search code examples
javaspring-bootmavenmulti-module

Maven modules use different versions of same library and parent compile gives error


I have a springboot project which uses package javax.servlet from org.apache.tomcat.embed:tomcat-ember-core9.0.34. (this is coming from springboot)

I have another maven project (API project) which uses this javax.servlet but with version 2.3

Now I have created a parent maven project which includes above 2 projects as modules. When I individually compile the springboot project, everything is fine. But when I compile the parent project, it takes the javax.servlet.api version 2.3 to compile the springboot project and gives error, since a method is not available in 2.3 which is being used by springboot.

How to solve this?

Please find a sample source code here: https://github.com/abmjunaed/maven-multi-project-error

I have added very minimal code to reproduce the scenario so that you can have a look and help!

In the External Libraries of IntelliJ, I can see both libs and maven from the parent project is taking the javax-servlet-api:2.3 to compile the springBoot project and giving the error

enter image description here


Solution

  • Found the solution and I can't believe it!

    Dependency to spring-boot-starter-web was after the dependency of the library project and that was causing the problem.

    Now I have added

    spring-boot-starter-web

    as the first dependency and

    lib-project

    as the 2nd dependency and everything works fine!

    <dependencies>
        <dependency> <!--1st dependency-->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>library</artifactId>
            <version>0.0.2-SNAPSHOT</version>
        </dependency>
    </dependencies>