Ripacchettizzazione di un file WAR in un file JAR

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

L'applicazione deve avere una classe Main che avvia un server web risponde alle richieste HTTP sulla porta specificata dall'ambiente PORT , in genere 8081.

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 creare un nuovo pacchetto App Engine L'applicazione Java 8 hello-world come JAR da eseguire sul runtime Java 11.

La migrazione utilizza l'artefatto appengine-simple-jetty-main. Ciò fornisce un Classe Main con un semplice server web Jetty che carica un file WAR e pacchetti la tua 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'anteprima come file ZIP ed estrarlo.

  2. Passa alla directory che contiene il codice di esempio:

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

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

    • 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 Directory ${build.directory}/appengine-staging. Aggiungendo il parametro Plug-in maven-dependency alla build, installazioni di App Engine le dipendenze specificate nella cartella corretta.
  5. Crea un elemento entrypoint nel file app.yaml per chiamare il metodo appengine-simple-jetty-main e passa il file WAR come . Ad esempio, consulta l'esempio di helloworld-servlet app.yaml file:

    runtime: java11
    entrypoint: 'java -cp "*" com.example.appengine.jetty.Main helloworld.war'
  6. Per eseguire la tua applicazione in locale:

    1. Pacchettizza la tua applicazione:

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

      Ad esempio, puoi avviare il server Esempio di helloworld-servlet eseguendo questo comando 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 della tua 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 progetto Google Cloud. Se il tuo pom.xml file specifica ID progetto , non è necessario includere la proprietà -Dapp.deploy.projectId nel il comando che esegui.