This document describes how to re-package a Java 8 application as a JAR file to run on a supported Java runtime. To use a supported Java runtime, you can either embed a server like Jetty or containerize your application with Docker for a custom runtime without completely rewriting your application. You can run your existing WAR applications on modern Java platforms or flexible cloud environments. Choose from the following methods that best suits your deployment strategy and infrastructure:
Prepare your Java 8 web application (WAR file)
Before you re-package your Java 8 application as a supported JAR file, you must
build a WAR file. This section provides a sample Java 8 application that builds
a WAR file. Follow the instructions to create a Java 8 hello-world
application:
Create a
HelloServlet.java
file in your source directory:Create a
web.xml
deployment descriptor file to configure your web application:Create a landing page
index.jsp
:Add the following code in the
pom.xml
file to define the build for your Java 8 application:WAR packing configuration:
<groupId>com.example</groupId> <artifactId>HelloWorldApp</artifactId> <version>1.0</version> <packaging>war</packaging>
maven-war-plugin
plugin with themaven.compiler
source and target set to version1.8
:<properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <java.version>8</java.version> </properties>
javax.servlet-api
dependency:<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> </dependencies>
Maven configuration:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.2</version> </plugin> </plugins> </build>
Your project directory should be similar to the following structure:
├── pom.xml └── src └── main ├── java │ └── com │ └── example │ └── HelloServlet.java └── webapp ├── WEB-INF │ └── web.xml └── index.jsp
Run
mvn install
in your application's project directory to generate the WAR fileHelloWorldApp-1.0.war
in the target directory.
Use Dockerfiles to deploy your application (recommended)
Custom runtimes are suitable for platforms supporting custom containers, such as App Engine custom runtimes. Custom runtimes provide flexibility by allowing you to configure the runtime environment. For an example walkthrough of deploying custom runtimes, see Create a custom runtime app in the App Engine flexible environment.
The following instructions describe how to containerize your Java 8 application using a Dockerfile:
- Prepare your Java 8 web application (WAR file)
- Build the container image and push it to Artifact Registry
- Deploy your application
Build the container image and push it to Artifact Registry
This section describes how to build a Docker image using Cloud Build and push it to an Artifact Registry repository. Follow these steps to create a container image of your application:
Create a
cloudbuild.yaml
file in your source directory to build the Docker image and push to Artifact Registry:steps: # Step 1: Build the Docker image - name: "gcr.io/cloud-builders/docker" args: - "build" - "-t" - "$LOCATION-docker.pkg.dev/$PROJECT/$REPOSITORY/SERVICE:VERSION" - "." # Step 2: Push the Docker image to Artifact Registry - name: "gcr.io/cloud-builders/docker" args: - "push" - "$LOCATION-docker.pkg.dev/$PROJECT/$REPOSITORY/SERVICE:VERSION" images: - "$LOCATION-docker.pkg.dev/$PROJECT/$REPOSITORY/SERVICE:VERSION"
Replace:
- LOCATION with the Google Cloud region where you deploy your app.
- PROJECT with your Google Cloud project ID.
- REPOSITORY with the name of your Artifact Registry repository.
- IMAGE with your container image URL.
- TAG with your container image tag.
Create a Dockerfile with the following configuration:
Download and install Docker to test your sample app, and run the Hello World container on your local machine.
Build the container image and push it to Artifact Registry:
gcloud builds submit .
Deploy your application
To deploy your App Engine application:
Configure your
app.yaml
file to use a custom runtime in the source directory:Your project directory should be similar to the following structure:
├── Dockerfile ├── README.md ├── app.yaml ├── cloudbuild.yaml ├── pom.xml └── src └── main ├── java │ └── com │ └── example │ └── HelloServlet.java └── webapp ├── WEB-INF │ └── web.xml └── index.jsp
Deploy your application using the
gcloud app deploy
command:gcloud app deploy --image-url=REGION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE:tag
Replace:
- LOCATION with the Google Cloud region where you deploy your app.
- PROJECT with your Google Cloud project ID.
- REPOSITORY with the name of your Artifact Registry repository.
- IMAGE with your container image URL.
- TAG with your container image tag.
Use an embedded Java runtime
The following instructions demonstrate how to re-package an App Engine Java 8 application with an embedded server (Jetty) to run as a standalone JAR on a supported Java runtime:
- Create an embedded Jetty server
- Prepare your Java 8 web application (WAR file)
- Run the WAR file with embedded Jetty and deploy your application
Create an Embedded Jetty server
To bundle your application WAR file with an embedded Jetty server, follow these steps:
Create a
Main
class to initialize and configure the Jetty server to run your WAR file. TheMain
class sets up the server port that defaults to8080
. You can also modify the source code to use a port specified in thePORT
environment variable. TheMain
class configures theWebAppContext
handler to serve your WAR file:Create the Maven project file
pom.xml
and add the following configuration:Set the
maven.compiler.source
andmaven.compiler.target
properties to a supported Java runtime:Add dependencies for Jetty:
Configure the
maven-assembly-plugin
property to package dependencies:
Your project directory should be similar to the following structure:
├─src │ └─main │ └─java │ └─jetty │ └─Main.java └─pom.xml
Run the
mvn install
command in the Jetty runner project directory. This generates thejetty-jar-with-dependencies.jar
in your target directory.Follow the instructions in the Prepare your Java 8 web application (WAR file) section to create a WAR file.
Run the WAR file with embedded Jetty and deploy your application
This section provides steps for packaging your application into an executable JAR file. Follow these instructions to package and deploy your application:
Place the generated Jetty runner JAR
jetty-jar-with-dependencies.jar
and your application WAR fileHelloWorldApp-1.0.war
in the same directory.Run the application using a supported Java runtime:
java -jar jetty-jar-with-dependencies.jar HelloWorldApp-1.0.war
- In your web browser, navigate to http://localhost:8080. You should see your application's welcome page.
Create an
entrypoint
element in yourapp.yaml
file to call thejetty-jar-with-dependencies
file, and pass your WAR file as an argument. The version you specify in the WAR file must be the same version as thepom.xml
file:Deploy your application using the
gcloud app deploy
command.