添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft Edge More info about Internet Explorer and Microsoft Edge

In this article

In this quickstart, you'll use the Maven Plugin for Azure App Service Web Apps to deploy a Java web application to a Linux Tomcat server in Azure App Service . App Service provides a highly scalable, self-patching web app hosting service. Use the tabs to switch between Tomcat, JBoss, or embedded server (Java SE) instructions.

If Maven isn't your preferred development tool, check out our similar tutorials for Java developers:

  • Gradle
  • IntelliJ IDEA
  • Eclipse
  • Visual Studio Code
  • If you don't have an Azure subscription , create an Azure free account before you begin.

    1 - Use Azure Cloud Shell

    Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. You can use either Bash or PowerShell with Cloud Shell to work with Azure services. You can use the Cloud Shell preinstalled commands to run the code in this article, without having to install anything on your local environment.

    To start Azure Cloud Shell:

    Option Example/Link
  • Start Cloud Shell.

  • Select the Copy button on a code block (or command block) to copy the code or command.

  • Paste the code or command into the Cloud Shell session by selecting Ctrl + Shift + V on Windows and Linux, or by selecting Cmd + Shift + V on macOS.

  • Select Enter to run the code or command.

    2 - Create a Java app

    Execute the following Maven command in the Cloud Shell prompt to create a new app named helloworld :

    mvn archetype:generate "-DgroupId=example.demo" "-DartifactId=helloworld" "-DarchetypeArtifactId=maven-archetype-webapp" "-DarchetypeVersion=1.4" "-Dversion=1.0-SNAPSHOT"
    

    Then change your working directory to the project folder:

    cd helloworld
    

    3 - Configure the Maven plugin

    The deployment process to Azure App Service uses your Azure credentials from the Azure CLI automatically. If the Azure CLI isn't installed locally, then the Maven plugin authenticates with OAuth or device sign-in. For more information, see authentication with Maven plugins.

    Run the Maven command shown next to configure the deployment. This command helps you to set up the App Service operating system, Java version, and Tomcat version.

    mvn com.microsoft.azure:azure-webapp-maven-plugin:2.12.0:config
    
  • For Create new run configuration, type Y, then Enter.

  • For Define value for OS, type 1 for Windows, or 2 for Linux, then Enter.

  • For Define value for javaVersion, type 3 for Java 17, then Enter.

  • For Define value for webContainer, type 1 for Tomcat 10.0, then Enter.

  • For Define value for pricingTier, type 9 for P1v2, then Enter.

  • For Confirm, type Y, then Enter.

    Please confirm webapp properties
    AppName : helloworld-1690440759246
    ResourceGroup : helloworld-1690440759246-rg
    Region : centralus
    PricingTier : P1v2
    OS : Linux
    Java Version: Java 17
    Web server stack: Tomcat 10.0
    Deploy to slot : false
    Confirm (Y/N) [Y]: 
    [INFO] Saving configuration to pom.
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  13.069 s
    [INFO] Finished at: 2023-07-27T06:52:48Z
    [INFO] ------------------------------------------------------------------------
    

    After you've confirmed your choices, the plugin adds the above plugin element and requisite settings to your project's pom.xml file that configure your web app to run in Azure App Service.

    The relevant portion of the pom.xml file should look similar to the following example.

    <build>
        <plugins>
            <plugin>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>>azure-webapp-maven-plugin</artifactId>
                <version>x.xx.x</version>
                <configuration>
                    <schemaVersion>v2</schemaVersion>
                    <resourceGroup>your-resourcegroup-name</resourceGroup>
                    <appName>your-app-name</appName>
                </configuration>
            </plugin>
        </plugins>
    </build>           
    

    You can modify the configurations for App Service directly in your pom.xml. Some common configurations are listed in the following table:

    Property Required Description Version <schemaVersion> false Specify the version of the configuration schema. Supported values are: v1, v2. 1.5.2 <subscriptionId> false Specify the subscription ID. 0.1.0+ <resourceGroup> Azure Resource Group for your Web App. 0.1.0+ <appName> The name of your Web App. 0.1.0+ <region> false Specifies the region to host your Web App; the default value is centralus. All valid regions at Supported Regions section. 0.1.0+ <pricingTier> false The pricing tier for your Web App. The default value is P1v2 for production workload, while B2 is the recommended minimum for Java dev/test. For more information, see App Service Pricing 0.1.0+ <runtime> false The runtime environment configuration. For more information, see Configuration Details. 0.1.0+ <deployment> false The deployment configuration. For more information, see Configuration Details. 0.1.0+

    For the complete list of configurations, see the plugin reference documentation. All the Azure Maven Plugins share a common set of configurations. For these configurations see Common Configurations. For configurations specific to App Service, see Azure Web App: Configuration Details.

    Be careful about the values of <appName> and <resourceGroup> (helloworld-1690440759246 and helloworld-1690440759246-rg accordingly in the demo). They're used later.

    4 - Deploy the app

    With all the configuration ready in your pom.xml file, you can deploy your Java app to Azure with one single command.

    mvn package azure-webapp:deploy
    

    Once deployment is completed, your application is ready at http://<appName>.azurewebsites.net/ (http://helloworld-1690440759246.azurewebsites.net in the demo). Open the url with your local web browser, you should see

    Congratulations! You've deployed your first Java app to App Service.

    5 - Clean up resources

    In the preceding steps, you created Azure resources in a resource group. If you don't need the resources in the future, delete the resource group from portal, or by running the following command in the Cloud Shell:

    az group delete --name <your resource group name; for example: helloworld-1690440759246-rg> --yes
    

    This command may take a minute to run.

    In this quickstart, you use the Maven Plugin for Azure App Service Web Apps to deploy a Java web application with an embedded server to Azure App Service. App Service provides a highly scalable, self-patching web app hosting service. Use the tabs to switch between Tomcat, JBoss, or embedded server (Java SE) instructions.

    The quickstart deploys either a Spring Boot app, embedded Tomcat, or Quarkus app using the azure-webapp-maven-plugin plugin.

    App Service can host Spring apps. For Spring apps that require all the Spring services, try Azure Spring Apps instead.

    If Maven isn't your preferred development tool, check out our similar tutorials for Java developers:

  • Gradle
  • IntelliJ IDEA
  • Eclipse
  • Visual Studio Code
  • If you don't have an Azure subscription, create an Azure free account before you begin.

    1 - Use Azure Cloud Shell

    Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. You can use either Bash or PowerShell with Cloud Shell to work with Azure services. You can use the Cloud Shell preinstalled commands to run the code in this article, without having to install anything on your local environment.

    To start Azure Cloud Shell:

    Option Example/Link
  • Start Cloud Shell.

  • Select the Copy button on a code block (or command block) to copy the code or command.

  • Paste the code or command into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux, or by selecting Cmd+Shift+V on macOS.

  • Select Enter to run the code or command.

    2 - Get the sample app

    Spring Boot Embedded Tomcat Quarkus
  • Download and extract the default Spring Boot web application template. This repository is cloned for you when you run the Spring CLI command spring boot new my-webapp.

    git clone https://github.com/rd-1-2022/rest-service my-webapp
    
  • Change your working directory to the project folder:

    cd my-webapp
    
  • Download and extract the embeddedTomcatExample repository, or clone it locally by running git clone:

    git clone https://github.com/Azure-Samples/java-docs-embedded-tomcat
    
  • Change your working directory to the project folder:

    cd java-docs-embedded-tomcat
    

    The application is run using the standard Tomcat class (see Main.java in the sample).

  • Generate a new Quarkus app named quarkus-hello-azure with the following Maven command:

    mvn io.quarkus.platform:quarkus-maven-plugin:3.2.2.Final:create \
        -DprojectGroupId=org.acme \
        -DprojectArtifactId=quarkus-hello-azure  \
        -Dextensions='resteasy-reactive'
    
  • Change your working directory to the project folder:

    cd quarkus-hello-azure
    

    3 - Configure the Maven plugin

    The deployment process to Azure App Service uses your Azure credentials from the Azure CLI automatically. If the Azure CLI isn't installed locally, then the Maven plugin authenticates with OAuth or device sign-in. For more information, see authentication with Maven plugins.

    Run the Maven command shown next to configure the deployment. This command helps you to set up the App Service operating system, Java version, and Tomcat version.

    mvn com.microsoft.azure:azure-webapp-maven-plugin:2.13.0:config
    
  • For Create new run configuration, type Y, then Enter.

  • For Define value for OS, type 2 for Linux, then Enter.

  • For Define value for javaVersion, type 1 for Java 17, then Enter.

  • For Define value for pricingTier, type 9 for P1v2, then Enter.

  • For Confirm, type Y, then Enter.

    Please confirm webapp properties
    AppName : <generated-app-name>
    ResourceGroup : <generated-app-name>-rg
    Region : centralus
    PricingTier : P1v2
    OS : Linux
    Java Version: Java 17
    Web server stack: Java SE
    Deploy to slot : false
    Confirm (Y/N) [Y]: y
    [INFO] Saving configuration to pom.
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  8.139 s
    [INFO] Finished at: 2023-07-26T12:42:48Z
    [INFO] ------------------------------------------------------------------------
    

    After you confirm your choices, the plugin adds the above plugin element and requisite settings to your project's pom.xml file that configure your web app to run in Azure App Service.

    The relevant portion of the pom.xml file should look similar to the following example.

    <build>
        <plugins>
            <plugin>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>>azure-webapp-maven-plugin</artifactId>
                <version>x.xx.x</version>
                <configuration>
                    <schemaVersion>v2</schemaVersion>
                    <resourceGroup>your-resourcegroup-name</resourceGroup>
                    <appName>your-app-name</appName>
                </configuration>
            </plugin>
        </plugins>
    </build>           
    

    You can modify the configurations for App Service directly in your pom.xml. Some common configurations are listed in the following table:

    Property Required Description Version <schemaVersion> false Specify the version of the configuration schema. Supported values are: v1, v2. 1.5.2 <subscriptionId> false Specify the subscription ID. 0.1.0+ <resourceGroup> Azure Resource Group for your Web App. 0.1.0+ <appName> The name of your Web App. 0.1.0+ <region> false Specifies the region to host your Web App; the default value is centralus. All valid regions at Supported Regions section. 0.1.0+ <pricingTier> false The pricing tier for your Web App. The default value is P1v2 for production workload, while B2 is the recommended minimum for Java dev/test. For more information, see App Service Pricing 0.1.0+ <runtime> false The runtime environment configuration. For more information, see Configuration Details. 0.1.0+ <deployment> false The deployment configuration. For more information, see Configuration Details. 0.1.0+

    For the complete list of configurations, see the plugin reference documentation. All the Azure Maven Plugins share a common set of configurations. For these configurations see Common Configurations. For configurations specific to App Service, see Azure Web App: Configuration Details.

    Be careful about the values of <appName> and <resourceGroup>. They're used later.

    4 - Deploy the app

    With all the configuration ready in your pom.xml file, you can deploy your Java app to Azure with one single command.

  • Build the JAR file using the following command:

    Spring Boot Embedded Tomcat Quarkus
    mvn clean package
    

    To make the application it deployable using azure-webapp-maven-plugin, and running on Azure App Service, the sample configures the package goal as follows:

  • Build a single uber JAR file, which contains everything the application needs to run.
  • Create an executable JAR by specifying the Tomcat class as the start-up class.
  • Replace the original artifact with the uber JAR to ensure that the deploy step deploys the right file.
  • Quarkus and Spring Boot both produce two JAR files with mvn clean package, but azure-webapp-maven-plugin picks the right JAR file to deploy automatically.

    mvn clean package -Dquarkus.package.type=uber-jar -D%prod.quarkus.http.port=80 
    

    This command adds two Quarkus properties:

  • quarkus.package.type=uber-jar tells Maven to generate an Uber-Jar, which includes all dependencies in the JAR file.
  • %prod.quarkus.http.port=80 tells Quarkus to use port 80 for the prod environment at runtime, which is the default port that the Linux Java container uses. If you want, you can change the port number of the Java container with the WEBSITES_PORT app setting.
  • You can configure these properties by using other means, but they're added to mvn package command here for simplicity.

  • Deploy to Azure by using the following command:

    mvn azure-webapp:deploy
    

    If the deployment succeeds, you see the following output:

    [INFO] Successfully deployed the artifact to https://<app-name>.azurewebsites.net
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  02:20 min
    [INFO] Finished at: 2023-07-26T12:47:50Z
    [INFO] ------------------------------------------------------------------------
    

    Congratulations! You deployed your first Java app to App Service.

    5 - Clean up resources

    In the preceding steps, you created Azure resources in a resource group. If you don't need the resources in the future, delete the resource group from portal, or by running the following command in the Cloud Shell:

    az group delete --name <your resource group name; for example: quarkus-hello-azure-1690375364238-rg> --yes
    

    This command might take a minute to run.

    In this quickstart, you'll use the Maven Plugin for Azure App Service Web Apps to deploy a Java web application to a Linux JBoss EAP server in Azure App Service. App Service provides a highly scalable, self-patching web app hosting service. Use the tabs to switch between Tomcat, JBoss, or embedded server (Java SE) instructions.

    If Maven isn't your preferred development tool, check out our similar tutorials for Java developers:

  • Gradle
  • IntelliJ IDEA
  • Eclipse
  • Visual Studio Code
  • If you don't have an Azure subscription, create an Azure free account before you begin.

    1 - Use Azure Cloud Shell

    Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. You can use either Bash or PowerShell with Cloud Shell to work with Azure services. You can use the Cloud Shell preinstalled commands to run the code in this article, without having to install anything on your local environment.

    To start Azure Cloud Shell:

    Option Example/Link
  • Start Cloud Shell.

  • Select the Copy button on a code block (or command block) to copy the code or command.

  • Paste the code or command into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux, or by selecting Cmd+Shift+V on macOS.

  • Select Enter to run the code or command.

    2 - Create a Java app

    Clone the Pet Store demo application.

    git clone https://github.com/Azure-Samples/app-service-java-quickstart
    

    Change directory to the completed pet store project and build it.

    The petstore-ee7 sample requires Java 11 or newer. The booty-duke-app-service sample project requires Java 17. If your installed version of Java is less than 17, run the build from within the petstore-ee7 directory, rather than at the top level.

    cd app-service-java-quickstart
    git checkout 20230308
    cd petstore-ee7
    mvn clean install
    

    If you see a message about being in detached HEAD state, this message is safe to ignore. Because you won't make any Git commit in this quickstart, detached HEAD state is appropriate.

    3 - Configure the Maven plugin

    The deployment process to Azure App Service uses your Azure credentials from the Azure CLI automatically. If the Azure CLI isn't installed locally, then the Maven plugin authenticates with OAuth or device sign-in. For more information, see authentication with Maven plugins.

    Run the Maven command shown next to configure the deployment. This command helps you to set up the App Service operating system, Java version, and Tomcat version.

    mvn com.microsoft.azure:azure-webapp-maven-plugin:2.12.0:config
    
  • For Create new run configuration, type Y, then Enter.

  • For Define value for OS, type 2 for Linux, then Enter.

  • For Define value for javaVersion, type 2 for Java 11, then Enter.

  • For webContainer option, type 1 for Jbosseap 7, then Enter.

  • For Define value for pricingTier, type 1 for P1v3, then Enter.

  • For Confirm, type Y, then Enter.

    Please confirm webapp properties
    AppName : petstoreee7-1690443003536
    ResourceGroup : petstoreee7-1690443003536-rg
    Region : centralus
    PricingTier : P1v3
    OS : Linux
    Java Version: Java 11
    Web server stack: Jbosseap 7
    Deploy to slot : false
    Confirm (Y/N) [Y]: 
    [INFO] Saving configuration to pom.
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  19.914 s
    [INFO] Finished at: 2023-07-27T07:30:20Z
    [INFO] ------------------------------------------------------------------------
    

    After you've confirmed your choices, the plugin adds the above plugin element and requisite settings to your project's pom.xml file that configure your web app to run in Azure App Service.

    The relevant portion of the pom.xml file should look similar to the following example.

    <build>
        <plugins>
            <plugin>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>>azure-webapp-maven-plugin</artifactId>
                <version>x.xx.x</version>
                <configuration>
                    <schemaVersion>v2</schemaVersion>
                    <resourceGroup>your-resourcegroup-name</resourceGroup>
                    <appName>your-app-name</appName>
                </configuration>
            </plugin>
        </plugins>
    </build>           
    

    You can modify the configurations for App Service directly in your pom.xml. Some common configurations are listed in the following table:

    Property Required Description Version <schemaVersion> false Specify the version of the configuration schema. Supported values are: v1, v2. 1.5.2 <subscriptionId> false Specify the subscription ID. 0.1.0+ <resourceGroup> Azure Resource Group for your Web App. 0.1.0+ <appName> The name of your Web App. 0.1.0+ <region> false Specifies the region to host your Web App; the default value is centralus. All valid regions at Supported Regions section. 0.1.0+ <pricingTier> false The pricing tier for your Web App. The default value is P1v2 for production workload, while B2 is the recommended minimum for Java dev/test. For more information, see App Service Pricing 0.1.0+ <runtime> false The runtime environment configuration. For more information, see Configuration Details. 0.1.0+ <deployment> false The deployment configuration. For more information, see Configuration Details. 0.1.0+

    For the complete list of configurations, see the plugin reference documentation. All the Azure Maven Plugins share a common set of configurations. For these configurations see Common Configurations. For configurations specific to App Service, see Azure Web App: Configuration Details.

    Be careful about the values of <appName> and <resourceGroup> (petstoreee7-1690443003536 and petstoreee7-1690443003536-rg accordingly in the demo). They're used later.

    4 - Deploy the app

    With all the configuration ready in your pom.xml file, you can deploy your Java app to Azure with one single command.

    # Disable testing, as it requires Wildfly to be installed locally.
    mvn package azure-webapp:deploy -DskipTests
    

    Once deployment is completed, your application is ready at http://<appName>.azurewebsites.net/ (http://petstoreee7-1690443003536.azurewebsites.net in the demo). Open the url with your local web browser, you should see

    Congratulations! You've deployed your first Java app to App Service.

    5 - Clean up resources

    In the preceding steps, you created Azure resources in a resource group. If you don't need the resources in the future, delete the resource group from portal, or by running the following command in the Cloud Shell:

    az group delete --name <your resource group name; for example: petstoreee7-1690443003536-rg> --yes
    

    This command may take a minute to run.

    Next steps

    Tutorial: Build a Tomcat web app with Azure App Service on Linux and MySQL

    Tutorial: Build a Java Spring Boot web app with Azure App Service on Linux and Azure Cosmos DB

    Set up CI/CD

    Pricing Information

    Aggregate Logs and Metrics

    Scale up

    Azure for Java Developers Resources

    Configure your Java app

     Secure with custom domain and certificate

  •