This blog post shows how to publish a Java Servlet, encapsulated in a WAR file, to an Azure Web Site using FTP via Maven.
The first step will be to generate a simple servlet using Maven:
|
|
This creates a simple, Hello World application in the hello-world directory. We can verify it works by running it in a local servlet container using the instructions found in Supporting mvn jetty:run in Maven applications. Add the Jetty maven plugin to pom.xml under the build section:
|
|
You can now run the application in Jetty using mvn jetty:run
and visit http://localhost:8080 to ensure it works. Next, you can use mvn package
to generate the WAR file target/hello-world.war
.
Now let’s switch to the Microsoft Azure Portal to create a Web Site and configure it to run Java applications:
- Click New: Website.
- Give the website a name (I’ll use
hello-world
) and then click Create. - Open the website and click Site settings (in the lower left-hand corner).
- For Java version, change the setting from OFF to 1.7.0_51.
- In Web Container, choose your preferred servlet container (I use Tomcat).
- Click Save.
Your Java website is now running at http://hello-world.azurewebsites.net. You can upload WAR files via FTP to the directory /site/wwwroot/webapps
and they will become available as part of your site.
Next, set up your website for deployment:
- Open the website in the Azure Portal.
- Click Set deployment credentials.
- Create a deployment username (I’ll use
deployme
) and password and click Save. Write down the username and password - Click Properties
- Write down the FTP host name. Mine is
ftp://waws-prod-blu-011.ftp.azurewebsites.windows.net
. Also note that the FTP/Deployment User has ahello-world
prefix (i.e. it ishello-world\deployme
)
We’ll now configure Maven to deploy the website by uploading the WAR file when the user uses mvn install
.
- Set up the deployment username and password for Maven by editing
~/.m2/settings.xml
and add the following section:
|
|
- Add
wagon-ftp
as a build extension to yourpom.xml
so we can upload files via FTP:
|
|
- Add
wagon-maven-plugin
as a build plugin in yourpom.xml
and instruct it to upload the WAR via FTP when the user runsmvn install
:
|
|
You can now deploy your website using mvn install
, and visit it at http://hello-world.azurewebsites.net/hello-world.