Java Web Apps with Visual Studio Code
This tutorial shows you how to create a Java web application with Visual Studio Code. You'll learn how to deploy a Java web application to a Linux Tomcat server in Azure App Service.
Scenario
A simple Hello World web app.
Before you begin
Before running and deploying this sample, you must have the Java SE Development Kit (JDK) and Apache Maven build tools on your local development environment. If you don't have, please install them.
Download and install theExtension Pack for Java, which has JDK 11 included.
Note: The
JAVA_HOME
environment variable must be set to the install location of the JDK to complete this tutorial.
Download Apache Maven version 3 or greater:
Install Apache Maven for your local development environment:
Create a Maven Web App project
maven-archetype-webapp
is an archetype which generates a Maven Web App project. To learn more, you can visitthis documentation.
- In an empty folder, run the following command to generate a new project from a Maven archetype.
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.4
Maven asks you for values needed to finish generating the project on deployment. Provide the following values when prompted:
Prompt Value Description groupId com.webappproject
A value that uniquely identifies your project across all projects, following thepackage naming rules for Java. artifactId webapp-project
A value that is the name of the jar, without a version number. version 1.0-SNAPSHOT
Choose the default value. package com.webappproject
A value that is the Java package for the generated function code. Use the default. Type
Y
or press Enter to confirm.Maven creates the project files in a new folder with a name ofartifactId, which in this example is
webapp-project
.Navigate into the project folder:
cd webapp-project
Deploy Web Apps to the cloud
We just built a Java web application and ran it locally. Now you will learn how to deploy from Visual Studio Code and run it onAzure in the cloud.
If you don't have an Azure subscription, you can sign up for afree Azure account.
Create your free Azure account
Install the Azure App Service extension
TheAzure App Service extension is used to create, manage, and deploy to Azure App Service with key features including:
- Create new Azure Web App/Deployment Slot
- Deploy to Azure Web App/Deployment Slot
- Start, stop, and restart the Azure Web App/Deployment Slot
- View a Web App's log files
- Swap Deployment Slots
To install the Azure App Service extension, open the Extensions view (⇧⌘X (Windows, LinuxCtrl+Shift+X)) and search forazure app service
to filter the results. Select the MicrosoftAzure App Service extension. For a more command-line Maven-centric experience, you can also check out theMaven plugin for Azure App Service Linux tutorial.
Sign in to your Azure subscription
To sign in to Azure, runAzure: Sign In from theCommand Palette (⇧⌘P (Windows, LinuxCtrl+Shift+P)). Or you can sign in to your Azure Account by clickingSign in to Azure... inRESOURCES Explorer.
Create a new Web App on Azure
Once the extension is installed, you can take the following steps to create a new Web App on Azure.
ClickCreate button on theRESOURCES Explorer view and selectCreate App Service Web App....
Enter a unique name for the new Web App.
Select the runtime task of the Web App, for example
Java 17
.Select the Java web server stack, for example
Apache Tomcat 10.0
.Select a pricing tier, for example
Free(F1)
.
Build and deploy to a Web App
The deploy process leverages theAzure Resources extension (installed along with the Azure App Service extension as a dependency) and you need to sign in with your Azure subscription. If you do not have an Azure subscription,sign up today for a free 30 day account and get $200 in Azure Credits to try out any combination of Azure services.
Once you have signed in, you can open the command prompt or terminal window and build the project using Maven commands. This will generate a newwar
orjar
artifact in thetarget
directory.
mvn clean package
After building the project, open thetarget
directory in VS Code Explorer. Right-click on the artifact and chooseDeploy to Web App, and follow the prompts to choose the Web App for your deployment.
Open theOutput window in VS Code to view the deployment logs. Once the deployment is completed, it will print out the URL for your Web App. Click the link to open it in a browser, you can see the web app running on Azure!
Note: For more advanced features of App Service, you can check out theAzure App Service extension.
Clean up resources
To delete your web app, navigate to theRESOURCES Explorer and locate theApp Services item.
Right-click the web app you'd like to delete and clickDelete.
- To delete your app service plan or resource group, visit theAzure portal and manually delete the resources under your subscription.
Next steps
- To containerize and deploy a web application, check out theDocker in VS Code.
- To learn more about Java Debugging features, see theJava Debugging Tutorial.