
maven的settings.xm和pom.xml都可以通过 maven.compiler.source , maven.compiler.target 这两个属性值来指定jdk版本
maven.compiler.sourcemaven.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版本编译内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论