我有以下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