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
來統(tǒng)一設置
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必須在激活狀態(tài) -->
<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必須在激活狀態(tài) -->
<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版本文章來源:http://www.zghlxwxcb.cn/news/detail-836525.html
- 用設置屬性的方式
<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>
- 用設置插件的方式 , 設置插件的方式優(yōu)先級高于設置屬性
<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>
- 用設置插件的方式 , 設置插件的方式優(yōu)先級高于設置屬性
<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>
兩種方法都用上, , 插件的優(yōu)先級高于屬性文章來源地址http://www.zghlxwxcb.cn/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模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!