將 Web 應用程式部署到 Azure App Service

已完成

azure-webapp-maven-plugin 針對 Java 開發人員有兩個目標:第一個是讓管理和設定 Azure App Service 外掛程式更為容易,第二個則是簡化將 Maven 專案部署到 Azure App Service 的過程。 在上一個練習中,您建立了快速的 "hello world" 範例 Java Web 應用程式,並在本機進行了測試。 將適用於 Azure App Service 的 Maven 外掛程式新增到專案,可讓您將應用程式部署到 Azure App Service。

在本單元中,您將查看貴公司在 Azure App Server 上裝載其應用程式的選項,然後您將瞭解如何新增並將 azure-webapp-maven-plugin 設定至 Maven 專案。

Azure App Service 簡介

Azure App Service 可讓您在 Azure 上裝載公司的網站、Web 應用程式、REST API 及其他應用程式程式碼。 專案程式碼會在雲端執行;因此無需要佈建或設定任何基礎結構。 在 Azure App Service 中執行 Web 應用程式,可提供在 Azure 上執行的所有優點:應用程式可在全球使用、可自動調整規模、具備內建的安全性和合規性,且您只需要為所使用的資源付費。

Azure App Service 支援多種程式設計語言,可讓開發人員使用其最熟悉的語言來繼續撰寫應用程式。 Java 與 .NET Core、Python、Node.js 等語言一樣都是第一等公民。您可以將您所建立的網頁應用程式裝載在 Linux、Windows 上或 Docker 容器中。 在將 Web 應用程式部署到 Azure 之前,您將會在 Azure 上建立 App Service 方案來指定 OS 和定價層,其會決定應用程式需要的佈建計算資源大小。

Maven 工作流程

Maven 針對建置專案有三個內建生命週期:defaultcleansite,其中 default 生命週期包括下列階段:

階段 描述
compile 編譯程式碼
package 將程式碼封裝成 JAR 或 WAR
install 將套件安裝至本機存放庫
deploy 將最終套件複製到遠端存放庫

但是,當使用適用於 Azure App Service 的 Maven 外掛程式時,您將不會使用 Maven default 生命週期中所包含的部署階段。 您會改為使用 mvn azure-webapp:deploy 命令,將應用程式部署到 Azure。

將適用於 Azure App Service 的 Maven 外掛程式新增到專案

若要將 Azure App Service 的 Maven 外掛程式新增至 Web 應用程式,您可將 azure-webapp-maven-plugin 的 XML 新增至專案的 pom.xml 檔案,如下所示:

<plugin>
  <groupId>com.microsoft.azure</groupId>
  <artifactId>azure-webapp-maven-plugin</artifactId>
  <version>2.13.0</version>
</plugin>

不過,此外掛程式有方便的互動方式,可將設定新增至 POM 檔案:

mvn com.microsoft.azure:azure-webapp-maven-plugin:2.13.0:config

外掛程式會提示設定 App Service 方案所需要的資訊。 在確認選擇後,外掛程式會將上述外掛程式元素和必要設定新增到專案的 pom.xml 檔案,其會設定在 Azure App Service 中執行 Web 應用程式。

注意

請查看 Maven Plugin for Azure App Service documentation (適用於 Azure App Service 的 Maven 外掛程式文件),或 Maven Central Repository (Maven 中央存放庫),以取得 Maven 外掛程式最新版本的資訊。

設定選項

本單元上一節示範以互動方式使用適用於 Azure App Service 的 Maven 外掛程式來設定 Web 應用程式。 但是,無需以互動方式執行設定。 若有需要,可以手動方式將外掛程式的 XML 新增到專案的 pom.xml 檔案。

下列加上標註的 pom.xml 檔案摘要會示範一些必要設定:

<plugin> 
  <groupId>com.microsoft.azure</groupId>  
  <artifactId>azure-webapp-maven-plugin</artifactId>  
  <version>2.13.0</version>  
  <configuration> 
    <schemaVersion>v2</schemaVersion>  
    <subscriptionId>aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e</subscriptionId>  
    <resourceGroup>learn-9c627899-58ae-4ef5-9630-3336ec7bce0c</resourceGroup>  
    <appName>MyWebApp-1610102860270</appName>  
    <pricingTier>S1</pricingTier>  
    <region>westus</region>  
    <runtime> 
      <os>Linux</os>  
      <javaVersion>Java 17</javaVersion>  
      <webContainer>Tomcat 10.0</webContainer> 
    </runtime>  
    <deployment> 
      <resources> 
        <resource> 
          <directory>${project.basedir}/target</directory>  
          <includes> 
            <include>*.war</include> 
          </includes> 
        </resource> 
      </resources> 
    </deployment> 
  </configuration> 
</plugin> 

下表列出一些相關設定選項。 如需完整的選項清單,請參閱適用於 Azure App Service 的 Maven 外掛程式文件。

標籤 選項。
<javaVersion> Linux 支援版本 8、11 和 17
Windows 支援版本 8、11 和 17
<webContainer> Linux 支援 Tomcat、JBoss EAP 7.2 和 Java SE
Windows 支援 Tomcat 和 Java SE
<resource> 指定 WAR 或 JAR 在專案中的位置

部署選項

若已建立 App Service 方案,則可在 pom.xml 檔案中指定該方案的設定。 當將 Web 應用程式部署到 Azure 時,Maven 會使用這些設定,將新的應用程式部署到現有 App Service 方案。

<!-- Deploy Web App to the existing App Service Plan -->
<appServicePlanResourceGroup>${PLAN_RESOURCEGROUP_NAME}</appServicePlanResourceGroup>
<appServicePlanName>${PLAN_NAME}</appServicePlanName>

另一個選項是允許 azure-webapp-maven-plugin 在以互動方式設定外掛程式設定時,為您建立 App Service 方案。 若您在 pom.xml 檔案中沒有指定現有的 App Service 方案,則此選項是外掛程式的預設行為。

無論是選擇以互動還是手動方式建立 App Service 方案,您都會使用相同的 Maven 命令來將應用程式程式碼部署到 Azure:

mvn package azure-webapp:deploy

在下一個練習中,您會將 Web 應用程式部署到 Azure App Service。