Maven how to define JDK version?

Asked

Viewed 8,082 times

5

Problem:

When I create a project maven the project always got the version JDK 1.5, change the JDK version in the IDE when doing a Maven > Project Update he goes back to JDK 1.5.

The archive setting.xml is the place where I define the version of all Maven projects? If not where do for all new projects always use the version JDK 1.7 for example.


Observing: found a way to solve the problem using the file pom.xml.

Code:

<!-- CONFIGURAR VERSÃO DO JAVA PARA 1.7 -->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Advantage and Disadvantage

I understand that the head start of this code is that each Maven project can work with a specified Java version. A downside this code is for each new project I have to change the pom.xml.

2 answers

5


TL;DR

Your solution is correct, that is, using the Maven Compiler Plugin to specify the Java version of the project.

Specifying the installation java

In addition to the version, it is possible to specify which executable or installation will be used for compiling with the tag <executable>.

Example:

<!-- CONFIGURAR VERSÃO DO JAVA PARA 1.7 -->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <executable><!-- path-to-javac --></executable>
            </configuration>
        </plugin>
    </plugins>
</build>

See the documentation here.

Specifying Java is good practice

This type of configuration is important because there are cases where a code compiled with a later version of Java, even in compatibility mode, does not run in an earlier version. Do not believe?

A simple example is the class constructor BigDecimal that receives an integer. It was added in Java 5, but is compiled smoothly in compatibility for Java 1.4. Obviously, when executing the code an error occurs at runtime, I just can’t remember exactly what class the exception is.

No need to repeat yourself

As for the downside of having to set up for each new project, create a Parent pom in an empty project (with the exception of pom) and use inheritance to reuse this configuration in all your projects.

See how to specify the parent:

<parent>
    <groupId>org.minhaempresa</groupId>
    <artifactId>meu-pom-principal</artifactId>
    <version>1.0.0</version>
</parent>

This gives us a tremendous productivity gain!

  • Thanks for the clarification, I thought it would have a universal form and that the file setting.xml existed for it. I am happy to know that other programmers use this method and that have a way to use the same pom.xml for N projects.

2

Eduardo, the solution is the same, configure the build plugin for the desired Java version.

<!-- CONFIGURAR VERSÃO DO JAVA PARA 1.7 -->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Note that Maven is independent of the IDE, so any configuration should be done on pom.xml. And in Eclipse, when you make one Maven > Project Update the effect is just the opposite. It updates the project settings based on the pom.xml.

Now, if you have many projects and they all use Maven, a good practice is to have Parent for all of them, where you can set everything that is common for all projects. You can read more about projects Parent here. In the case of Java version, the pom.xml of your Parent would look like this:

<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    <plugins>
  </pluginManagement>
</build>
  • Thanks @Miguel Cartagena I will read about Parent projects.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.