maven在settings.xml和pom.xml中指定jdk版本编译的方法

 更新时间:2024年05月08日 10:58:25   作者:kfepiza  
在开发Java应用时,通常需要指定要使用的Java版本,下面这篇文章主要给大家介绍了关于maven在settings.xm和pom.xml中指定jdk版本编译的方法,文中通过代码介绍的非常详细,需要的朋友可以参考下

maven的settings.xm和pom.xml都可以通过 maven.compiler.source , maven.compiler.target 这两个属性值来指定jdk版本

  • maven.compiler.source

  • maven.compiler.target

maven.compiler.source
maven.compiler.target

在pom.xml中的位置

<project>
  <properties>
    <maven.compiler.source>21</maven.compiler.source>
    <maven.compiler.target>21</maven.compiler.target>
  </properties>
</project>

在settings.xml中的位置

<settings>
    <profiles>
        <profile>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <maven.compiler.source>21</maven.compiler.source>
                <maven.compiler.target>21</maven.compiler.target>  <!-- JRE System Liblary 的版本和这句相同  -->
            </properties>
        </profile>
    </profiles>
</settings>

在spring项目中, 用java.version来统一设置

maven的settings.xm和pom.xml也可以通过设定 maven-compiler-plugin 这个插件来指定jdk版本

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.9.6</version>
    <configuration>
        <source>21</source>
        <target>21</target>
    </configuration>
</plugin>

在pom.xml中的位置

<project>
  ...
  <build>
    ...
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.9.6</version>
        <configuration>
          <source>21</source>
          <target>21</target>
        </configuration>
      </plugin>
    </plugins>
    ...
  </build>
  ...
</project>

在settings.xml中的位置 , 好像用不了

<settings>
  ...
  <profiles>
    <profile>
      <id>profile-maven-compiler-plugin</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.9.6</version>
            <configuration>
              <source>17</source>
              <target>17</target>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
  ...
</settings>

Maven 在 settings.xml 中指定jdk版本

settings.xml 中的属性写在 setting👉profiles👉profile👉properties中,位于第5层

方法一, 直接写死, 例如指定jdk21

<settings>
    <profiles>
        <profile>
            <id>jdk-version-21</id>
            <!-- id和activation都可以用于激活该profile, 定义id可以在activeProfiles的activeProfile里设置该id从而激活该id代表的profile, id和activation可以只保留一个,也可两个都使用.  -->
            <activation>
                <activeByDefault>true</activeByDefault> <!-- 该profile是否默认激活, 不激活的话, 下面的properties是否默认生效, 这里设为true就能激活该profile从而使属性生效. 下方又用<activeProfile>jdk-version-21</activeProfile>再次确保该profile激活 -->
            </activation>
            <!--要使properties起作用, properties所属的profile必须在激活状态  -->
            <properties>
                <maven.compiler.source>21</maven.compiler.source>
                <maven.compiler.target>21</maven.compiler.target>  <!-- JRE System Liblary 的版本和这句相同  -->
            </properties>
        </profile>
    </profiles>
    <!--  activeProfiles里的activeProfile对应profiles里的profile的id; 是激活profile的方式之一; 在activeProfiles中激活的profile可以不要activation标签了-->
    <!--  activeProfiles与profiles同级是第二级, profile是第三级, settings → activeProfiles → activeProfile  ,  activeProfile可以有多个-->
    <activeProfiles>
        <!-- 要激活的profile的id , 在这里激活了的profile里的activation就无效了,可以去掉,当然也可以保留-->
        <activeProfile>jdk-version-21</activeProfile>  <!-- 要激活的profile的id . 这里和上面该id的profile中的 <activeByDefault>true</activeByDefault> 任一个都能激活该id代表的profile, 两处设置确保启用该profile-->
    </activeProfiles>
</settings>

去掉注释

    <profiles>
        <profile>
            <id>jdk-version-21</id>
            <activation>
                <activeByDefault>true</activeByDefault> <!-- 该profile是否默认激活, 不激活的话, 下面的properties是否默认生效, 这里设为true就能激活该profile从而使属性生效. 下方又用<activeProfile>jdk-version-21</activeProfile>再次确保该profile激活 -->
            </activation>
            <properties>
                <maven.compiler.source>21</maven.compiler.source>
                <maven.compiler.target>21</maven.compiler.target>  <!-- JRE System Liblary 的版本和这句相同  -->
            </properties>
        </profile>
    </profiles>
    <activeProfiles>
        <activeProfile>jdk-version-21</activeProfile>  <!-- 要激活的profile的id . 这里和上面该id的profile中的 <activeByDefault>true</activeByDefault> 任一个都能激活该id代表的profile, 两处设置确保启用该profile-->
    </activeProfiles>

