快速入门:在应用服务中创建 Java 应用Quickstart: Create a Java app in App Service

Note

本文将应用部署到 Windows 上的应用服务。This article deploys an app to App Service on Windows.

Azure 应用服务提供高度可缩放、自修补的 Web 托管服务。Azure App Service provides a highly scalable, self-patching web hosting service. 本快速入门介绍如何将 Azure CLI用于 Azure 应用服务的 Maven 插件配合使用来部署 Java Web 存档 (WAR) 文件。This quickstart shows how to use the Azure CLI with the Maven Plugin for Azure App Service to deploy a Java web archive (WAR) file.

Note

也可使用 IntelliJ 和 Eclipse 等常见 IDE 执行相同的操作。The same thing can also be done using popular IDEs like IntelliJ and Eclipse. 请查看 Azure Toolkit for IntelliJ 快速入门Azure Toolkit for Eclipse 快速入门中的类似文档。Check out our similar documents at Azure Toolkit for IntelliJ Quickstart or Azure Toolkit for Eclipse Quickstart.

在 Azure 中运行的示例应用

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

创建 Java 应用Create a Java app

在 Azure CLI 提示符下,执行以下 Maven 命令来创建一个名为 helloworld 的新应用:Execute the following Maven command in the Azure CLI prompt to create a new app named helloworld:

mvn archetype:generate -DgroupId=example.demo -DartifactId=helloworld -DarchetypeArtifactId=maven-archetype-webapp

配置 Maven 插件Configure the Maven plugin

若要从 Maven 进行部署,请在 Azure CLI 中使用代码编辑器打开 helloworld 目录中的项目 pom.xml 文件。To deploy from Maven, use the code editor in the Azure CLI to open up the project pom.xml file in the helloworld directory.

code pom.xml

然后在 pom.xml 文件的 <build> 元素内添加以下插件定义。Then add the following plugin definition inside the <build> element of the pom.xml file.

<plugins>
    <!--*************************************************-->
    <!-- Deploy to Tomcat in App Service Windows         -->
    <!--*************************************************-->
    <plugin>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>azure-webapp-maven-plugin</artifactId>
        <version>1.7.0</version>
        <configuration>
            <!-- Specify v2 schema -->
            <schemaVersion>v2</schemaVersion>
            <!-- App information -->
            <subscriptionId>${SUBSCRIPTION_ID}</subscriptionId>
            <resourceGroup>${RESOURCEGROUP_NAME}</resourceGroup>
            <appName>${WEBAPP_NAME}</appName>
            <region>${REGION}</region>
            <!-- Java Runtime Stack for App Service on Windows-->
            <runtime>
                <os>windows</os>
                <javaVersion>1.8</javaVersion>
                <webContainer>tomcat 9.0</webContainer>
            </runtime>
            <deployment>
                <resources>
                    <resource>
                        <directory>${project.basedir}/target</directory>
                        <includes>
                            <include>*.war</include>
                        </includes>
                    </resource>
                </resources>
            </deployment>
        </configuration>
    </plugin>
</plugins>

Note

在本文中,我们仅使用在 WAR 文件中打包的 Java 应用。In this article we are only working with Java apps packaged in WAR files. 该插件还支持 JAR Web 应用程序。若要试用,请访问在 Linux 上将 Java SE JAR 文件部署到应用服务The plugin also supports JAR web applications, visit Deploy a Java SE JAR file to App Service on Linux to try it out.

更新插件配置中的以下占位符:Update the following placeholders in the plugin configuration:

占位符Placeholder 说明Description
SUBSCRIPTION_ID 想要将应用部署到的订阅的唯一 ID。The unique ID of the subscription you want to deploy your app to. 可以使用 az account show 命令从 Azure CLI 中找到默认订阅的 ID。Default subscription's ID can be found from the Azure CLI using the az account show command. 对于所有可用的订阅,请使用 az account list 命令。For all the available subscriptions, use the az account list command.
RESOURCEGROUP_NAME 要在其中创建应用的新资源组的名称。Name for the new resource group in which to create your app. 通过将应用的所有资源都放在一个组中,可以一起管理它们。By putting all the resources for an app in a group, you can manage them together. 例如,删除资源组会删除与该应用关联的所有资源。For example, deleting the resource group would delete all resources associated with the app. 使用唯一的新资源组名称(例如 myResourceGroup)更新此值。Update this value with a unique new resource group name, for example, myResourceGroup. 将在后面的部分使用此资源组名称来清除所有 Azure 资源。You will use this resource group name to clean up all Azure resources in a later section.
WEBAPP_NAME 部署到 Azure (WEBAPP_NAME.chinacloudsites.cn) 时,应用名称将成为应用的主机名的一部分。The app name will be part of the host name for the app when deployed to Azure (WEBAPP_NAME.chinacloudsites.cn). 使用将托管 Java 应用的新应用服务应用的唯一名称(例如 contoso)更新此值。Update this value with a unique name for the new App Service app, which will host your Java app, for example contoso.
REGION 托管着应用的 Azure 区域,例如 chinaeast2 。An Azure region where the app is hosted, for example chinaeast2. 可以从 Azure CLI 使用 az account list-locations 命令获取区域列表。You can get a list of regions from the Azure CLI using the az account list-locations command.

部署应用Deploy the app

使用以下命令将 Java 应用部署到 Azure:Deploy your Java app to Azure using the following command:

mvn package azure-webapp:deploy

部署完成后,在 Web 浏览器中使用以下 URL 浏览到已部署的应用程序,例如 http://<webapp>.chinacloudsites.cn/Once deployment has completed, browse to the deployed application using the following URL in your web browser, for example http://<webapp>.chinacloudsites.cn/.

在 Azure 中运行的示例应用

祝贺你!Congratulations! 现已将第一个 Java 应用部署到 Windows 应用服务。You've deployed your first Java app to App Service on Windows.

清理资源Clean up resources

若要清理资源,请运行以下命令:To clean up your resources, run the following command:

az group delete --name myResourceGroup

后续步骤Next steps