Memaketkan ulang file WAR ke dalam file JAR

Jika bermigrasi ke versi Java terbaru yang didukung dan aplikasi Anda tidak menggunakan layanan paket lama, Anda harus memaketkan ulang aplikasi web App Engine Anda menjadi file JAR yang dapat dieksekusi.

Aplikasi Anda harus memiliki class Main yang memulai server web yang merespons permintaan HTTP di port 8080, yang mungkin ditentukan oleh variabel lingkungan PORT.

Contoh:

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.Configuration.ClassList;
import org.eclipse.jetty.webapp.WebAppContext;

/** Simple Jetty Main that can execute a WAR file when passed as an argument. */
public class Main {

  public static void main(String[] args) throws Exception {
    if (args.length != 1) {
      System.err.println("Usage: need a relative path to the war file to execute");
      System.exit(1);
    }
    System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StrErrLog");
    System.setProperty("org.eclipse.jetty.LEVEL", "INFO");

    // Create a basic Jetty server object that will listen on port defined by
    // the PORT environment variable when present, otherwise on 8080.
    int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"));
    Server server = new Server(port);

    // The WebAppContext is the interface to provide configuration for a web
    // application. In this example, the context path is being set to "/" so
    // it is suitable for serving root context requests.
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    webapp.setWar(args[0]);
    ClassList classlist = ClassList.setServerDefault(server);

    // Enable Annotation Scanning.
    classlist.addBefore(
        "org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
        "org.eclipse.jetty.annotations.AnnotationConfiguration");

    // Set the the WebAppContext as the ContextHandler for the server.
    server.setHandler(webapp);

    // Start the server! By using the server.join() the server thread will
    // join with the current thread. See
    // "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()"
    // for more details.
    server.start();
    server.join();
  }
}

Contoh migrasi WAR (Java 21)

Petunjuk berikut menunjukkan cara memaketkan ulang aplikasi WAR hello-world App Engine sebagai JAR untuk dijalankan di runtime Java 21.

Migrasi menggunakan artefak appengine-simple-jetty-main. Tindakan ini akan menyediakan class Main dengan server web Jetty sederhana yang memuat file WAR dan memaketkan aplikasi Anda ke dalam file JAR yang dapat dieksekusi:

  1. Clone artefak Jetty Server Tersemat ke komputer lokal Anda:

    git clone https://github.com/GoogleCloudPlatform/java-docs-samples
    

    Atau, Anda dapat mendownload contoh sebagai file ZIP dan mengekstraknya.

  2. Ubah ke direktori yang berisi kode contoh:

    cd java-docs-samples/flexible/repacking-legacy-applications/appengine-simple-jetty-main/
    
  3. Bangun JAR peluncur Jetty:

    mvn install
    

    Tindakan ini akan menghasilkan jetty-jar-with-dependencies.jar di direktori target/ Anda.

  4. Konfigurasi file build pom.xml untuk server Jetty:

    • Properti compiler (Java 21):
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <jetty.version>9.4.57.v20241219</jetty.version>
      </properties>
    • Dependensi Jetty tersemat:
      <dependencies>
        <!-- Embedded Jetty dependencies -->
        <dependency>
          <groupId>org.eclipse.jetty</groupId>
          <artifactId>jetty-server</artifactId>
          <version>${jetty.version}</version>
        </dependency>
        <dependency>
          <groupId>org.eclipse.jetty</groupId>
          <artifactId>jetty-webapp</artifactId>
          <version>${jetty.version}</version>
          <type>jar</type>
        </dependency>
        <dependency>
          <groupId>org.eclipse.jetty</groupId>
          <artifactId>jetty-util</artifactId>
          <version>${jetty.version}</version>
        </dependency>
        <dependency>
          <groupId>org.eclipse.jetty</groupId>
          <artifactId>jetty-annotations</artifactId>
          <version>${jetty.version}</version>
        </dependency>
        <!-- extra explicit dependency needed because there is a JSP in the sample-->
        <dependency>
          <groupId>org.eclipse.jetty</groupId>
          <artifactId>apache-jsp</artifactId>
          <version>${jetty.version}</version>
        </dependency>
      </dependencies>
    • Konfigurasi maven-assembly-plugin:
      
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <finalName>jetty</finalName>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>com.example.appengine.jetty.Main</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      
  5. Tempatkan jetty-jar-with-dependencies.jar yang dihasilkan dan file WAR aplikasi Anda. Misalnya, helloworld.war di direktori yang sama.

  6. Buat file app.yaml di direktori aplikasi Anda untuk menentukan runtime Java 21 dan elemen entrypoint untuk memanggil file jetty-jar-with-dependencies.jar dan meneruskan file WAR Anda sebagai argumen:

    runtime: java21
    entrypoint: 'java -jar jetty-jar-with-dependencies.jar helloworld.war'
    
  7. Untuk menjalankan aplikasi Anda secara lokal:

    1. Mulai server dengan file WAR Anda sebagai argumen:

      java -jar jetty-jar-with-dependencies.jar helloworld.war
      
    2. Di browser web, masukkan alamat berikut:

      http://localhost:8080

  8. Untuk men-deploy aplikasi Anda:

    alat gcloud

    gcloud app deploy

    Plugin Maven

    mvn package appengine:deploy -Dapp.deploy.projectId=PROJECT_ID

    Ganti PROJECT_ID dengan ID project Google Cloud Anda. Jika file pom.xml sudah menentukan project ID , Anda tidak perlu menyertakan properti -Dapp.deploy.projectId dalam perintah yang dijalankan.