有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么Maven+Spring引导会创建巨大的JAR文件?

我有以下Maven项目结构:

parent_project
+--main_application
+--domain_models_and_repository
+--module_1
+--module_2
+--module_3

以及以下简化的POM:

父项目。pom

<project>
    <dependencies>
        [Spring Boot dependencies]
    </dependencies>

    <modules>
        <module>main_application</module>
        <module>domain_models_and_repository</module>
        <module>module_1</module>
        <module>module_2</module>
        <module>module_3</module>
    </modules>

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

主应用程序

<project>
    <parent>
        <artifactId>parent_project</artifactId>
    </parent>

    <dependencies>
        <dependency>
            <artifactId>domain_models_and_repository</artifactId>
        </dependency>
        <dependency>
            <artifactId>module_1</artifactId>
        </dependency>
        <dependency>
            <artifactId>module_2</artifactId>
        </dependency>
        <dependency>
            <artifactId>module_3</artifactId>
        </dependency>
    </dependencies>
</project>

模块_1

<project>
    <parent>
        <artifactId>parent_project</artifactId>
    </parent>

    <dependencies>
        <dependency>
            <artifactId>domain_models_and_repository</artifactId>
        </dependency>
    </dependencies>
</project>

模块2

<project>
    <parent>
        <artifactId>parent_project</artifactId>
    </parent>

    <dependencies>
        <dependency>
            <artifactId>domain_models_and_repository</artifactId>
        </dependency>
    </dependencies>
</project>

模块3

<project>
    <parent>
        <artifactId>parent_project</artifactId>
    </parent>

    <dependencies>
        <dependency>
            <artifactId>domain_models_and_repository</artifactId>
        </dependency>
        <dependency>
            <artifactId>module_1</artifactId>
        </dependency>
        <dependency>
            <artifactId>module_2</artifactId>
        </dependency>
    </dependencies>
</project>

实际上,我有更多的模块,其中更多是其他模块的依赖项。当我运行mvn install 我得到一个1.2GB的主应用程序文件。我注意到所有模块的所有依赖项都已组装 进入模块。因此,许多jar文件被多次组装到该文件中。我怎样才能避免这种情况


共 (1) 个答案

  1. # 1 楼答案

    您已经在父pom中声明了spring-boot-maven-plugin。因此,每个创建的工件都将是一个可执行的jar文件,所有这些可执行的jar文件都包含jar所需的依赖项

    域_模型_和_存储库包含父项中声明的所有依赖项及其自身的依赖项

    模块1模块2包含所有父依赖项、本地声明的依赖项、由域_模型_和_存储库项目以及模块本身表示的所有依赖项

    模块3包含所有父依赖项、其本地声明的依赖项和所有其他尚未从域_模型_和_存储库模块1模块2以及这些模块本身获得的依赖项

    主应用程序包含父依赖项、它自己本地声明的依赖项,以及来自域_模型_和_存储库模块1模块2以及这些模块本身的所有其他尚未可用的依赖项

    要修复此问题,请从父应用程序中删除spring-boot-maven-plugin,并仅将其添加到主应用程序的pom中。这样一来,只有主应用程序是可执行的jar,而所有其他模块都只是普通的jar文件