简体   繁体   English

为什么Maven + Spring Boot创建巨大的jar文件?

[英]Why does Maven + Spring Boot create huge jar-files?

I have the following Maven project structure: 我具有以下Maven项目结构:

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

And the following simplified POMS: 以及以下简化的POMS:

parent_project.pom parent_project.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>

main_application main_application

<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>

module_1 module_1

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

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

module_2 模块_2

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

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

module_3 module_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>

In reality I have more modules and more of them are dependencies of others. 实际上,我有更多模块,其中更多模块是其他模块的依赖项。 When I run mvn install I get a 1.2GB file for the main application. 当我运行mvn install ,主应用程序得到一个1.2GB的文件。 I noticed that all dependencies of all modules are assembled into the modules. 我注意到所有模块的所有依赖项都组装到了模块中。 Thus many jar-files are multiple times assembled into the file. 因此,许多jar文件会多次组装到文件中。 How can I avoid this? 如何避免这种情况?

You have declared the spring-boot-maven-plugin in your parent pom. 您已经在父pom中声明了spring-boot-maven-plugin Due to this each created artifact will be an executable jar file and all these executable jar files contain the dependencies needed for the jar. 因此,每个创建的工件都将是一个可执行jar文件,并且所有这些可执行jar文件都包含jar所需的依赖项。

domain_models_and_repository contains all the dependencies declared in the parent and its own dependencies. domain_models_and_repository包含在父级中声明的所有依赖项及其自身的依赖项。

module 1 and module 2 contain all the parent dependencies, the locally declared dependencies and all dependencies expressed by the domain_models_and_repository project as well as the module itself. 模块1模块2包含所有父依赖项,本地声明的依赖项以及domain_models_and_repository项目表示的所有依赖项以及模块本身。

module 3 contains all the parent dependencies, its own locally declared dependencies and all other not yet available dependencies from domain_models_and_repository , module 1 and module 2 as well the those modules itself. 模块3包含所有的父依赖项,它自己在本地声明的依赖项以及domain_models_and_repository模块1模块2以及所有这些模块本身的所有其他尚不可用的依赖

main application contains the parent dependencies its own locally declared dependencies and all other not yet available dependencies from domain_models_and_repository , module 1 and module 2 as well the those modules itself. 主应用程序包含父依赖项,它自己在本地声明的依赖项以及domain_models_and_repository模块1模块2以及这些模块本身中所有其他尚未使用的依赖

To fix remove the spring-boot-maven-plugin from the parent and only add it to the pom of your main application. 要解决此问题,请从父级中删除spring-boot-maven-plugin ,仅将其添加到主应用程序的pom中。 That way only your main application is an executable jar and all other modules are just plain jar files. 这样,只有您的主应用程序是可执行jar,而所有其他模块只是普通的jar文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM