Deploying a Dropwizard Application - RESTful Web Services with Dropwizard (2014)

RESTful Web Services with Dropwizard (2014)

Appendix B. Deploying a Dropwizard Application

Throughout this book, we have demonstrated and used the most important parts of a Dropwizard project. Our application is now ready, production ready. It is ready to be deployed on a server from where it can be accessed by everyone through the Internet.

Preparing the application for deployment

As you may have guessed, our application does not have many dependencies. Just check for your pom.xml file and look for the section where maven-compiler-plugin is declared.

<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">

<modelVersion>4.0.0</modelVersion>

<groupId>com.dwbook.phonebook</groupId>

<artifactId>dwbook-phonebook</artifactId>

<packaging>jar</packaging>

<version>1.0-SNAPSHOT</version>

<name>dwbook-phonebook</name>

<url>http://maven.apache.org</url>

<!-- Maven Repositories -->

<repositories>

<repository>

<id>sonatype-nexus-snapshots</id>

<name>Sonatype Nexus Snapshots</name>

<url>http://oss.sonatype.org/content/repositories/snapshots</url>

</repository>

</repositories>

<!-- Dependencies -->

<dependencies>

<dependency>

<groupId>io.dropwizard</groupId>

<artifactId>dropwizard-core</artifactId>

<version>0.7.0-SNAPSHOT</version>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.6</version>

</dependency>

<dependency>

<groupId>io.dropwizard</groupId>

<artifactId>dropwizard-jdbi</artifactId>

<version>0.7.0-SNAPSHOT</version>

</dependency>

<dependency>

<groupId>io.dropwizard</groupId>

<artifactId>dropwizard-client</artifactId>

<version>0.7.0-SNAPSHOT</version>

</dependency>

<dependency>

<groupId>io.dropwizard</groupId>

<artifactId>dropwizard-auth</artifactId>

<version>0.7.0-SNAPSHOT</version>

</dependency>

<dependency>

<groupId>io.dropwizard</groupId>

<artifactId>dropwizard-views-mustache</artifactId>

<version>0.7.0-SNAPSHOT</version>

</dependency>

<dependency>

<groupId>io.dropwizard</groupId>

<artifactId>dropwizard-assets</artifactId>

<version>0.7.0-SNAPSHOT</version>

</dependency>

<dependency>

<groupId>io.dropwizard</groupId>

<artifactId>dropwizard-testing</artifactId>

<version>0.7.0-SNAPSHOT</version>

</dependency>

<dependency>

<groupId>org.hamcrest</groupId>

<artifactId>hamcrest-all</artifactId>

<version>1.3</version>

</dependency>

</dependencies>

<!-- Build Configuration -->

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.1</version>

<configuration>

<source>1.7</source>

<target>1.7</target>

<encoding>UTF-8</encoding>

</configuration>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-shade-plugin</artifactId>

<version>1.6</version>

<configuration>

<filters>

<filter>

<artifact>*:*</artifact>

<excludes>

<exclude>META-INF/*.SF</exclude>

<exclude>META-INF/*.DSA</exclude>

<exclude>META-INF/*.RSA</exclude>

</excludes>

</filter>

</filters>

</configuration>

<executions>

<execution>

<phase>package</phase>

<goals>

<goal>shade</goal>

</goals>

<configuration>

<transformers>

<transformer

implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">

<mainClass>com.dwbook.phonebook.App</mainClass>

</transformer>

</transformers>

</configuration>

</execution>

</executions>

</plugin>

</plugins>

</build>

</project>

All that should be present on the server is the Java Runtime Environment of the version that is equal or greater to the one specified in the <target> element of the build plugin's configuration section.

How to do it…

Once we confirm that our dependencies (the Java versions) are satisfied, we can upload the JAR file through an FTP and run the application in the same way as we already do:

$ java -jar <applicationFilename.jar> server <configFileName.yaml>

How it works…

In our pom.xml file, we have all the required Maven parameters declared along with maven-shade-plugin, which allows us to build a single JAR file that includes all the third-party modules and libraries our application uses. Just remember to upload your config file on the server as well or create a new one with a possibly different setting, such as database connection details.

There's more…

There are many good reasons why you may wish to change the default port of your application from 8080 to something else.

This can be achieved with just a few additions to your configuration file: config.yaml. However, in order for these settings to work, we will need to add ServiceResourceTransformer in the build configuration by adding the following entry in the pom.xml file, within the<transformers> section: <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>.

Add the section server and configure its properties as shown in the following code:

server:

applicationConnectors:

- type: http

# The port the application will listen on

port: 8181

adminConnectors:

- type: http

# The admin port

port: 8282

Multiple configuration files

A good practice is to maintain different sets of configuration files (YAML) for your application per environment. For instance, you will probably be using different databases for test and production environments, and it's better to keep the connection information in different files. In addition, you may want to have a more verbose log level on your development or test environment than in production. Depending on the nature and the complexity of your application, there would for sure be many additional reasons that you and your application would benefit by. Luckily, Dropwizard offers many settings that can be tweaked to match your application's needs.