【发布时间】:2023-03-24 21:05:01
【问题描述】:
我有以下 Maven 项目结构:
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 次,因此是一个大文件。为什么所有的 jars 都需要是 spring boot 应用程序/jars?只有实际可运行的应用程序应该是 Spring Boot 应用程序,所有其他应用程序都可以简单地使用启动器进行依赖管理并创建普通的 jar 文件。只需从父级中删除
spring-boot-maven-plugin并将其仅放在应创建可执行 jar 的项目中。 -
我不知道弹簧靴。但是由于没有其他人回答,而且我在使用 maven 方面有很多实践,我会告诉你一个想法。您将 spring boot 依赖项添加到父 pom.xml 中。这意味着它们被添加到配置此父级的每个 POM 中。所以这些“罐子”被添加到每个模块中。我不知道 spring boot 插件是如何工作的,但它是可能的,它包括每个模块!以及每个模块的依赖关系!!到主罐子。如果我正确地将依赖项和构建配置添加到主 pom 并将其从父 pom 中删除会使你的主 jar 更小。
-
为什么我的问题被标记为重复?引用的线程是关于另一个问题。
标签: java spring maven spring-boot