有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在Spring/Spring Boot pom中指定Java 11版本。xml?

指定Java11作为Spring(或Spring Boot)pom中使用的版本的正确方法是什么。xml文件

也就是说,我需要在pom的java.version属性中输入什么值

对于Java8,我们使用1.8,就像the documentation shows,对于Java9,它是1.9

我不确定Java 11是否是1.11(尽管看起来不太可能),我看到在使用maven-compiler-plugin时它被指定为11,但是我没有使用编译器插件

例如

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <release>11</release>
    </configuration>
</plugin>

我四处搜索,找不到答案,因为我读过的几乎所有文档以及与Java版本相关的博客帖子都只显示版本8或9

那么我应该用什么呢1.11还是{}

我尝试了这两种方法,我的web服务器似乎启动正确,编译正确(IntelliJ显示Information:javac 11.0.2 was used to compile java sources),但我不完全相信更改java.version值有什么用,因为我可以将其设置为例如100,一切都正常

我能找到的唯一相关问题是:Minimum Spring version compatible with Java 11Spring Boot fails due to a Hibernate error after migrating to JDK 11Spring 4.3.x with OpenJDK 11,但它们并没有真正阐明什么


共 (4) 个答案

  1. # 1 楼答案

    简短回答:

    正确的方法是对不同的Java版本使用<java.version>中的以下值:

    • Java 8:1.8或8
    • Java 9:9
    • 爪哇10:10
    • 爪哇11:11
    • 爪哇12:12

    所以对于Java 11,它应该是:

    <properties>
       <java.version>11</java.version>
    </properties>
    

    However I'm not sure if Java 11 would be "1.11" (seems unlikely), and I've seen it specified as just "11" when using maven-compiler-plugin, however I'm not using the compiler plugin.

    实际上,最后它仍然使用maven-compiler-plugin进行编译。Springboot只是配置了一个<java.version>属性,这样通过更改这个值,您就隐式地将maven-compiler-plugin<source/><target/>更改为与<java.version>中指定的相同的值:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
            <source>11</source>  <!-- same as <java.version> -->
            <target>11</target>    <!-- same as <java.version> -->
        </configuration>
    </plugin>
    

    详细答案:

    似乎你想要细节来说服你

    这是因为每个spring boot项目都将扩展父pom spring-boot-starter-parent,其defines{}如下所示:

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>
    

    从maven编译器插件docsmaven.compiler.sourcemaven.compiler.target<source><target>配置参数的user property。由于用户属性的行为,将这两个属性设置为11意味着设置以下内容:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
            <source>11</source>   <!-- maven.compiler.source  -->
            <target>11</target> <!-- maven.compiler.target -->
        </configuration>
    </plugin>
    

    maven-compiler-plugin{a2}中,<source><target>是Java编译器(-source-target)的参数。然后,从javac文档中,我们可以看到这两个参数允许有以下值:

    • 1.6 : No language changes were introduced in Java SE 6. However, encoding errors in source files are now reported as errors instead of warnings as was done in earlier releases of Java Platform, Standard Edition.
    • 6 : Synonym for 1.6.
    • 1.7 : The compiler accepts code with features introduced in Java SE 7.
    • 7 : Synonym for 1.7.
    • 1.8 : The compiler accepts code with features introduced in Java SE 8.
    • 8 : Synonym for 1.8.
    • 9 : The compiler accepts code with features introduced in Java SE 9.
    • 10 : The compiler accepts code with features introduced in Java SE 10.
    • 11 : The compiler accepts code with features introduced in Java SE 11.
    • 12 : The compiler accepts code with features introduced in Java SE 12.

    因此,对于Java11,<java.version>应该设置为11

  2. # 2 楼答案

    我将其添加为一个答案,以便对更多人有所帮助:

    由于Java 9,这些版本被指定为91011等等,而不是1.81.7等等

    使用<release>11</release>指定JDK版本的方式是正确的。它是用maven-compiler-plugin 3.6引入的,与旧方法等效,如下所示:

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>11</source> -- Using JDK 11 to compile
          <target>11</target> -- Using JDK 11 as target
        </configuration>
      </plugin>
    </plugins>
    

    Thispost可以帮助将maven-compiler-plugin与JDK 11一起使用

  3. # 3 楼答案

    您不需要在POM文件中提到maven编译器插件, 如果只想指定编译器版本。 相反,将这些属性添加到pom文件中

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

    编辑:我的错, 从maven编译器插件的3.6版本开始,版本号属性似乎发生了变化。 上面的例子适用于Java8及更低版本。 对于java 9, 添加此属性:

    <properties>
        <maven.compiler.release>9</maven.compiler.release>
    </properties>
    

    查看这个问题的公认答案:Specifying java version in maven - differences between properties and compiler plugin

  4. # 4 楼答案

    如果有人希望在gradle项目中将Java 11指定为Spring(或Spring Boot)中使用的版本,那么在构建中使用sourceCompatibility='11'或sourceCompatibility='1.11'。格雷德尔锉刀

    plugins {
    id 'org.springframework.boot' version '2.1.4.RELEASE'
    }
    
    apply plugin: 'io.spring.dependency-management'
    
    group = 'au.com.ranuka'
    version = '0.0.1-SNAPSHOT'
    
    sourceCompatibility = '11'
    //or 
    sourceCompatibility = '1.11'
    
    repositories {
       mavenCentral()
    }
    
    dependencies {
    
    }