一个简单的Spring Boot应用程序在打包为可部署的WAR时打印出“Hello World”,为11.5 MB . 当我爆炸WAR时,web-inf / lib文件夹是有太多库的根本原因 .
如何仅使用最少的必需/依赖库来创建WAR?虽然提到spring-boot-starter-tomcat的范围,但是嵌入式Tomcat Server是否仍然与WAR一起打包?
我正在玩Spring Test Suite(STS)附带的'gs-spring-boot-complete'项目,这是它附带的默认POM .
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-spring-boot</artifactId>
<version>0.1.0</version>
<!-- ... -->
<packaging>war</packaging>
<!-- ... -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.10.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- tag::actuator[] -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- end::actuator[] -->
<!-- tag::tests[] -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- end::tests[] -->
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
更新:我从POM中删除了对 spring-boot-starter-tomcat 和 spring-boot-starter-actuator 的依赖关系,但WAR仍为11.5 MB .
目前只有2个直接依赖项 spring-boot-starter-web & spring-boot-starter-test (我知道这可能反过来又有其他依赖项) .
我的问题是,即使我不打算使用嵌入式Tomcat容器,“Spring Boot”应用程序是否会生成如此庞大的可部署WAR?我试图确认是否有任何可以做的事情来进行优化(我的意思是小型)可部署的WAR .
2 回答
如果您没有兴趣将应用程序作为可执行war文件运行,并且只想将其部署到servlet容器,那么您可以删除Spring Boot的Maven插件并声明
spring-boot-starter-tomcat
作为提供的依赖项:删除Spring Boot的Maven插件将阻止在war文件中打包提供的依赖项 . 使
spring-boot-starter-tomcat
成为提供的依赖项,而不是完全排除它,将允许您使用嵌入式Tomcat运行集成测试 .这将给你一个7.6MB的战争文件 . 根据您的应用程序和您要使用的技术,您可能还需要考虑排除一些
spring-boot-starter-web
's transitive dependencies. For example, it' s拉入Hibernate Validator(> 0.5MB)和Jackson(> 1MB) .目的是创建一个包含所有依赖项的war / jar . 您要部署的最低要求是多少?
我建议删除tomcat和 Actuator 依赖关系一开始 - 然后重新检查大小