In the previous Gradle series post we built Wrapper to execute Gradle tasks without installed version on your machine. It’s time to write some code and make our project useful. In this post Gradle will help us to start a web server.

To make it happened we need:

Dropwizard

All the aspects related with providing readable RESTful API via reliable Web Server are covered by Dropwizard. These folks give all of these nice features for us:

  • Jetty – lighweight Java Web Server
  • Jersey – building RESTful web applications (JAX-RS implementation)
  • Jackson – parsing JSON data format
  • Metrics – measure app performance
  • and more!

To start using all of above features you must add Dropwizard core as a dependency to the project:

Configuration

It stores all the project and environment related settings. Our simple configuration contains just one field: appName

@NotEmpty annotation will assure that application will not start if appName value will be not defined.

@JsonProperty allows to deserialise and serialise from a YAML property.

Let’s add YAML configuration file with property appName and value equals ‘Clean code rulez!

I placed config file in the new directory config at the top project level.

Application

The last Dropwizard part is to create a new Application class to start web server with simple logic.

This class is using our Configuration class and is a main entry point to the application. To run the app we just need a main method to start a run method which starts a web server as a process.

During spin on the Dropwizard we should see appName value in the logs:

LOGGER.info("Application name: {}", configuration.getAppName());

To start web server I will use Gradle plugin.

Gradle Application plugin

Gradle provides Application plugin to create an executable JVM application. This plugin contains java and distribution plugins.

At this moment we can replace standard java plugin with application plugin.

apply plugin: 'application'

After refresh you should see 2 new sections on Gradle tasks list: Application and Distribution

Application tasks
-----------------
installApp - Installs the project as a JVM application along with libs and OS specific scripts.
run - Runs this project as a JVM application

Distribution tasks
------------------
assembleDist - Assembles the main distributions
distTar - Bundles the project as a distribution.
distZip - Bundles the project as a distribution.
installDist - Installs the project as a distribution as-is.

We will use run task to start application. This task needs full name of the main class to execute and execution parameters:

mainClassName = "com.karollotkowski.cleancode.CleanCodeApplication"
project.ext {
    configPath = "$rootProject.projectDir/config/"
}
run {
 args 'server', configPath + 'app_config.yml'
}

Full build.gradle file with Dropwizard and Gralde plugin settings:

Spin on web server

All the stuff is ready to start a server. Execute run Gradle task from cmd or IDE:

$ ./gradlew run

Output should looks similar to this:

As you see there is an appName variable value:

INFO  [2015-10-13 22:28:52,297] com.karollotkowski.cleancode.CleanCodeApplication: Application name: Clean code rulez!

Dropwizard started two services:

Advertisements