Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Maven and Java Multi-Version Modules

DZone's Guide to

Maven and Java Multi-Version Modules

· DevOps Zone
Free Resource

Download “The DevOps Journey - From Waterfall to Continuous Delivery” to learn about the importance of integrating automated testing into the DevOps workflow, brought to you in partnership with Sauce Labs.

Introduction

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
JAVA_HOME %JAVA_HOME_6%

The parent pom.xml

The parent pom.xml defines the global java version settings

<properties> <jdk.version>6</jdk.version> <jdk>${env.JAVA_HOME_6}</jdk> </properties> 

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

<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> <showDeprecation>true</showDeprecation> <showWarnings>true</showWarnings> <executable>${jdk}/bin/javac</executable> <fork>true</fork> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <jvm>${jdk}/bin/java</jvm> <forkMode>once</forkMode> </configuration> </plugin> </plugins> </build> 

If you enjoyed this article, I bet you are going to love my book as well.






The specific module pom.xml

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

<properties> <jdk.version>7</jdk.version> <jdk>${env.JAVA_HOME_7}</jdk> </properties> 

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

Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure, brought to you in partnership with Sauce Labs

Topics:
java ,devops ,maven ,tips and tricks

Published at DZone with permission of Vlad Mihalcea. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}