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
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> |
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/
'오래된글 > Articles' 카테고리의 다른 글
시스템 부팅시 도커 컨테이너 자동 실행 (0) | 2018.05.03 |
---|---|
First WebSocket (0) | 2018.05.03 |
Android Service에서 AlertDialog 띄우기 (0) | 2018.05.03 |
Android 촬영 사진의 회전 각도 구하기 (0) | 2018.05.03 |
모바일 보안 취약점 및 대책 (0) | 2018.05.03 |