【问题标题】:Why does Maven + Spring Boot create huge jar-files?为什么 Maven + Spring Boot 会创建巨大的 jar 文件?
【发布时间】:2023-03-24 21:05:01
【问题描述】:

我有以下 Maven 项目结构:

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

还有以下简化的 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>

ma​​in_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 文件被多次组装到文件中。我怎样才能避免这种情况?

【问题讨论】:

  • 每个项目都是一个启动项目,这意味着您的所有项目都会创建可运行的 jar,其中包括依赖项。所以你基本上在你的 jar 中获得所有依赖项 5 次,因此是一个大文件。为什么所有的 jars 都需要是 spring boot 应用程序/jars?只有实际可运行的应用程序应该是 Spring Boot 应用程序,所有其他应用程序都可以简单地使用启动器进行依赖管理并创建普通的 jar 文件。只需从父级中删除 spring-boot-maven-plugin 并将其仅放在应创建可执行 jar 的项目中。
  • 我不知道弹簧靴。但是由于没有其他人回答,而且我在使用 maven 方面有很多实践,我会告诉你一个想法。您将 spring boot 依赖项添加到父 pom.xml 中。这意味着它们被添加到配置此父级的每个 POM 中。所以这些“罐子”被添加到每个模块中。我不知道 spring boot 插件是如何工作的,但它是可能的,它包括每个模块!以及每个模块的依赖关系!!到主罐子。如果我正确地将依赖项和构建配置添加到主 pom 并将其从父 pom 中删除会使你的主 jar 更小。
  • 为什么我的问题被标记为重复?引用的线程是关于另一个问题。

标签: java spring maven spring-boot


【解决方案1】:

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

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

module 1module 2 包含所有父依赖项、本地声明的依赖项以及 domain_models_and_repository 项目表达的所有依赖项以及模块本身。

模块 3 包含所有父依赖项、它自己的本地声明的依赖项以及来自 domain_models_and_repository模块 1模块 2 以及这些模块本身。

主应用程序包含其自己的本地声明的依赖项的父依赖项以及来自 domain_models_and_repositorymodule 1 的所有其他尚不可用的依赖项>模块 2 以及那些模块本身。

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

【讨论】:

  • 对于 12 个微服务,我的每个服务从 360 MB 减少到 64 KB。非常感谢朋友。
猜你喜欢
  • 1970-01-01
  • 2018-09-10
  • 2017-03-20
  • 2018-02-23
  • 1970-01-01
  • 2016-05-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-18
相关资源
最近更新 更多