Ripacchettizzazione di un file WAR in un file JAR

Se stai eseguendo la migrazione alla versione Java supportata più recente e la tua app non utilizza i servizi in bundle legacy, devi ripacchettizzare l'applicazione web Java 8 di App Engine in un file JAR eseguibile.

L'applicazione deve avere una classe Main che avvia un server web che risponde alle richieste HTTP sulla porta 8080, che potrebbe essere specificata dalla variabile di ambiente PORT.

Ad esempio:

import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

public class Main {

  public static void main(String[] args) throws IOException {
    // Create an instance of HttpServer bound to port defined by the 
    // PORT environment variable when present, otherwise on 8080.
    int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"));
    HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);

    // Set root URI path.
    server.createContext("/", (var t) -> {
      byte[] response = "Hello World from Google App Engine Java 11.".getBytes();
      t.sendResponseHeaders(200, response.length);
      try (OutputStream os = t.getResponseBody()) {
        os.write(response);
      }
    });

    // Start the server.
    server.start();
  }
}

Esempio di migrazione WAR (Java 11)

Le seguenti istruzioni mostrano come ripacchettizzare un'applicazione hello-world Java 8 di App Engine come JAR da eseguire sul runtime Java 11.

La migrazione utilizza l'artefatto appengine-simple-jetty-main. Questo fornisce una classe Main con un semplice server web Jetty che carica un file WAR e pacchettizza l'app in un file JAR eseguibile:

  1. Clona l'artefatto del server Jetty incorporato sulla tua macchina locale:

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

    In alternativa puoi scaricare l'esempio come file ZIP ed estrarlo.

  2. Passa alla directory che contiene il codice campione:

    cd java-docs-samples/appengine-java11/appengine-simple-jetty-main/
    
  3. Installa la dipendenza localmente:

    mvn install
    
  4. Aggiungi il seguente codice al file pom.xml del progetto:

    • Dipendenza appengine-simple-jetty-main:
      <dependency>
        <groupId>com.example.appengine</groupId>
        <artifactId>simple-jetty-main</artifactId>
        <version>1</version>
        <scope>provided</scope>
      </dependency>
    • Plug-in maven-dependency:
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.6.1</version>
        <executions>
          <execution>
            <id>copy</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>
                ${project.build.directory}/appengine-staging
              </outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
      App Engine esegue il deployment dei file che si trovano nella directory ${build.directory}/appengine-staging. Se aggiungi il plug-in maven-dependency alla build, App Engine installa le dipendenze specificate nella cartella corretta.
  5. Crea un elemento entrypoint nel file app.yaml per chiamare l'oggetto appengine-simple-jetty-main e passare il file WAR come argomento. Ad esempio, consulta il helloworld-servlet campione app.yaml file:

    runtime: java11
    entrypoint: 'java -cp "*" com.example.appengine.jetty.Main helloworld.war'
  6. Per eseguire l'applicazione localmente:

    1. Pacchettizza l'applicazione:

      mvn clean package
      
    2. Avvia il server con il file WAR come argomento.

      Ad esempio, puoi avviare il server nell' helloworld-servletesempio eseguendo il seguente comando dalla java-docs-samples/appengine-java11/appengine-simple-jetty-main/ cartella:

      mvn exec:java -Dexec.args="../helloworld-java8/target/helloworld.war"
      
    3. Nel browser web, inserisci il seguente indirizzo:

      http://localhost:8080

  7. Per eseguire il deployment dell'applicazione:

    Strumenti gcloud

    gcloud app deploy

    Plug-in Maven

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

    Sostituisci PROJECT_ID con l'ID del tuo Google Cloud progetto. Se il file pom.xml specifica già l'ID progetto, non devi includere la proprietà -Dapp.deploy.projectId nel comando che esegui.