Monthly Archives: December 2015

Maven: Creating deployable distribution: Part 2: creating fat jar

In the Part 1 of this, we had seen how to use the appassembler plugin to create a distribution tar. In this increment, we will see how to use the Maven assembly plugin to create a uber jar or fat jar. A uber jar or fat jar is a single jar having the contents of all the dependencies needed for the project. Again, we will be using the maven-distribution-example as reference. This project would need the project rmi-service-api to compile. We have created a profile called uber-jar:

The assembly plugin uses a pre-fabricated descriptor called jar-with-dependencies. This would condense all dependencies into the single fat jar.

Note there are 2 executions. The first one creates the uber jar and the second one takes that uber jar and the readme.txt and creates a zip file. It does this by using the below assembly descriptor (uber-jar-assembly-descriptor.xml):

The uber jar can be generated by:

mvn clean package -P uber-jar

The sources can be found here: https://github.com/paawak/blog/tree/master/code/maven-ant-assembly-example

Pretty print XML with Java

I have the following XML document all, in a single line:

All I need is, to pretty print it with nice tabs and all. This is the code:

This would nicely format it as below:

However, please note that it fails if there are spaces within the tags.

The source can be found here: https://github.com/paawak/blog/tree/master/code/pretty-print-xml

[Jackson] Json to Java Conversion with Ploymorphism

Converting Json String to Java Object is never straight forward, especially when polymorphism is involved. Consider the below interface:

It has two sub classes:

We now will try to convert a Json String to a Java Object:

The full test can be found here: https://github.com/paawak/blog/blob/master/code/json-inheritance-demo/src/test/java/com/swayam/demo/json/polymorphic/noannotation/PolymorphicJsonTest.java

This will immediately throw the below exception:

The reason is, the Json String does not have any information about the specific subclass. This can be very easily overcome by forcing the type information while serializing Java to Json, by using the @JsonTypeInfo annotation. This should be placed on the interface or the abstract class as shown below:

Now, the below code would work:

The full test can be found here: https://github.com/paawak/blog/blob/master/code/json-inheritance-demo/src/test/java/com/swayam/demo/json/polymorphic/withannotation/PolymorphicJsonTest.java

The entire source code for this can be found here: https://github.com/paawak/blog/tree/master/code/json-inheritance-demo

 

[Sorting] IllegalArgumentException in TimSort

Sometimes, simple things like sorting are not simple. We were doing a sorting on a large data set containing around 2000 elements. Sometimes, when most of these elements had null fields, we would get a very weird exception:

Did a bit of googling, and found out this happens when the sorting implementation is not transitive.

The problem that we faced is really, reproducing this consistently, as it seemed to work in most cases and then starts failing seemingly randomly.

In this entry, I will try to reproduce this issue. Let us consider the below entity:

 

We would use the below comparator for sorting this:

 

We will try to sort a List<Person> with the above comparator:

 

This would fail consistently with the below data set:

 

The full test case is here:

https://github.com/paawak/blog/blob/master/code/sort/src/test/java/com/swayam/demo/sort/PersonComparatorNonTransitiveTest.java

Now, consider the below comparator:

 

The difference between the transitive and the non-transitive version is obvious. The real challenge here is having a data-set that fails consistently to cause the exception in TimSort.

The entire source can be found here: https://github.com/paawak/blog/tree/master/code/sort