Home > Software engineering >  Spring Boot multi-module project: Could not resolve dependencies
Spring Boot multi-module project: Could not resolve dependencies

Time:04-02

Currently, I am trying to figure out how Spring Boot multi-module projects are working. The project is set up to contain:

  • a shared library
  • two spring boot applications
  • one parent pom

I can build all modules by calling mvn package from the root module. However, when I try to call the same goal from within a spring boot module I get

[ERROR] Failed to execute goal on project spring-boot-app-2: Could not resolve dependencies for project io.azureblue:spring-boot-app-2:jar:0.0.1-SNAPSHOT: Could not find artifact io.azureblue:shared-library:jar:1.0-SNAPSHOT -> [Help 1]

I don't understand why. This is just a demo project, so it's no big deal to compile everything together. But in a real life situation it could take some considerable amount of time and I would like to compile modules separately.

When I look in my repository folder (.m2/repository/), I can see the shared library is there (io/azureblue/shared-library/...). Below are my POM files. You can find the entire project in my repository.

What am I doing wrong here?

Parent POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>io.azureblue</groupId>
    <artifactId>spring-boot-multi-module-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-multi-module-demo</name>
    <description>spring-boot-multi-module-demo</description>
    <properties>
        <java.version>11</java.version>
    </properties>

    <packaging>pom</packaging>
    <modules>
        <module>spring-boot-module-1</module>
        <module>spring-boot-module-2</module>
        <module>shared-library</module>
    </modules>

</project>

Shared-Library pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-boot-multi-module-demo</artifactId>
        <groupId>io.azureblue</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>shared-library</artifactId>
    <groupId>io.azureblue</groupId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>18</maven.compiler.source>
        <maven.compiler.target>18</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

Spring Boot POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>io.azureblue</groupId>
    <artifactId>spring-boot-app-1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>io.azureblue</groupId>
            <artifactId>shared-library</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

CodePudding user response:

mvn package only builds the .jar file inside the project's target directory. To actually copy the built artifact inside your local maven repository (to be able to use it as a dependency when not building the whole project), you need to use mvn install on your multi-module project (or at least on the shared-library project). After that, you can invoke mvn package from one of the spring-boot modules without any problem.

CodePudding user response:

Maven multi-module projects can be built via the aggregator which will build all the child modules and properly resolve the interdependencies, the children can also be built independently, but the dependency resolution will not go the other way.

If you're building a single module in a 3-module project, you either have to build it from the top-level project or ensure that all of its dependencies are available in your repository.

In your case, your shared library which is a dependency of your other two modules needs to be available. You can run mvn install from the share-library module to accomplish this. After this, you will be able to build your other two. This does beg the question though, if you don't want to build it all together, what's the purpose of doing this as a multi-module project? You can easily just make the share-library a separate project altogether.

  • Related