只用 <activeByDefault>true</activeByDefault> 激活, 可以不要 <id>jdk-version-21</id>和 <activeProfile>jdk-version-21</activeProfile>

    <profiles>
        <profile>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <maven.compiler.source>21</maven.compiler.source>
                <maven.compiler.target>21</maven.compiler.target>  <!-- JRE System Liblary 的版本和这句相同  -->
            </properties>
        </profile>
    </profiles>

只用 <activeProfile>jdk-version-21</activeProfile> 激活 , 则可以不要

    <profiles>
        <profile>
            <id>jdk-version-21</id>
            <properties>
                <maven.compiler.source>21</maven.compiler.source>
                <maven.compiler.target>21</maven.compiler.target>  <!-- JRE System Liblary 的版本和这句相同  -->
            </properties>
        </profile>
    </profiles>
    <activeProfiles>
        <activeProfile>jdk-version-21</activeProfile>
    </activeProfiles>

引用属性变量,只在一个地方修设值jdk版本

<settings>
    <profiles>
        <profile>
            <id>set-jdk-version</id>
            <!-- id和activation都可以用于激活该profile, 定义id可以在activeProfiles的activeProfile里设置该id从而激活该id代表的profile, id和activation可以只保留一个,也可两个都使用.  -->
            <activation>
                <activeByDefault>true</activeByDefault> <!-- 该profile是否默认激活, 不激活的话, 下面的properties是否默认生效, 这里设为true就能激活该profile从而使属性生效. 下方又用<activeProfile>jdk-version-21</activeProfile>再次确保该profile激活 -->
            </activation>
            <!--要使properties起作用, properties所属的profile必须在激活状态  -->
            <properties>
                <jdk-version>21</jdk-version> <!--自定义一个属性用来设置版本,之后可以用${该属性名引用},就不用多处修改了-->
                <maven.compiler.source>${jdk-version}</maven.compiler.source>
                <maven.compiler.target>${jdk-version}</maven.compiler.target>  <!-- JRE System Library 的版本和这句相同  -->
            </properties>
        </profile>
    </profiles>
    <!--  activeProfiles里的activeProfile对应profiles里的profile的id; 是激活profile的方式之一; 在activeProfiles中激活的profile可以不要activation标签了-->
    <!--  activeProfiles与profiles同级是第二级, profile是第三级, settings → activeProfiles → activeProfile  ,  activeProfile可以有多个-->
    <activeProfiles>
        <!-- 要激活的profile的id , 在这里激活了的profile里的activation就无效了,可以去掉,当然也可以保留-->
        <activeProfile>set-jdk-version</activeProfile>  <!-- 要激活的profile的id . 这里和上面该id的profile中的 <activeByDefault>true</activeByDefault> 任一个都能激活该id代表的profile, 两处设置确保启用该profile-->
    </activeProfiles>
</settings>

一处设置,双重激活

    <profiles>
        <profile>
            <id>set-JdkVersion</id>
            <activation>
                <activeByDefault>true</activeByDefault> <!-- 该profile是否默认激活, 不激活的话, 下面的properties是否默认生效, 这里设为true就能激活该profile从而使属性生效. 下方又用<activeProfile>JdkVersion-21</activeProfile>再次确保该profile激活 -->
            </activation>
            <properties>
                <JdkVersion>21</JdkVersion> <!--自定义一个属性用来设置版本,之后可以用${该属性名引用},就不用多处修改了-->
                <maven.compiler.source>${JdkVersion}</maven.compiler.source>
                <maven.compiler.target>${JdkVersion}</maven.compiler.target>  <!-- JRE System Library 的版本和这句相同  -->
            </properties>
        </profile>
    </profiles>
    <activeProfiles>
        <activeProfile>set-JdkVersion</activeProfile>  <!-- 要激活的profile的id . 这里和上面该id的profile中的 <activeByDefault>true</activeByDefault> 任一个都能激活该id代表的profile, 两处设置确保启用该profile-->
    </activeProfiles>

Maven 在 pom.xml 中指定jdk版本

在pom.xml中可以用设置属性或者设置插件两种方法来设置jdk版本

  • 用设置属性的方式
<project>
  <properties>
    <maven.compiler.source>21</maven.compiler.source>
    <maven.compiler.target>21</maven.compiler.target>
  </properties>
