Maven 多模块项目的问题:未找到依赖项

问题描述 投票:0回答:1

我正在开发一个 Maven 多模块项目,并且在两个模块中使用通用包时遇到问题。这是项目结构:

enter image description here

目标: 我想在 image-gen 和 module2 模块中使用通用包。另外,我想:

  1. 为所有组合模块创建一个 JAR 文件,例如:

    ./mvnw package  #This should create one JAR file containing all modules (pseudocode)
    
  2. 根据需要为每个模块创建单独的 JAR 文件,例如:

    ./mvnw clean package -pl image-gen  # This should create a JAR file for the image-gen module
    

当前设置: 家长

pom.xml

<?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>3.3.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>testapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>common</module>
        <module>image-gen</module>
        <module>module2</module>
    </modules>
    <name>testapp</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

用于 image-gen 和 module2 的 pom.xml:

<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://www.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>testapp</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>image-gen</artifactId> <!-- or module2 -->
    <version>1.0-SNAPSHOT</version>

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

    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

问题: 当我运行

./mvnw clean package
时,构建失败并显示以下错误消息:

[INFO] common 1.0-SNAPSHOT ................................ SUCCESS [  2.105 s]
[INFO] image-gen 1.0-SNAPSHOT ............................. FAILURE [  0.582 s]
[INFO] module2 1.0-SNAPSHOT ............................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.658 s
[INFO] Finished at: 2024-07-08T10:27:28+09:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile (default-compile) on project image-gen: Compilation failure
[ERROR] /C:/Projects/testapp/image-gen/src/main/java/com/example/imagegen/service/ImgGenService.java:[5,33] package com.example.common.entity does not exist
[ERROR]
[ERROR] -> [Help 1]

期望的结果:

  1. 能够构建所有模块并使用image-gen和module2中的通用模块。

  2. 能够为所有模块创建一个组合的 JAR 文件,例如:

    ./mvnw package

  3. 能够为每个模块创建单独的JAR文件,例如:

    ./mvnw clean package -pl image-gen

您可以通过克隆此存储库并运行来重现相同的结果:

git clone https://github.com/Muneeer/testapp.git
cd testapp
./mvnw clean package

任何有关解决此问题的帮助或指导将不胜感激!

附加信息:

Java版本:17

Maven版本:3.8.1

Spring Boot版本:3.3.1

spring-boot maven dependency-management multi-module
1个回答
0
投票

与 khmarbaise 评论的相同。 Spring boot 子模块应该指向 spring-boot-starter-parent,而不是父 pom.xml。在构建依赖于公共模块的单独子应用程序时还需要使用 make 标志

./mvnw clean package -pl image-gen -am

使用公共库创建多模块示例:https://github.com/akang1108/so-78718963

© www.soinside.com 2019 - 2024. All rights reserved.