

AWS App Runner 自 2026 年 4 月 30 日起，不再開放給新客戶。如果您想要使用 App Runner，請在該日期之前註冊。現有客戶可以繼續正常使用該服務。如需詳細資訊，請參閱[AWS App Runner 可用性變更](https://docs.aws.amazon.com/apprunner/latest/dg/apprunner-availability-change.html)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 Java 平台
<a name="service-source-code-java"></a>

 AWS App Runner Java 平台提供受管執行期。每個執行時間都可讓您根據 Java 版本使用 Web 應用程式輕鬆建置和執行容器。當您使用 Java 執行期時，App Runner 會從受管 Java 執行期映像開始。此映像以 [Amazon Linux Docker 映像](https://hub.docker.com/_/amazonlinux)為基礎，並包含 Java 版本的執行時間套件和一些工具。App Runner 使用此受管執行期映像做為基礎映像，並新增您的應用程式程式碼來建置 Docker 映像。然後，它會部署此映像，以在容器中執行您的 Web 服務。

 當您使用 App Runner 主控台或 [CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html) API 操作[建立服務時，](manage-create.md)您可以指定 App Runner 服務的執行時間。您也可以指定執行時間做為原始程式碼的一部分。在程式碼儲存庫中包含的 [App Runner 組態檔案中](config-file.md)使用 `runtime`關鍵字。受管執行時間的命名慣例為 *<language-name><major-version>*。

目前，所有支援的 Java 執行時間都以 Amazon Corretto 為基礎。如需有效的 Java 執行期名稱和版本，請參閱 [Java 執行期版本資訊](service-source-code-java-releases.md)。

App Runner 會在每次部署或服務更新時，將服務的執行時間更新為最新版本。如果您的應用程式需要特定版本的受管執行時間，您可以使用 [App Runner 組態檔案中](config-file.md)的 `runtime-version`關鍵字來指定它。您可以鎖定任何層級的版本，包括主要或次要版本。App Runner 只會對服務的執行時間進行較低層級的更新。

Amazon Corretto 執行時間的版本語法：


| **執行時間** | **語法** | **範例** | 
| --- | --- | --- | 
|  corretto11  |  `11.0[.openjdk-update[.openjdk-build[.corretto-specific-revision]]]`  |  `11.0.13.08.1`  | 
|  corretto8  |  `8[.openjdk-update[.openjdk-build[.corretto-specific-revision]]]`  |  `8.312.07.1`  | 

下列範例示範版本鎖定：
+ `11.0.13` – 鎖定 Open JDK 更新版本。App Runner 只會更新 Open JDK 和 Amazon Corretto 低階組建。
+ `11.0.13.08.1` – 鎖定至特定版本。App Runner 不會更新您的執行時間版本。

**Topics**
+ [Java 執行期組態](#service-source-code-java.config)
+ [Java 執行時間範例](#service-source-code-java.examples)
+ [Java 執行期版本資訊](service-source-code-java-releases.md)

## Java 執行期組態
<a name="service-source-code-java.config"></a>

當您選擇受管執行時間時，您也必須至少設定建置和執行命令。您可以在[建立](manage-create.md)或[更新](manage-configure.md) App Runner 服務時設定它們。您可以使用下列其中一種方法執行此操作：
+ **使用 App Runner 主控台** – 在建立程序或組態索引標籤**的設定建置**區段中指定命令。
+ **使用 App Runner API** – 呼叫 [CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html) 或 [UpdateService](https://docs.aws.amazon.com/apprunner/latest/api/API_UpdateService.html) API 操作。使用 [CodeConfigurationValues](https://docs.aws.amazon.com/apprunner/latest/api/API_CodeConfigurationValues.html) 資料類型的 `BuildCommand`和 `StartCommand`成員指定命令。
+ **使用[組態檔案](config-file.md)** – 在最多三個建置階段指定一或多個建置命令，以及用於啟動應用程式的單一執行命令。還有其他選用的組態設定。

提供組態檔案是選用的。當您使用主控台或 API 建立 App Runner 服務時，您可以指定 App Runner 在建立時直接從組態檔案取得您的組態設定。

## Java 執行時間範例
<a name="service-source-code-java.examples"></a>

下列範例顯示用於建置和執行 Java 服務的 App Runner 組態檔案。最後一個範例是完整 Java 應用程式的原始碼，您可以部署到 Corretto 11 執行時間服務。

**注意**  
這些範例中使用的執行時間版本為 *11.0.13.08.1*。您可以將其取代為您想要使用的版本。如需最新支援的 Java 執行期版本，請參閱 [Java 執行期版本資訊](service-source-code-java-releases.md)。

### 最小 Corretto 11 組態檔案
<a name="service-source-code-java.examples.minimal"></a>

此範例顯示您可以搭配 Corretto 11 受管執行時間使用的最小組態檔案。如需 App Runner 使用最少組態檔案所做的假設，請參閱 。

**Example apprunner.yaml**  

```
version: 1.0
runtime: corretto11
build:
  commands:    
    build:
      - mvn clean package
run:                              
  command: java -Xms256m -jar target/MyApp-1.0-SNAPSHOT.jar .
```

### 擴充 Corretto 11 組態檔案
<a name="service-source-code-java.examples.extended"></a>

此範例示範如何使用所有組態金鑰搭配 Corretto 11 受管執行時間。

**注意**  
這些範例中使用的執行時間版本為 *11.0.13.08.1*。您可以將其取代為您想要使用的版本。如需最新支援的 Java 執行期版本，請參閱 [Java 執行期版本資訊](service-source-code-java-releases.md)。

**Example apprunner.yaml**  

```
version: 1.0
runtime: corretto11
build:
  commands:
    pre-build:
      - yum install some-package
      - scripts/prebuild.sh
    build:
      - mvn clean package
    post-build:
      - mvn clean test
  env:
    - name: M2
      value: "/usr/local/apache-maven/bin"
    - name: M2_HOME
      value: "/usr/local/apache-maven/bin"
run:
  runtime-version: 11.0.13.08.1
  command: java -Xms256m -jar target/MyApp-1.0-SNAPSHOT.jar .
  network:
    port: 8000
    env: APP_PORT
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"
```

### 完成 Corretto 11 應用程式來源
<a name="service-source-code-java.examples.end2end"></a>

此範例顯示完整 Java 應用程式的原始程式碼，您可以部署到 Corretto 11 執行時間服務。

**Example src/main/java/com/HelloWorld/HelloWorld.java**  

```
package com.HelloWorld;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorld {

    @RequestMapping("/")
    public String index(){
        String s = "Hello World";
        return s;
    }
}
```

**Example src/main/java/com/HelloWorld/Main.java**  

```
package com.HelloWorld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {

    public static void main(String[] args) {

        SpringApplication.run(Main.class, args);
    }
}
```

**Example apprunner.yaml**  

```
version: 1.0
runtime: corretto11
build:
  commands:    
    build:
      - mvn clean package
run:                              
  command: java -Xms256m -jar target/HelloWorldJavaApp-1.0-SNAPSHOT.jar .
  network:
    port: 8080
```

**Example pom.xml**  

```
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.1.RELEASE</version>
    <relativePath/>
  </parent>
  <groupId>com.HelloWorld</groupId>
  <artifactId>HelloWorldJavaApp</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <java.version>11</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <release>11</release>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
```