DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • Secrets Management With Datadog Secret Backend Utility
  • Using Event-Driven Ansible to Monitor Your Web Application
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever

Trending

  • How to Submit a Post to DZone
  • DZone's Article Submission Guidelines
  • Enforcing Architecture With ArchUnit in Java
  • MCP Servers: The Technical Debt That Is Coming
  1. DZone
  2. Coding
  3. Languages
  4. Standalone Web Application with Executable Tomcat

Standalone Web Application with Executable Tomcat

By 
Tomasz Nurkiewicz user avatar
Tomasz Nurkiewicz
DZone Core CORE ·
Dec. 03, 12 · Interview
Likes (0)
Comment
Save
Tweet
Share
22.6K Views

Join the DZone community and get the full member experience.

Join For Free

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:

<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:

$ 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):
$ 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:
<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.





Web application Apache Tomcat Executable

Published at DZone with permission of Tomasz Nurkiewicz, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • Secrets Management With Datadog Secret Backend Utility
  • Using Event-Driven Ansible to Monitor Your Web Application
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!