Creating a Maven profile to compile using a different JDK version tutorial

Profiles are very handy in maven to use different JDK versions, include different files for different environments, etc.

Step 1: Say you have a pom.xml file using Java 7 to compile.

Step 2: Say now you want to compile using Java 1.5.

Why would you need to do that?

For example, you want to build the war or ear file using Java 7, but for the assembly plugin, which creates a .zip file to be used as a batch client, This batch client needs to run in an environment, which has Java 5. This where Maven profiles come in handy to compile the batch client with a different version of Java.

Maven Profiles

can accomplish this.

Step 3: So, if you run it as

it uses Java 7. if you run it with “-P batch“, where “batch” is the profile id in the pom.xml.

it uses Java 5. As you can see the profile overrides the Java version properties.

You can create any number of profiles, and use the id with the “-P” when you run the mvn command.

Q. How many different ways can you build profiles?
A. 3 different ways.

1) Per Project: Defined in the project POM file, pom.xml. As shown above.
2) Per User: Defined in Maven settings xml file (%USER_HOME%/.m2/settings.xml). %USER_HOME% represents user home directory.
3) Global: Defined in Maven global settings xml file (%M2_HOME%/conf/settings.xml)

Practical uses of Maven profiles

1) Say your project has several developers on site and offshore. For different reasons, some of the developers are using Windows and some are using Linux. The team can develop two profiles, one for Linux and one for Windows, setting project root paths, temporary folder paths, support program paths. These profile settings can be put into the settings.xml file for each programmer.

2) A project using a continuous integration server like Hudson or Jenkins needs to have its own database for integration testing. So, you can have profiles like “dev-test”, “prod-test” and “full-test”.

300+ Java Developer Interview Q&As

800+ Java Interview Q&As