이 문서에서는 자바 8 애플리케이션을 자바 11/17 런타임에서 실행할 JAR 파일로 다시 패키징하는 방법을 설명합니다.
애플리케이션에는 PORT 환경 변수로 지정될 수 있는 포트 8080에서 HTTP 요청에 응답하는 웹 서버를 시작하는 Main 클래스가 있어야 합니다.
예를 들면 다음과 같습니다.
importorg.eclipse.jetty.server.Server;importorg.eclipse.jetty.webapp.Configuration.ClassList;importorg.eclipse.jetty.webapp.WebAppContext;/** Simple Jetty Main that can execute a WAR file when passed as an argument. */publicclassMain{publicstaticvoidmain(String[]args)throwsException{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.intport=Integer.parseInt(System.getenv().getOrDefault("PORT","8080"));Serverserver=newServer(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.WebAppContextwebapp=newWebAppContext();webapp.setContextPath("/");webapp.setWar(args[0]);ClassListclasslist=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();}}
WAR 마이그레이션 예시
다음 안내에서는 자바 11/17 런타임에서 실행할 App Engine 자바 8 hello-world 애플리케이션을 JAR로 다시 패키징하는 방법을 보여줍니다.
마이그레이션은 appengine-simple-jetty-main 아티팩트를 사용합니다. 이는 WAR 파일을 로드하고 앱을 실행 가능한 JAR 파일로 패키징하는 간단한 Jetty 웹 서버가 포함된 Main 클래스를 제공합니다.
App Engine은 ${build.directory}/appengine-staging 디렉터리에 있는 파일을 배포합니다. App Engine은 빌드에 maven-dependency 플러그인을 추가하여 지정된 종속 항목을 올바른 폴더에 설치합니다.
app.yaml 파일에 entrypoint 요소를 만들어 appengine-simple-jetty-main 객체를 호출하고 WAR 파일을 인수로 전달합니다. WAR 버전은 pom.xml 파일에 나열된 버전과 동일해야 합니다.
runtime:javaenv:flexruntime_config:operating_system:ubuntu18runtime_version:11entrypoint:'java-cp"*"com.example.appengine.jetty.Mainhelloworld-1.war'handlers:-url:/.*script:this field is required, but ignoredmanual_scaling:instances:1
애플리케이션을 로컬에서 실행하려면 다음 안내를 따르세요.
애플리케이션을 패키징합니다.
mvncleanpackage
WAR 파일을 인수로 사용하여 서버를 시작합니다.
예를 들어 java-docs-samples/appengine-java11/appengine-simple-jetty-main/ 폴더에서 다음 명령어를 실행하여 helloworld-war 샘플에서 서버를 시작할 수 있습니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-03-07(UTC)"],[[["This document guides you through repackaging a Java 8 application as a JAR file compatible with Java 11/17 runtimes, requiring a `Main` class that starts a web server on port 8080."],["The migration process utilizes the `appengine-simple-jetty-main` artifact, which includes a `Main` class with a basic Jetty web server capable of loading a WAR file and packaging the app into an executable JAR."],["To implement the migration, you must add the `appengine-simple-jetty-main` dependency and the `maven-dependency` plugin to your project's `pom.xml` file."],["An `entrypoint` element must be defined in the `app.yaml` file, invoking the `appengine-simple-jetty-main` object and passing the WAR file as an argument."],["The application can be deployed using Maven, using the `mvn package appengine:deploy` command and specifying the project ID if it's not already specified in the `pom.xml` file."]]],[]]