maven在settings.xm和pom.xml中指定jdk版本编译

这篇具有很好参考价值的文章主要介绍了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>

两种方法都用上, , 插件的优先级高于属性文章来源地址https://www.toymoban.com/news/detail-836525.html

  <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版本编译的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • IDEA创建spring boot项目无法加载出maven里的pom.xml,而是settings.gradle

    在创建的maven项目里找不到pom.xml文件而是settings.gradle,如下图所示: 原因和解决方法: 是因为在创建Springboot项目的时候type里选的是Gradle,gradle和maven又不一样,换成maven就行了,具体操作如下图所示: 在用idea创建maven项目时,在Project Metadata界面Type选项下唯有Maven和Maven PO

    2024年02月09日
    浏览(48)
  • jdk8 maven 3.6.3 settings.xml

    localRepositoryC:Usersziyou.m2repository/localRepository mirror         idhuaweicloud/id         mirrorOf*/mirrorOf         name华为云公共仓库/name         urlhttps://mirrors.huaweicloud.com/repository/maven//url     /mirror profile       idjdk-1.8/id       activation         activeByDefaulttrue/activeByDefaul

    2024年02月12日
    浏览(38)
  • 【java】maven 指定项目编译与打包的JDK版本

    Maven 是一个流行的构建工具,用于管理 Java 项目的依赖项、构建和部署。在 Maven 中,可以指定项目的编译和打包所使用的 JDK 版本。本篇博客将介绍如何在 Maven 中指定项目的 JDK 版本,并讨论该选项对项目的影响。 在 Maven 中,可以通过在 pom.xml 文件中设置 maven.compiler.source

    2024年02月16日
    浏览(35)
  • mvn 命令指定jdk版本

            想在自己的项目中搞一个gitlab-ci,从代码规范检测到单测。gitlab-ci都配置好了,使用镜像来启动。用的网上找的mvn soner命令,执行到最后总是报错,查询了错误是Jdk版本过低。因为公司项目用的是Jdk8,但是mvn soner自动推送到soner的功能需要Jdk11支持。就很难受了。

    2024年02月12日
    浏览(35)
  • 【Maven】Maven 中 pom.xml 文件

    Maven 是一个项目管理工具,可以对 Java 项目进行构建和管理依赖。 本文,我们认识下 pom.xml 文件。POM(Project Object Model, 项目对象模型) 是 Maven 工程的基本工作单位,也是 Maven 的核心。其包含项目的基本信息,用于描述项目如何构建、声明项目依赖等。 POM 是 Project Object Mod

    2024年02月15日
    浏览(34)
  • Maven 超级pom、最终有效pom、pom 详解、settings 详解

    在项目的 pom.xml 中不进行任何配置,仍然不影响 Maven 构建的运行,是因为所有的 pom 文件都会继承一个默认的配置,这个配置称为 超级pom ,在自己项目中的配置会覆盖 超级pom 中的配置,未被覆盖的就会继续使用 超级pom 中的配置 超级pom 定义在 maven-model-builder.jar 中,如果想

    2024年02月09日
    浏览(44)
  • Maven 的 pom.xml 样例

     pom.xml 模板样例: 仓库优化:

    2024年02月13日
    浏览(61)
  • Maven--pom.xml文件详解

    1.pom简介 pom指的是project object model,又叫项目对象模型。Maven的pom文件是一个XML文件,用于描述项目的各种属性、依赖和构建信息,包括项目的名称、版本、许可证、作者、描述、依赖关系、构建过程、插件等。总的来说,POM文件是Maven工程的基本工作单元,它包含了项目的所

    2024年01月19日
    浏览(41)
  • maven的pom.xml文件详解

    2024年02月12日
    浏览(46)
  • maven之pom.xml配置文件详解

    pom代表项目对象模型,它是Maven中工作的基本组成单位。它是一个XML文件,在项目的根目录中。pom包含的对象是使用maven来构建的,pom.xml文件包含了项目的各种配置信息。 创建一个POM之前,应该要先决定项目组(groupId),项目名(artifactId)和版本(version),因为这些属性在项目仓

    2023年04月09日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包