Specifying Java version in maven – differences between properties and compiler plugin

I’m not very experienced with Maven and while experimenting with multi-module project I started wondering how can I specify Java version for all my child modules in parent Maven pom. Until today I was using just:

<properties>
    <java.version>1.8</java.version>
</properties>

…but when researching I found that you can also specify Java version in Maven compiler plugin, like that:

<plugins>
    <plugin>    
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
</plugins>

And then wrap this into plugin management tag to enable child poms usage of this. So the first question is this:

What are the differences beetwen setting Java version in properties and in Maven compiler plugin?

I couldn’t find clear answer but in process of researching I found that you can also specify Java version in this way:

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

…which suggest that compiler plugin is there even if I don’t explicit declare it. Running mvn package outputs with

maven-compiler-plugin:3.1:compile (default-compile) @ testproj ---

…and some other plugins that I didn’t declare.

So are those plugins default, hidden part of Maven pom? Are there any differences between setting source/target in properties and in Maven plugin configuration element?

Some other questions are – which way should be used (and when if they are not equal)? Which one is best for multi-module project and what happens if Java version specified in pom is different than version pointed in JAVA_HOME?

6 Answers
6

Leave a Comment