我有以下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文件被多次組裝到文件中。我怎樣才能避免這種情況?
每個項目都是一個啓動項目,這意味着您的所有項目都會創建可運行的jar包,其中包含依賴關係。所以你基本上在你的jar中有5次依賴關係,因此是一個大文件。爲什麼所有的罐子都需要成爲彈簧啓動應用/罐子?只有實際可運行的應用程序應該是Spring啓動應用程序,其他所有應用程序都可以簡單地使用啓動器進行依賴關係管理並創建普通的jar文件。只需從父項中移除'spring-boot-maven-plugin',並將其僅放入應創建可執行jar的項目中。 –
我不知道春季引導。但是,由於沒有人回答,我在使用maven方面有很多練習,我會告訴你一個想法。您將彈簧引導依賴關係添加到父pom中。這意味着它們被添加到每個配置此父級的POM中。所以這些「罐子」被添加到每個模塊。我不知道spring引導插件是如何工作的,但它可能包含每個模塊!和每個模塊的依賴關係!到主罐子。如果我正確地將依賴關係和構建配置添加到主pom,並將其從父pom中刪除,則會使主jar更小。 –
爲什麼我的問題被標記爲重複?引用的線程是關於另一個問題。 – stevecross