30

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.

niels
  • 7,321
  • 2
  • 38
  • 58
  • 1
    The fact that the jars are still there doesn't mean you cannot deploy it as a war. You can perfectly deploy it as is. Make sure that you exclude tomcat from the `spring-boot-starter-web` dependency. – M. Deinum Sep 23 '14 at 09:45
  • 1
    @niels, you edited this question, and since revision *2* it includes the answer to your question. What about reverting to your initial answer and providing a separate answer? – Abdull Oct 26 '14 at 23:48
  • @Abdull good idea. It makes it more clear. – niels Oct 27 '14 at 13:32

5 Answers5

35

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)

Razzle
  • 479
  • 4
  • 11
niels
  • 7,321
  • 2
  • 38
  • 58
  • 1
    But if the spring-boot-starter-tomcat is commented out, how do you run the app? I tried it this way, and deployed it to a tomcat container, but got error messages then. All similar to: org.apache.catalina.startup.ContextConfig checkHandlesTypes WARNING: Unable to load class [org.apache.tomcat.util.descriptor.web.NameRule] to check against the @HandlesTypes annotation of one or more ServletContentInitializers. java.lang.ClassNotFoundException: org.apache.tomcat.util.descriptor.web.NameRule etc. – Mathias Conradt May 09 '15 at 17:06
  • You can deploy the war in an arbitrary servlet-container. If your application doesn't need tomcat classes it works fine this way. Is it possible that you need at compile-time the tomcat-classes? How do you compile? mvn from commandline? Make sure you make a mvn clean at the beginning. – niels May 11 '15 at 06:29
  • I use gradle, I already figured it out though. The build process (bootRepackage) produces two artifacts, one with and one without the embedded Tomcat container. Turned out I took the wrong artifact for the external Tomcat deployment - should take the war without the embedded Tomcat then. – Mathias Conradt May 11 '15 at 06:31
  • SUPERB! Thank you very much. – tm1701 Sep 16 '17 at 08:58
  • When i run my application with above suggestion, it gives me error "Unregistering JMX-exposed beans on shutdown" – Tatkal Apr 11 '18 at 09:28
  • @Tatkal I Think it's difficult, to give you help with so less information. I can only guess that you have configured JMX at the logback config. I would recommend to open a new question with detailed information. Have you checked twice that the error only happens if you make the adjustments in pom.xml? – niels Apr 12 '18 at 17:50
13

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

Narasimha
  • 1,537
  • 1
  • 16
  • 17
Yonatan
  • 2,543
  • 2
  • 19
  • 20
5

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.

Lopestt
  • 77
  • 1
  • 8
5

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>
Tadele Ayelegn
  • 4,126
  • 1
  • 35
  • 30
2

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.

zappee
  • 20,148
  • 14
  • 73
  • 129