Akashic Records

How to embed Tomcat and Java web apps in an executable JAR 본문

오래된글/Articles

How to embed Tomcat and Java web apps in an executable JAR

Andrew's Akashic Records 2018. 5. 29. 13:11
728x90

Historically, we packaged production-ready Java web apps as WAR files. A servlet engine, like an IBM WebSphere or Apache Tomcat server, would host multiple applications at once. But for a variety of reasons -- the widespread adoption of microservices and desire to perform highly isolated unit tests, for instance -- the one-to-many relationship between the Java web app and the servlet container has consolidated into a one-to-one.


Embedded Tomcat example prerequsites

This tutorial has only two prerequisites:


You must have the Java Development Kit (JDK) installed and configured. Version 8 of the JDK or newer is preferred.

You must have Maven 3.5 or newer installed.

That's it. You don't even need to download the Apache Tomcat server because Maven's Tomcat plug-in will acquire the Tomcat binaries for you.


Create a new Java web app

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp



When the Maven build completes, we will edit a sample index.jsp file, which you'll find in the following directory:

C:\ROOT\java-web-app\src\main\webapp


Develop Java web apps
Edit the index.jsp file so, when the Java web app runs, we can uniquely identify it as our own. I have updated the provided index.jsp file to read as follows:


Use Maven to embed Tomcat

With the pom.xml file edited and updated with a reference to the Tomcat plug-in for Maven, the final step is to build the project and call on the appropriate goal -- tomcat7:exec-war-only -- to create the executable JAR file. When this Maven goal is invoked, the Tomcat plug-in for Maven will automatically place the embedded Tomcat server inside the executable JAR.


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>kr.co.keytech.test</groupId>

  <artifactId>java-web-app</artifactId>

  <packaging>war</packaging>

  <version>1.0-SNAPSHOT</version>

  <name>java-web-app Maven Webapp</name>

  <url>http://maven.apache.org</url>

  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>4.11</version>

      <scope>test</scope>

    </dependency>

  </dependencies>

<build>

   <!-- plugin to embed Tomcat in a Java JAR file -->

    <finalName>tutorial</finalName>

    <pluginManagement>

      <plugins>

          <plugin>

             <groupId>org.apache.tomcat.maven</groupId>

             <artifactId>tomcat7-maven-plugin</artifactId>

             <version>2.1</version>


             <configuration>

               <!-- context root for Java web apps -->

<path>/tutorial</path> 

<port>8080</port>

<ajpPort>8009</ajpPort>

<httpsPort>8443</httpsPort>

<contextReloadable>true</contextReloadable>

               <!-- name of executable jar file -->

               <finalName>executable.jar</finalName>

             </configuration>

<!--

<configuration>

<path>/</path> 

<port>18080</port>

<ajpPort>18009</ajpPort>

<httpsPort>18443</httpsPort>

<contextReloadable>true</contextReloadable>

<keystoreFile>${user.home}/.keystore</keystoreFile>

<keystorePass>changeit</keystorePass>

</configuration>

-->

          </plugin>

       </plugins>

    </pluginManagement>

 </build>

</project>



$ mvn clean install tomcat7:exec-war-only


How to run an executable JAR file

To run the executable JAR file, issue the following command:

java -jar executable.jar


http://localhost:8080/tutorial/



728x90
Comments