Spring Boot 多模块项目:无法解决依赖关系

Mat*_*tze 1 java spring maven spring-boot

目前,我正在尝试弄清楚 Spring Boot 多模块项目是如何工作的。该项目设置为包含:

  • 共享库
  • 两个 Spring Boot 应用程序
  • 一位家长 pom

mvn package我可以通过从根模块调用来构建所有模块。然而,当我尝试从 Spring Boot 模块中调用相同的目标时,我得到了

[错误] 无法在项目 spring-boot-app-2 上执行目标:无法解析项目 io.azureblue 的依赖项:spring-boot-app-2:jar:0.0.1-SNAPSHOT:找不到工件 io.azureblue :shared-library:jar:1.0-SNAPSHOT -> [帮助 1]

我不明白为什么。这只是一个演示项目,所以将所有内容编译在一起没什么大不了的。但在现实生活中,这可能需要相当长的时间,我想单独编译模块。

当我查看我的存储库文件夹 ( .m2/repository/) 时,我可以看到共享库在那里 ( io/azureblue/shared-library/...)。下面是我的 POM 文件。您可以在我的存储库中找到整个项目。

我在这里做错了什么?

父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>
Run Code Online (Sandbox Code Playgroud)

共享库 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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

小智 5

mvn package.jar仅在项目目录内构建文件target。要实际将构建的工件复制到本地 Maven 存储库中(以便在不构建整个项目时能够将其用作依赖项),您需要mvn install在多模块项目上使用(或至少在项目上shared-library)。之后,您可以mvn package毫无问题地从 spring-boot 模块之一调用。