Publishing a Java Servlet to Azure Website Using Maven

Azurelogo

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:

mvn archetype:generate -DgroupId=com.example -DartifactId=hello-world -DarchetypeArtifactId=maven-archetype-webapp

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:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <build>
    <plugins>
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.2.0.M0</version>
      </plugin>
    </plugins>
  </build>
</project>

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:

  1. Click New: Website.
  2. Give the website a name (I’ll use hello-world) and then click Create.
  3. Open the website and click Site settings (in the lower left-hand corner).
  4. For Java version, change the setting from OFF to 1.7.0_51.
  5. In Web Container, choose your preferred servlet container (I use Tomcat).
  6. 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:

  1. Open the website in the Azure Portal.
  2. Click Set deployment credentials.
  3. Create a deployment username (I’ll use deployme) and password and click Save. Write down the username and password
  4. Click Properties
  5. 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 a hello-world prefix (i.e. it is hello-world\deployme)

We’ll now configure Maven to deploy the website by uploading the WAR file when the user uses mvn install.

  1. Set up the deployment username and password for Maven by editing ~/.m2/settings.xml and add the following section:
    <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0                               http://maven.apache.org/xsd/settings-1.0.0.xsd">
      <servers>
        <server>
          <id>azure-hello-world</id>
          <username>hello-world\deployme</username>
          <password>password</password>
        </server>
      </servers>
    </settings>
    
  2. Add wagon-ftp as a build extension to your pom.xml so we can upload files via FTP:
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <build>
        <extensions>
          <extension>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-ftp</artifactId>
            <version>1.0-beta-3</version>
          </extension>
        </extensions>
      </build>
    </project>
    
  3. Add wagon-maven-plugin as a build plugin in your pom.xml and instruct it to upload the WAR via FTP when the user runs mvn install:
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>wagon-maven-plugin</artifactId>
            <version>1.0-beta-3</version>
            <executions>
              <execution>
                <id>upload-war</id>
                <phase>install</phase>
                <goals>
                  <goal>upload</goal>
                </goals>
                <configuration>
                  <fromDir>${basedir}/target</fromDir>
                  <includes>${project.build.finalName}.war</includes>
                  <url>ftp://waws-prod-blu-011.ftp.azurewebsites.windows.net</url>
                  <toDir>/site/wwwroot/webapps</toDir>
                  <serverId>azure-hello-world</serverId>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </project>
    

You can now deploy your website using mvn install, and visit it at http://hello-world.azurewebsites.net/hello-world.

About Steven Engelhardt, CFA, AIF
Adjunct Professor of Software Engineering at DePaul University • Software Engineering, Data & Analytics in FinTech • Lives in Chicago, IL

One Response to Publishing a Java Servlet to Azure Website Using Maven

  1. Aldrin Leal says:

    I’ve written a more polished example using git + Maven (+ Dropwizard) here:

    https://github.com/aldrinleal/azurator-sample

    There’s also a video:

    Its still a proof of concept, but I believe the approach is more flexible 🙂

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s