2015-06-24 52 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文件。

相關問題