Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring boot war without tomcat embedded

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

...
<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>
    </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>
...

How ever if I run mvn package I get a war, where the tomcat*.jar are in a provided-lib folder but still in the lib-folder. I read build-tool-plugins-maven-packaging, but can't find what's wrong.

I know a main idea is to run it as an application, how ever our customer want's to deploy it on his application-server.

like image 747
niels Avatar asked Sep 23 '14 09:09

niels


People also ask

How do we exclude the embedded tomcat server from spring boot application?

If we want to exclude tomcat from spring boot, we don't need to do much, we just need to add one additional block(<exclusions>) to the Spring Boot dependency. <exclusions> tag is used to make us sure that given server/artifactId is being removed at the time of build.

Is tomcat required for spring boot?

Spring Boot has a complete Tomcat inside. It builds a so-called fat-jar with everything needed inside. You don't need Tomcat installed in your system.

Does spring boot comes with embedded tomcat?

By default, Spring Boot provides an embedded Apache Tomcat build. By default, Spring Boot configures everything for you in a way that's most natural from development to production in today's platforms, as well as in the leading platforms-as-a-service.

How do I disable embedded server in spring boot?

Using Dependencies The easiest way to prevent a Spring Boot application from starting an embedded web server is to not include the web server starter in our dependencies. This means not including the spring-boot-starter-web dependency in either the Maven POM or Gradle build file.


Video Answer


3 Answers

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)

like image 90
niels Avatar answered Oct 09 '22 06:10

niels


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

like image 39
Yonatan Avatar answered Oct 09 '22 07:10

Yonatan


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.

like image 5
Lopestt Avatar answered Oct 09 '22 08:10

Lopestt