</project>
    <maven.compiler.source>21</maven.compiler.source>
    <maven.compiler.target>21</maven.compiler.target>
  • 用设置插件的方式 , 设置插件的方式优先级高于设置属性
<project>
  ...
  <build>
    ...
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <!-- <version>3.9.6</version> --> <!-- 可以不要version -->
        <configuration>
          <source>21</source>
          <target>21</target>
        </configuration>
      </plugin>
    </plugins>
    ...
  </build>
  ...
</project>
  • 用设置插件的方式 , 设置插件的方式优先级高于设置属性
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <!-- <version>3.9.6</version> --> <!-- 可以不要version -->
        <configuration>
          <source>21</source>
          <target>21</target>
        </configuration>
      </plugin>
    </plugins>

两种方法都用上, , 插件的优先级高于属性

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <JdkVersionOfThisPom>17</JdkVersionOfThisPom>
    <java.version>${JdkVersionOfThisPom}</java.version>
    <maven.compiler.source>${JdkVersionOfThisPom}</maven.compiler.source>
    <maven.compiler.target>${JdkVersionOfThisPom}</maven.compiler.target>
    <maven.compiler.compilerVersion>${JdkVersionOfThisPom}</maven.compiler.compilerVersion>
  </properties>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
<!--        <version>3.9.6</version>-->
        <configuration>
          <source>${JdkVersionOfThisPom}</source>
          <target>${JdkVersionOfThisPom}</target>
          <compilerVersion>${JdkVersionOfThisPom}</compilerVersion>
        </configuration>
      </plugin>
    </plugins>
  </build>

总结 

到此这篇关于maven在settings.xm和pom.xml中指定jdk版本编译的文章就介绍到这了,更多相关maven指定jdk版本编译内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • mybatis.type-aliases-package的作用及用法说明

    mybatis.type-aliases-package的作用及用法说明

    这篇文章主要介绍了mybatis.type-aliases-package的作用及用法说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Nacos客户端配置中心缓存动态更新实现源码

    Nacos客户端配置中心缓存动态更新实现源码

    这篇文章主要为大家介绍了Nacos客户端配置中心缓存动态更新实现源码,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪
    2022-03-03
  • Java notify和notifyAll的区别和相同

    Java notify和notifyAll的区别和相同

    本文主要介绍Java notify和notifyAll的知识,这里整理详细的资料来说明notify 和NotifAll的区别,有需要的小伙伴可以参考下
    2016-09-09
  • 浅谈为什么同一个java文件只能有一个public类

    浅谈为什么同一个java文件只能有一个public类

    这篇文章主要介绍了浅谈为什么同一个java文件只能有一个public类,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • MyBatis-Plus中实现自定义复杂排序逻辑的详细步骤

    MyBatis-Plus中实现自定义复杂排序逻辑的详细步骤

    这篇文章主要介绍了MyBatis-Plus中实现自定义复杂排序逻辑,通过使用MyBatis-Plus的QueryWrapper和SQL原始片段,我们可以灵活地实现复杂的数据排序逻辑,这种方法尤其适用于需要对数据进行特定规则排序的场景,需要的朋友可以参考下
    2024-07-07
  • 二代身份证验证示例

    二代身份证验证示例

    这篇文章主要介绍了二代身份证验证示例,需要的朋友可以参考下
    2014-02-02
  • 详解如何在Java8中创建和使用线程池

    详解如何在Java8中创建和使用线程池

    在 Java 8 中,线程池(Thread Pool)是一种管理线程资源的机制,能够有效地控制并发执行的线程数量,减少线程创建和销毁的开销,提高系统的性能,本篇文章将详细介绍如何在 Java 8 中创建和使用线程池,需要的朋友可以参考下
    2024-06-06
  • 教你用Java Swing实现自助取款机系统

    教你用Java Swing实现自助取款机系统

    今天给大家带来的是关于JAVA的相关知识,文章围绕着如何用Java Swing实现自助取款机系统展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
    2021-06-06
  • Jpa数据操作以及@Query和@Modifying注解使用方式

    Jpa数据操作以及@Query和@Modifying注解使用方式

    这篇文章主要介绍了Jpa数据操作以及@Query和@Modifying注解使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • JsonObject的属性与值的判空(Null值)处理方式

    JsonObject的属性与值的判空(Null值)处理方式

    这篇文章主要介绍了JsonObject的属性与值的判空(Null值)处理方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12

最新评论