Maven how to set the JDK version?

4

Problem:

When I create a project maven the project always had the version JDK 1.5 , if I change the JDK version in the IDE when doing a Maven > Project Update it returns to JDK 1.5 .

Is the setting.xml file the place where I define the version of all maven projects? If not where I do for all new projects always use the JDK 1.7 version for example.

Note: I 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 advantage of this code is that each Maven project can work with a specified Java version. The disadvantage of this code is for every new project I have to change the pom.xml .

    
asked by anonymous 18.02.2014 / 20:54

2 answers

4

TL; DR

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

Specifying the Java installation

In addition to the version, you can specify which executable or installation will be used to compile with the <executable> tag.

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 documentation here .

Specifying Java is a good practice

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

A simple example is the constructor of class BigDecimal that receives an integer. It was added in Java 5, but it is compiled seamlessly in compatibility for Java 1.4. Obviously, when running the code a run-time error occurs, I just can not remember exactly which class of exception.

No need to repeat yourself

As for the disadvantage of having to configure for each new project, create a < in> parent pom in an empty project (with the exception of pom) and use inheritance to reuse this setting in all your projects.

Here's how to specify the parent:

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

This gives us tremendous productivity gains!

    
18.02.2014 / 21:09
2

Eduardo, the solution is the same, configure the compilation 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 IDE, so any configuration must be done in pom.xml . And in Eclipse, when you make a Maven > Project Update the effect is just the opposite. It updates the project settings based on pom.xml .

Now, if you have lots of projects and they all use Maven, it's a good practice to have parent for all of them, where you can define everything that is common to all projects. You can read more about 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>
    
18.02.2014 / 21:08