Enterprise Java

Standalone web application with executable Tomcat

When it comes to deploying your application, simplicity is the biggest advantage. You’ll understand that especially when project evolves and needs some changes in the environment. Packaging up your whole application in one, standalone and self-sufficient JAR seems like a good idea, especially compared to installing and upgrading Tomcat in target environment. In the past I would typically include Tomcat JARs in my web application and write thin command-line runner using Tomcat API. Luckily there is a tomcat7:exec-war maven goal that does just that. It takes your WAR artifact and packages it together with all Tomcat dependencies. At the end it also includes Tomcat7RunnerCli Main-class to manifest.

Curious to try it? Take your existing WAR project and add the following to your pom.xml:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.0</version>
    <executions>
        <execution>
            <id>tomcat-run</id>
            <goals>
                <goal>exec-war-only</goal>
            </goals>
            <phase>package</phase>
            <configuration>
                <path>/standalone</path>
                <enableNaming>false</enableNaming>
                <finalName>standalone.jar</finalName>
                <charset>utf-8</charset>
            </configuration>
        </execution>
    </executions>
</plugin>

Run mvn package and few seconds later you’ll find shiny standalone.jar in your target directory. Running your web application was never that simple:

1
$ java -jar target/standalone.jar

…and you can browse localhost:8080/standalone. Although the documentation of path parameter says (emphasis mine):

The webapp context path to use for the web application being run. The name to store webapp in exec jar. Do not use / just between the two of us, <path>/</path> seems to work after all. It turns out that built in main class is actually a little bit more flexible. For example you can say (I hope it’s self-explanatory):

1
$ java -jar standalone.jar -httpPort=7070

What this runnable JAR does is it first unpacks WAR file inside of it to some directory (.extract by default1) and deploys it to Tomcat – all required Tomcat JARs are also included. Empty standalone.jar (with few KiB WAR inside) weights around 8.5 MiB – not that much if you claim that pushing whole Tomcat with every release alongside your application is wasteful.

Talking about Tomcat JARs, you should wonder how to choose Tomcat version included in this runnable? Unfortunately I couldn’t find any simple option, so we must fall back to explicitly redefining plugin dependencies (version 2.0 has hardcoded 7.0.30 Tomcat). It’s quite boring, but not that complicated and might be useful for future reference:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <tomcat7Version>7.0.33</tomcat7Version>
</properties>
 
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.0</version>
            <executions>
                <execution>
                    <id>tomcat-run</id>
                    <goals>
                        <goal>exec-war-only</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <path>/standalone</path>
                        <enableNaming>false</enableNaming>
                        <finalName>standalone.jar</finalName>
                        <charset>utf-8</charset>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                  <groupId>org.apache.tomcat.embed</groupId>
                  <artifactId>tomcat-embed-core</artifactId>
                  <version>${tomcat7Version}</version>
                </dependency>
                <dependency>
                  <groupId>org.apache.tomcat</groupId>
                  <artifactId>tomcat-util</artifactId>
                  <version>${tomcat7Version}</version>
                </dependency>
                <dependency>
                  <groupId>org.apache.tomcat</groupId>
                  <artifactId>tomcat-coyote</artifactId>
                  <version>${tomcat7Version}</version>
                </dependency>
                <dependency>
                  <groupId>org.apache.tomcat</groupId>
                  <artifactId>tomcat-api</artifactId>
                  <version>${tomcat7Version}</version>
                </dependency>
                <dependency>
                  <groupId>org.apache.tomcat</groupId>
                  <artifactId>tomcat-jdbc</artifactId>
                  <version>${tomcat7Version}</version>
                </dependency>
                <dependency>
                  <groupId>org.apache.tomcat</groupId>
                  <artifactId>tomcat-dbcp</artifactId>
                  <version>${tomcat7Version}</version>
                </dependency>
                <dependency>
                  <groupId>org.apache.tomcat</groupId>
                  <artifactId>tomcat-servlet-api</artifactId>
                  <version>${tomcat7Version}</version>
                </dependency>
                <dependency>
                  <groupId>org.apache.tomcat</groupId>
                  <artifactId>tomcat-jsp-api</artifactId>
                  <version>${tomcat7Version}</version>
                </dependency>
                <dependency>
                  <groupId>org.apache.tomcat</groupId>
                  <artifactId>tomcat-jasper</artifactId>
                  <version>${tomcat7Version}</version>
                </dependency>
                <dependency>
                  <groupId>org.apache.tomcat</groupId>
                  <artifactId>tomcat-jasper-el</artifactId>
                  <version>${tomcat7Version}</version>
                </dependency>
                <dependency>
                  <groupId>org.apache.tomcat</groupId>
                  <artifactId>tomcat-el-api</artifactId>
                  <version>${tomcat7Version}</version>
                </dependency>
                <dependency>
                  <groupId>org.apache.tomcat</groupId>
                  <artifactId>tomcat-catalina</artifactId>
                  <version>${tomcat7Version}</version>
                </dependency>
                <dependency>
                  <groupId>org.apache.tomcat</groupId>
                  <artifactId>tomcat-tribes</artifactId>
                  <version>${tomcat7Version}</version>
                </dependency>
                <dependency>
                  <groupId>org.apache.tomcat</groupId>
                  <artifactId>tomcat-catalina-ha</artifactId>
                  <version>${tomcat7Version}</version>
                </dependency>
                <dependency>
                  <groupId>org.apache.tomcat</groupId>
                  <artifactId>tomcat-annotations-api</artifactId>
                  <version>${tomcat7Version}</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

In the next article we will learn how to tame these pesky Tomcat internal logs appearing in the terminal (java.util.logging…) In the meantime I discovered and reported MTOMCAT-186 Closing executable JAR does not call ServletContextListener.contextDestroyed() – have look if this is a deal breaker for you.

1 – it might be a good idea to specify different directory using –extractDirectory and clean it before every restart with –resetExtract.
 

Reference: Standalone web application with executable Tomcat from our JCG partner Tomasz Nurkiewicz at the Java and neighbourhood blog.

Tomasz Nurkiewicz

Java EE developer, Scala enthusiast. Enjoying data analysis and visualization. Strongly believes in the power of testing and automation.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Matías Ocampo
11 years ago

Hi, I follow your steps but I received following error:

mac79:kbappsdev2 timba$ java -jar target/standalone.jar

Exception in thread “main” java.lang.NullPointerException

at java.util.StringTokenizer.(StringTokenizer.java:182)

at java.util.StringTokenizer.(StringTokenizer.java:204)

at org.apache.tomcat.maven.runner.Tomcat7Runner.populateWebAppWarPerContext(Tomcat7Runner.java:528)

at org.apache.tomcat.maven.runner.Tomcat7Runner.extract(Tomcat7Runner.java:444)

at org.apache.tomcat.maven.runner.Tomcat7Runner.run(Tomcat7Runner.java:158)

at org.apache.tomcat.maven.runner.Tomcat7RunnerCli.main(Tomcat7RunnerCli.java:204)

Thanks

Back to top button