Maven and Java multi-version modules


Usually, a project has a minimum Java version requirement and that applies to all of its modules. But every rule has its exceptions, as recently I stumbled on the following issue.

One open source project of mine mandates Java 1.6 for most of its modules, except one requiring the 1.7 version.

This happens when integrating external libraries having different Java requirements than your own project.

Because that one module integrates the DBCP2 framework (supporting at least Java 1.7), I need to instruct Maven to use two different Java compilers.

Environment variables

We need to define the following environment variables

Environment Variable Name Environment Variable Value
JAVA_HOME_6 C:\Program Files\Java\jdk1.6.0_38
JAVA_HOME_7 C:\Program Files\Java\jdk1.7.0_25

The parent pom.xml

The parent pom.xml defines the global java version settings


We need to instruct both the compiler and the test plugins to use the configured java version.


The specific module pom.xml

Those modules requiring a different java version, just need to override the default settings:


And that’s it, we can now build each modules using its own specific minimum java version requirement.

If you liked this article, you might want to subscribe to my newsletter too.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s