指定依附元件

您可以在 App Engine 標準環境中,將任何 Java 相容程式庫與 Java 執行階段搭配使用。以下操作說明使用 Apache Maven 來建構、執行及部署範例應用程式。 如需安裝 Maven 的詳細說明,請參閱使用 Apache Maven 和 App Engine 外掛程式

宣告及管理依附元件

如要使用 Maven 管理依附元件,請在專案的 pom.xml 檔案中,於 <dependencies> 區段指定依附元件。

如要管理專案對 Maven 本身的依附元件,可以使用 Maven Wrapper。如果您未使用 Maven Wrapper,App Engine 預設會在執行 gcloud app deploy 時使用最新版的 Maven。

使用 Artifact Registry 的私有依附元件

如要為 Java 應用程式代管私人依附元件,可以使用 Artifact Registry Maven 存放區。如要連線至 Artifact Registry 託管的 Maven 存放區,Maven 或 Gradle 設定必須使用 Artifact Registry Maven Tools 外掛程式。

指定 Java Servlet 程式庫

Eclipse Jetty 與 Tomcat 應用程式都需要 Java Servlet 程式庫。請在 pom.xml 檔案的 <dependencies> 項目中指定這個程式庫:

<dependencies>

  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <type>jar</type>
    <scope>provided</scope>
  </dependency>

</dependencies>
請注意,SparkJavaSpring Boot 等架構不需要 Servlet 程式庫。

使用 Cloud 用戶端程式庫

Java 適用的 Cloud 用戶端程式庫可慣用存取 Google Cloud 服務。如要使用程式庫,請將其宣告為依附元件。

一般來說,您只會針對應用程式需要的特定程式庫宣告依附元件。舉例來說,如要使用 Cloud Storage 程式庫:

<!--  Using libraries-bom to manage versions.
See https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google-Cloud-Platform-Libraries-BOM -->
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>libraries-bom</artifactId>
      <version>26.28.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-storage</artifactId>
  </dependency>
</dependencies>

您可以將 Java 適用的 Cloud 用戶端程式庫設為自動處理驗證