1
votes

A)Summarize the problem

There are three methods to define compiler version in pom maven. I did not mention maven.compiler release , plugin and version it works (means maven build success)then how it is picking the compiler version and i did not mention version

For The things ihave tried please refer section b

1)

<maven.compiler.target>1.8</maven.compiler.target>
  <maven.compiler.source>1.8</maven.compiler.source>
            <plugin> 
            <groupId>org.apache.maven.plugins</groupId> 
            <artifactId>maven-compiler-plugin</artifactId> 
            <version>3.8.1</version> 
            <configuration> 
                <source>1.8</source> 
                <target>1.8</target> 
                <verbose>true</verbose> 
            </configuration> 
    </plugin>
<maven.compiler.release> 8</maven.compiler.release>

B) What i have tried

I remove both maven.compiler release and plugin it works then how it is picking the compiler version

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>io.flowing.retail</groupId>
    <artifactId>flowing-retail-kafka-shipping</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>

        <spring.boot.version>2.2.5.RELEASE</spring.boot.version>
        <spring-cloud-stream.version>Horsham.RELEASE</spring-cloud-stream.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-stream-dependencies</artifactId>
                <version>${spring-cloud-stream.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-kafka</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <!-- required to add JSR310 time formats for Jackson to ObjectMapper -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-json</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
             <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>              
                <configuration>                         
                      <mainClass>io.flowing.retail.shipping.ShippingApplication</mainClass>
                </configuration>
             </plugin>
             <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                      <goals>
                        <goal>repackage</goal>
                      </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  1. I define invalid maven.compiler.release. It then also works

    4.0.0 io.flowing.retail flowing-retail-kafka-shipping 0.0.1-SNAPSHOT <maven.compiler.release>108</maven.compiler.release> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target>

             <spring.boot.version>2.2.5.RELEASE</spring.boot.version>
             <spring-cloud-stream.version>Horsham.RELEASE</spring-cloud-stream.version>
         </properties>
         <dependencyManagement>
             <dependencies>
                 <dependency>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-dependencies</artifactId>
                     <version>${spring.boot.version}</version>
                     <type>pom</type>
                     <scope>import</scope>
                 </dependency>
                 <dependency>
                     <groupId>org.springframework.cloud</groupId>
                     <artifactId>spring-cloud-stream-dependencies</artifactId>
                     <version>${spring-cloud-stream.version}</version>
                     <type>pom</type>
                     <scope>import</scope>
                 </dependency>
             </dependencies>
         </dependencyManagement>
         <dependencies>
             <dependency>
                 <groupId>org.springframework.cloud</groupId>
                 <artifactId>spring-cloud-starter-stream-kafka</artifactId>
                 <exclusions>
                     <exclusion>
                         <groupId>org.springframework.boot</groupId>
                         <artifactId>spring-boot-starter-web</artifactId>
                     </exclusion>
                 </exclusions>
             </dependency>
             <dependency>
                 <!-- required to add JSR310 time formats for Jackson to ObjectMapper -->
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-json</artifactId>
             </dependency>
         </dependencies>
         <build>
             <plugins>
                  <plugin>
                     <groupId>org.codehaus.mojo</groupId>
                     <artifactId>exec-maven-plugin</artifactId>              
                     <configuration>                         
                           <mainClass>io.flowing.retail.shipping.ShippingApplication</mainClass>
                     </configuration>
                  </plugin>
                  <plugin>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-maven-plugin</artifactId>
                     <executions>
                         <execution>
                           <goals>
                             <goal>repackage</goal>
                           </goals>
                         </execution>
                     </executions>
                 </plugin>
             </plugins>
         </build>
     </project>
    
1
If you use spring boot, set <java.version> and nothing else. - dan1st
The compiler argument -release was introduced in Java 9. Can you edit your question and explain what the problem is? Are you wondering which parameter overrides the other ones? - Piotr P. Karwasz
@dan1st ,There are three methods to define compiler version in pom maven. I did not mention maven.compiler release , plugin and version it works (means maven build success)then how it is picking the compiler version and i did not mention version - Ishan Garg
@PiotrP.Karwasz can u please refer section B of this question i have done some edits , if u kow please answer it will be helpful - Ishan Garg
Just set a property java.version and spring boot will manage source/target/release. - dan1st

1 Answers

2
votes

[Let me first clarify the expression "picking the compiler version" you use in your question. The compiler you use is configured through the compilerId and compilerVersion options and it's usually the compiler of the JDK you installed. We are talking about setting the compatibility options of your compiler.]

The properties you mention are used to provide the -source, -target and --release arguments to the javac compiler (or equivalent if you use another compiler).

Since --release was introduced in Java 9, which one applies depends on the JDK used to run maven. If you provide the --release argument:

  • on JDK 9 and higher, maven will ignore the -source and -target parameters,
  • on JDK 8 and lower, the compilation will fail miserably.

The maven-compiler-plugin specifies three ways to provide these parameters:

  1. in the <configuration> section of a <plugin> tag. This one overrides the others,
  2. as a property in your pom.xml. This one applies if you didn't specify the parameter in a <configuration> section,
  3. if you didn't specify any of the above, a default for -source and -target applies (--release does not have a default).

You can find the defaults in the maven-compiler-plugin.jar (resource META-INF/maven/plugin.xml):

<configuration>
    ...
    <release implementation="java.lang.String">${maven.compiler.release}</release>
    ...
    <source implementation="java.lang.String" default-value="1.6">${maven.compiler.source}</source>
    ...
    <target implementation="java.lang.String" default-value="1.6">${maven.compiler.target}</target>
</configuration>

So, since you are using version 3.8.1 of the plugin, if you don't specify anything at all, Java 6 applies.

TL;DR: if you use Java 9 and higher, your configuration options will apply in the order 3., 2., 1.

Edit: If your compiler supports it, between setting the -source/-target and --release options, I would always choose the latter. With the --release option, you won't have issues like this one. What happened there is: the source syntax was compatible with Java 8 (-source 1.8), the classes could be read by a JVM 8 (-target 1.8), but a method changed signature (in a backward, but not forward compatible way) between Java 8 and 11 and the library would not work with JRE 8.