为什么Maven + Spring Boot创建巨大的jar文件? [英] Why does Maven + Spring Boot create huge jar-files?

查看:107
本文介绍了为什么Maven + Spring Boot创建巨大的jar文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下Maven项目结构:

I have the following Maven project structure:

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

以及以下简化的POMS:

And the following simplified POMS:

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

<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

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

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

module_2

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

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

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>

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

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. I noticed that all dependencies of all modules are assembled into the modules. Thus many jar-files are multiple times assembled into the file. How can I avoid this?

推荐答案

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

You have declared the spring-boot-maven-plugin in your parent pom. 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.

domain_models_and_repository 包含在父级中声明的所有依赖项及其自身的依赖项.

domain_models_and_repository contains all the dependencies declared in the parent and its own dependencies.

模块1 模块2 包含所有父依赖项,本地声明的依赖项以及 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.

模块3 包含 domain_models_and_repository 模块1 模块2 以及这些模块本身.

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.

主应用程序包含父依赖项,其自己本地声明的依赖项以及 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.

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

To fix remove the spring-boot-maven-plugin from the parent and only add it to the pom of your main application. That way only your main application is an executable jar and all other modules are just plain jar files.

这篇关于为什么Maven + Spring Boot创建巨大的jar文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