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

时间:2015-06-24 12:31:54

标签: java spring maven spring-boot

我有以下Maven项目结构:

select 
  id,
  date,
  (
    select category 
    from mytable x
    where x.id = m.id
    and not exists
    (
      select * 
      from mytable older
      where older.id = x.id 
      and older.date < x.date
    )
  ) as oldest_category
from mytable m;

以下简化的POMS:

parent_project.pom

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

main_application

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

module_1

<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_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>
    </dependencies>
</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> </dependencies> </project> 时 我得到一个主应用程序的1.2GB文件。我注意到所有模块的所有依赖关系都已组装完毕 进入模块。因此,许多jar文件被多次组装到文件中。我怎么能避免这个?

1 个答案:

答案 0 :(得分:12)

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

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

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

模块3 包含所有父依赖项,它自己的本地声明的依赖项以及来自 domain_models_and_repository 模块1 的所有其他尚未提供的依赖项,以及模块2 以及那些模块本身。

主应用程序包含其自身本地声明的依赖项的父依赖项以及来自 domain_models_and_repository 模块1 模块2 以及那些模块本身。

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