3

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.

kryger
  • 12,906
  • 8
  • 44
  • 65
javi_swift
  • 377
  • 6
  • 16

3 Answers3

6

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.

Michał Kalinowski
  • 16,925
  • 5
  • 35
  • 48
4

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>
Kuurde
  • 887
  • 2
  • 8
  • 24
  • 1
    Exactly. What you name your property doesn't matter. They're just like variables. Once you declare a property which represents your JDK version, e.g. `myJdkVersionPropertyName`, you reference it in the `configuration` tag of the `maven-compiler-plugin` by the same name, e.g. `${myJdkVersionPropertyName}`. The `source` and `target` elements of this tag are what actually set the JDK version. – Zoltán Jun 18 '15 at 09:14
  • You both are right. I hadn´t understood the meaning of "property". My parent pom was defining to 1.5 so overriding the value on this property did the trick. Thanks!! – javi_swift Jun 18 '15 at 09:18
  • I'm not familiar with Maven but am on a project managed by Maven. Where would this content be place? Name of file/folder location, I don't see this in the pom.xml. – edjm Aug 11 '15 at 12:43
  • This would go in the pom.xml file. Maybe your project is set up like the answer below from Michal Kalinowski? Otherwise it will fall back to the default java version which is 1.5 – Kuurde Aug 12 '15 at 07:57
0

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>