spring boot war without tomcat embedded

后端 未结 5 1614
孤街浪徒
孤街浪徒 2020-11-28 10:23

I want to create a war file without embedded tomcat with maven. Here the relevant part of my pom

...

    org.springframework.bo         


        
相关标签:
5条回答
  • 2020-11-28 11:03

    I'm not sure if that's the spring-boot way of doing it, but you can exclude the tomcat jars using the maven-war-plugin configuration. That is, add the following to your pom.xml:

    <build>
        <plugins>
            ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <packagingExcludes>WEB-INF/lib/tomcat-*.jar</packagingExcludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

    Using this approach, the war generated is not executable (cannot be run on command line using java -jar ) but can only be deployed to any servlet container

    0 讨论(0)
  • 2020-11-28 11:05

    Following the Hint from M. Deinum I excluded the tomcat-depedency.

    With the following pom.xml (relevant snippet) a maven clean package has the result I want to get.

    ...
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.6.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!-- Add tomcat only if I want to run directly -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    ...
    

    Warning for idea-user: You must activate "Include dependencies with the provided scope" in the run-configuration (see Unable to start spring-boot application in IntelliJ Idea for more information)

    0 讨论(0)
  • 2020-11-28 11:07

    I think that the easiest way to build a final WAR from your existing Spring Boot project without embedded Tomcat is the following:

    1) Set WARpackaging for your artifact: <packaging>war</packaging>

    2) Set the Tomcat Server dependency to provide:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    

    If some of your existing Spring Boot dependencies contain it by default, just exclude it. Example:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
    </dependency>
    

    That is it.

    0 讨论(0)
  • 2020-11-28 11:09

    I had this same need but removing the mentioned dependency didn't worked. I was able to get the WAR file by adding this <packaging>war</packaging> dependency to my pom file.

    I used this Spring article as a guide... sharing so this may help other people as well.

    0 讨论(0)
  • 2020-11-28 11:21

    Changing spring-boot-starter dependency from

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    

    To this one will exclude the embedded tomcat server.

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    
    0 讨论(0)
提交回复
热议问题