【问题标题】:Lightweight deployable WAR for a Spring Boot Application用于 Spring Boot 应用程序的轻量级可部署 WAR
【发布时间】:2015-04-18 08:08:51
【问题描述】:

一个简单的 Spring Boot 应用程序在打包为可部署的 WAR 时仅打印“Hello World”,大小为 11.5 MB。当我分解 WAR 时,web-inf/lib 文件夹是库太多的根本原因。

  1. 如何仅使用最少的必要/依赖库来创建 WAR?
  2. 在提到 provided 作为 spring-boot-starter-tomcat 的范围时,嵌入式 Tomcat 服务器是否仍然与 WAR 一起打包?

我在玩 Spring 测试套件 (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兆。

目前只有2个直接依赖spring-boot-starter-web & spring-boot-starter-test (我知道这可能反过来有其他依赖项)。

我的问题是,即使我不打算使用嵌入式 Tomcat 容器,“Spring Boot”应用程序会产生如此巨大的 可部署的 WAR ?我试图确认是否有任何事情 可以做一个优化的(我的意思是小型的)可部署的 WAR。

【问题讨论】:

    标签: spring maven spring-boot


    【解决方案1】:

    如果您对将应用程序作为可执行的 war 文件运行不感兴趣并且只想将其部署到 servlet 容器,那么您可以删除 Spring Boot 的 Maven 插件并将 spring-boot-starter-tomcat 声明为提供的依赖项:

    <?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>com.example</groupId>
        <artifactId>example-app</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.2.1.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </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>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>
    

    删除 Spring Boot 的 Maven 插件将阻止提供的依赖项打包到您的 war 文件中。使 spring-boot-starter-tomcat 成为提供的依赖项,而不是完全排除它,将允许您使用嵌入式 Tomcat 运行集成测试。

    这将为您提供一个 7.6MB 的战争文件。根据您的应用程序和您要使用的技术,您可能还需要考虑排除一些spring-boot-starter-web 的传递依赖项。例如,它引入了 Hibernate Validator (> 0.5MB) 和 Jackson (> 1MB)。

    【讨论】:

    • 那太好了……尽管我不想马上这样做,但我很想知道周围遵循的最佳实践。甚至在不想让我的 WAR 文件不必要地膨胀之前,我就想了解真正发生了什么以及还能做些什么。谢谢
    【解决方案2】:

    目的是创建一个包含所有依赖项的war/jar。您要部署的最低限度是多少?

    我建议首先删除 tomcat 和执行器依赖项 - 然后重新检查大小

    【讨论】:

    • 我从 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。
    • 只需阅读此链接 - spring.io/guides/gs/convert-jar-to-war - 您必须按照提供的方式声明嵌入式首选项以删除 tomcat
    • 是的,你可以注意到我粘贴的 pom 内容。它在我的 POM 中被声明为“提供”
    • 哦,我明白了 - 我读到你可以使用 maven 的阴影插件。这可能有助于减小尺寸
    • 以防万一供我参考,这里有一个很好的解释maven shade插件stackoverflow.com/questions/13620281/…
    猜你喜欢
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    • 2020-11-09
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 2020-03-05
    相关资源
    最近更新 更多