About Vlad Mihalcea

Vlad Mihalcea is a software architect passionate about software integration, high scalability and concurrency challenges.

Maven and Java multi-version modules

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 NameEnvironment Variable Value
JAVA_HOME_6C:\Program Files\Java\jdk1.6.0_38
JAVA_HOME_7C:\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>

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.

Reference: Maven and Java multi-version modules from our JCG partner Vlad Mihalcea at the Vlad Mihalcea’s Blog blog.
Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

2 Responses to "Maven and Java multi-version modules"

  1. Andrei says:

    Do you really need different JDKs? Can’t you just specify source and target versions and use JDK7?

  2. I really really need different JDKs, that’s an open source trait I cannot escape. When we develop commercial software, the project scope is quite narrow, so it makes sense to benefit from the latest advancements in the Java language. Open source frameworks are designed for larger scopes, and since there are still many platforms that haven’t yet migrated to 1.7, it’s wise to support 1.6 too.

Leave a Reply


four × 9 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close