java - Spring Boot中的多模块依赖管理问题

标签 java spring spring-boot spring-boot-maven-plugin

我在 spring boot 多模块 jar 部署方面遇到问题

我有如下 3 个组件。

Parent
   --Child1
   --Child2

Child2 依赖于 child1

但是当我拿 jar child1 类时,jar 中不存在。我收到 java.lang.classnotfoundException

在我的下面

A pom.xml

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
</parent>

<groupId>com.A</groupId>
    <artifactId>A</artifactId>
    <version>0.0.1</version>
    <packaging>pom</packaging>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <type>pom</type>
                <version>${spring.boot.version}</version>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>${spring.boot.version}</version>
            </dependency>
            <dependency>
                <groupId>com.A</groupId>
                <artifactId>A</artifactId>
                <version>0.0.1</version>
            </dependency>
            <dependency>
                <groupId>com.B</groupId>
                <artifactId>B</artifactId>
                <version>0.0.1</version>
            </dependency>
            </dependencies>
        </dependencyManagement>
            <build>
    <finalName>service</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

    </build>

B pom.xml

<groupId>com.B</groupId>
    <artifactId>B</artifactId>
    <version>0.0.1</version>
    <name>Omni Channel Core</name>
    <description>Omni Channel Core Functions</description>
    <parent>
        <groupId>A</groupId>
        <artifactId>A</artifactId>
        <version>0.0.1</version>
    </parent>
    <packaging>jar</packaging>


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

C pom.xml

<groupId>com.C</groupId>
    <artifactId>C</artifactId>
    <version>0.0.1</version>
    <parent>
        <groupId>A</groupId>
        <artifactId>A</artifactId>
        <version>0.0.1</version>
    </parent>
    <packaging>jar</packaging>


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

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

    </build>

每当我将 C 的 jar 放入其中时,依赖的 jar B 就不存在。

如何解决此问题任何帮助将不胜感激!!!

最佳答案

maven 多模块 spring-boot 项目的配置非常简单。检查this sample我根据你的问题创建的。

我有以下三个模块:

multi-modules
   --module-B
   --module-C

所以我的多模块基本上相当于你的模块A

父pom的配置

我的parent's pom配置与您的非常相似,除了我还配置了一个部分,在其中声明该父级的所有子模块的列表。我们可能无法始终做到这一点,但在这种特殊情况下,这是可能的,因为我在同一个项目中创建父模块和子模块。这样做的一个小好处是,我可以通过运行(在父级级别)这样的命令从父级顺序构建所有子模块:mvn clean install

<modules>
   <module>module-B</module>
   <module>module-C</module>
</modules>

子模块的配置

为了简化事情,我们可以使多模块项目的逻辑结构与其物理结构相匹配,即有一个父级的 pom 目录,其中包含两个相同级别的子目录,每个模块一个。

matching physical structure of multi-module project

这样我们就不必进行任何丑陋的相对路径配置。

模块-B

没有什么特别的configuration 。我添加了一个虚拟 ServiceB 类,其中包含一些简约的内容。

模块-C

configuration与 module-B 类似,只是它依赖于 module-B,当然我利用在父级中完成的依赖项管理配置来仅使用 groupId 定义对 module-B 的依赖项和 artifactId

构建和检查

此时,我们可以在父级别运行 mvn clean install 并检查 module-B 依赖项是否进入 module-C

enter image description here

我们可以在模块 C 库列表中看到这一点:

enter image description here

希望这有帮助。

关于java - Spring Boot中的多模块依赖管理问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60676215/

相关文章:

java - 在 spring boot 应用程序中组织服务、服务实现和存储库的最佳实践

java - 杰汤。按顺序打印所有文本节点

java - 如何访问 PDE Incubator Dependency Visualization 的源代码

java - Vavr 注释处理工具未启动

mysql - java.sql.SQLException : Operand should contain 1 column(s) - Spring

java - 在运行时动态添加 spring 上下文配置?

javascript - session 奇怪地返回 null

java - 使用 Spring 定义 Mongo 模式验证

spring-boot - 如何在 spring-boot 应用程序中外部化 i18n 属性文件

java - 替换 Java 字符串中的 '\' 字符