2015-06-24 50 views
1

我有以下Maven项目结构:为什么Maven + Spring Boot会创建巨大的jar文件?

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> 

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

+4

每个项目都是一个启动项目,这意味着您的所有项目都会创建可运行的jar包,其中包含依赖关系。所以你基本上在你的jar中有5次依赖关系,因此是一个大文件。为什么所有的罐子都需要成为弹簧启动应用/罐子?只有实际可运行的应用程序应该是Spring启动应用程序,其他所有应用程序都可以简单地使用启动器进行依赖关系管理并创建普通的jar文件。只需从父项中移除'spring-boot-maven-plugin',并将其仅放入应创建可执行jar的项目中。 –

+1

我不知道春季引导。但是,由于没有人回答,我在使用maven方面有很多练习,我会告诉你一个想法。您将弹簧引导依赖关系添加到父pom中。这意味着它们被添加到每个配置此父级的POM中。所以这些“罐子”被添加到每个模块。我不知道spring引导插件是如何工作的,但它可能包含每个模块!和每个模块的依赖关系!到主罐子。如果我正确地将依赖关系和构建配置添加到主pom,并将其从父pom中删除,则会使主jar更小。 –

+0

为什么我的问题被标记为重复?引用的线程是关于另一个问题。 – stevecross

回答

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文件。

相关问题