Different ways to override java version specified in parent pom

8.4k Views Asked by At

I'm using a parent pom which establishes java version to 1.5. In my concrete project I use 1.6 so I was changing the compiler version in eclipse each time I did a Maven Update.

Looking for a solution to this I found some solutions involving overriding the behavior of the parent pom in the child one.

My question is if there are any differences between them and if so, which option should I use. The options I found (perhaps there are more) are:

  • In properties tag: <app.java.version>1.6</app.java.version>
  • In properties tag: <jdk.version>1.6</jdk.version>
  • In configuration tag: <source>${jdk.version}</source>

I'm very new to Maven. Thanks in advance.

3

There are 3 best solutions below

4
On BEST ANSWER

Properties are just properties, which do not mean much unless you use them somewhere.
The important thing is that you set the version in the maven-compiler-pluginconfiguration:

<properties>
    <jdk.version>1.6</jdk.version>
</properties>

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
            <source>${jdk.version}</source>
            <target>${jdk.version}</target>
        </configuration>
    </plugin>
</plugins>
0
On

You definitely want to go with:

<properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
</properties>

It is the de facto standard Maven property for setting up Java version and it's used not only by maven-compiler-plugin but also by other standard Maven plugins (including reporting ones), so this applies your Java version globally, not only for compiling classes.

1
On

If your parent pom uses 1.7 jdk and you want child pom to use 1.6 java version. combine.self="override" is important use following code:

<build>
    <pluginManagement>
    <plugins>           
        <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration combine.self="override">
      <verbose>true</verbose>
      <fork>true</fork>
      <executable>/usr/lib/jvm/java-1.6.0-openjdk-amd64/bin/javac</executable>
      <compilerVersion>1.6</compilerVersion>
    </configuration>
  </plugin>
    </plugins>
</pluginManagement>
</build>