Posted in java by pedro | Wednesday, July 1st, 2009 at 12:59 pm
New version of maven was released. One of the benefit is that there are many bug fixes and some regression from 2.1.0 was fixed.
The main issue when upgrading is that new maven requires Java 1.5 or later! If you still have system which must be build for Java 1.4 you’ve got trouble to overcome. There is guide how you can configure your pom to use ensure that you do not use API from later version t han suggested.
This is achieved by using animal-sniffer. Animal-sniffer is project which goal is to provide tools for developers which must use many different Java versions. For example I use java 1.4, java 5 and java 6 in my daily work.
Currently there are maven plugin and command line tool. Command line help us to find which class is compiled by which java version, it may help to spot libraries or modules compiled by higher version than our project.
Most helpful is maven plugin which can be configure in such way that it rise build error if we use API from higher java version. Assume the code:
package info.pietrowski;
public class Main {
public static void main(String[] args) {
BigDecimal d = new BigDecimal(10); // this constructor is since 1.5
System.out.println(d);
}
}
Now we configure maven to target for 1.4 java platform, but JAVA_HOME points to java 5 or higher
...
maven-compiler-plugin1.41.4
...
Running this program with different java version drive to the output
$ jdk1.6.0_13/bin/java -classpath test.jar info.pietrowski.Main
10
$ j2sdk1.4.2_16/bin/java -classpath test.jar info.pietrowski.Main
Exception in thread "main" java.lang.NoSuchMethodError: java.math.BigDecimal.(I)V
at info.pietrowski.Main.main(Main.java:10)
The problem is that compiler choose BigDecimal constructor based on rt.jar, and despite produce bytecode 1.4 compatible, can run it because rt.jar in java 1.4 do not have this constructor.
The rescue for this problem is animal-sniffer maven plugin which we can configure like this:
My name is Sebastian Pietrowski. I've finished Warsaw University as Master degree. During my studies I started work for merlin.pl. The primary language I use is Java but I have also programmed in Python, Ruby and Scala. I worked as a technical solution architect at merlin.pl. infrastructure when we were moving from PL/SQL to J2EE. I engineering a great performance optimized solution that made the application 10 times faster than requirements and 85 times faster as original solution.
Currently, I am working as a Senior Expert at F.Hoffmann-La Roche to help define future roadmap in design and development of Enterprise software at Roche and Genentech and build adoption for new technologies. I'm continuously mentoring new developers, helping them understand how important test driven development is and empowering them to get better at their daily job. I'm involved in many activities which brings new technologies for better and faster development. You can find more details on my LinkedIn profile.
But don’t get me wrong, I am not your typical nerd. I'm a pleasant guy that you can drink a glass of wine with me and talk about a range of topics with. My leisure activities include playing basketball, soccer and listening to music. I try to be pragmatic while staying focused on application performance and tuning with success in my daily work.
My favorite quote from Yoda's and my life’s motto is: Do, or do not. There is no try.