Maven: Build a executable JAR

Prepare java deploy package

In true microservice architecture we need to prepare self sufficient applications which must be very easy to deploy. For java server app this means executable JAR file with all dependencies included.

Because I use spring boot application my application already is self sufficient. I only need to prepare a combined JAR with all dependencies.


In the maven pom.xml file I add build plugin:

After executing a maven “package” lifecycle command, maven will produce combined JAR with everything inside in the target folder :

Now I can run whole server app with simple jar command:

Because this executable jar include everything, web server, drivers, configurations etc, it will immediately serve REST services to the world.

Such an application could be deployed to any virtual machine instance locally or on the cloud.

You can get source code of this project from github repository “SpringBootMyApp” .


Web compression on Spring Boot application

Compression of JSON messages

If you look at common JSON message, the data are highly repetitive ! Each array of objects contains meta data for all object properties. If all of your messages are short you probably won’t need a compression but in typical scenario where REST services return data from database you gain a lot.

In the message of this size (220 bytes) there is no gain at all (compressed size is 250 bytes) and you lost some processor time to compress it. It is total waste of resources, so you have to limit compression above 2 kb for example.

Lets find out how much difference we make with a compression in the real world example

For the test we will get JSON message from publicly accessible site and check the message size. The size of received message in uncompressed JSON format is  951 kb.

Now I will check in network sniffer how big this message really was : The message on the TCP level is only 170 kb ! If you look at the message payload (down right), what was really on the wire is unreadable compressed data. You can get more information here about Capsa network analyzer freeware product used in this measurement.

It means that this type of messages were compressed to less then 20% of the original size. It means at least 5 times smaller network traffic as without compression.  The compression ratio could be even higher, much higher, if your specific messages are not so full of unique data and that’s happening a lot in ERP type applications.

Enable compression in Spring boot application

Add this settings to file:

How to check if compression is enabled

Check for a specific statement about content encoding in the response header :

To inspect messages in JSON format I use Postman API development environment, free edition.

This is the link to the source code of the used project on the github.


Java Spring Boot project setup

You can get source code of this project from github repository “SpringBootMyApp” .

Create Spring Boot Maven project

Go to Spring Boot project generator web site and select minimal project definition with dependencies :

  • Web:  dependency for embedded tomcat server
  • jOOQ: integrated SQL query language and data model code generator
  • Flyway: database model migration tool
  • Postgresql JDBC driver

After downloading ZIP project file, unzip it to some folder and open folder in IntelliJ IDE environment.

Startup class

The project will automatically include embedded tomcat server and spring application start will configure whole application at startup.

Setup project properties

Under src/main/resources find file and add configuration for database access  and flyway configuration. Your application will start migration procedure at every application start and syncronize to the correct version automatically.

Flyway migration files

You need a folder where your SQL migration files will reside:

Add first migration file, for example “create currency table” will be saved in file V1_0_0__currency.sql:

Be extra careful with names , first part (V1_0_0) is version of migration file, second part of the name is a description, separated with two underline characters.

The scripts must be correct SQL! Validate it first in database environment. If script is not valid, migration procedure will break and you will probably have hard time to figure out what was wrong.

Now you run your application and your database must be prepared according to migration script changes automatically. Flyway start automatically with your application.

More information on this video tutorial for flyway and spring boot..

Maven plugin settings

If you want more control around code generation and migration life cycle, you add flyway and jooq plugins:

Add this properties to the properties section in pom.xml file:

  • Database connection settings in pom.xml will be used by jooq code generator and flyway migration maven plugins.

Flyway and Jooq dependency and version

You probably already have it from generated pom.xml, add it if you don’t have it.

Version was not required by spring boot but I put version in because I encounter differences between plugin version and spring boot version of the libraries.

Now you need to define plugin definitions as :

Flyway and Jooq maven commands

After you add plugin definitions to maven pom.xml file you get new lifecycle commands :

When flyway commands are used directly they search for SQL files in the target “classes” folder and not in the source tree (“src”). When project compiled the files, target folder is synchronized with the current version of code in the source tree.

After compiling project you could run “flyway:migrate” command for example. You can always check files in the target folder and delete it if you are not sure you have the latest version of the files.

Jooq code generation

Jooq generate data model code from your database. The code is added to the project in the namespace as you defined in plugin definition.  It’s wise to split plugin definition to properties and plugin definition part.

Every time you compile project, database model code is regenerated. Check here for more information about jooq.

You can check how database model code is regenerated simply by deleting a line from one of generated files and recompile the project again.

More about jooq & spring boot can be found here.

Create JSON service

Now I need to test the environment if everything is set in place as should be. I will create simple currency service as sample application and test basic CRUD operations.

Create simple data container class

To be able to send JSON payload back and forth from client to server we create a simple transport class:

Service class and database interaction with Jooq

Now we create service class with which we will interact with database :

REST controller with router mapping

On the end we need a controller class to expose access points for REST service:

Don’t forget to instantiate services with DI (dependency injection) with @Autowire annotation. This simplify development a lot !

Interactive testing

The service should in this point work as expected, just run application and navigate to “localhost:8080/api/currency”.

You can search for a specific currency with added path variable appended to Url address:

Postman application for Http API testing

To inspect service in more details on the client side, you can use Postman application.

Look at example ofadding a currency with a POST message:

In the headar I add “content-type” variable with value “application/json” and in the payload a json message with a new currency json structure. On the right side I received “200-OK” and a currency record with a record and new row identifier registered in the database.