本地开发不同项目使用不同版本 JDK 的解决方案

一般我们在公司工作中,很少有人就负责一个项目,而负责不同的项目,由于各种原因使用的 JDK 版本可能并不相同,如果存在不兼容的情况,那么本地 mvn clean compile 的时候就会报错,这个时候就需要我们去修改我们设置的环境变量,如果多个项目同时开发,那么就需要时不时切来切去,特别烦人,其实这个问题 maven 早就替大家考虑过了,我们只需要: 修改 maven 的 toolchains.xml 文件 <?xml version="1.0" encoding="UTF-8"?> <!&#8211; Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. &#8211;> <!&#8211; | This is the toolchains file for Maven. It can be specified at two levels: | | 1. User Level. This toolchains.xml file provides configuration for a single user, | and is normally provided in ${user.home}/.m2/toolchains.xml. | | NOTE: This location can be overridden with the CLI option: | | -t /path/to/user/toolchains.xml | | 2. Global Level. This toolchains.xml file provides configuration for all Maven | users on a machine (assuming they&#8217;re all using the same Maven | installation). It&#8217;s normally provided in | ${maven.conf}/toolchains.xml. | | NOTE: This location can be overridden with the CLI option: | | -gt /path/to/global/toolchains.xml | | The sections in this sample file are intended to give you a running start at | getting the most out of your Maven installation. |&#8211;> <toolchains xmlns="http://maven.apache.org/TOOLCHAINS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/TOOLCHAINS/1.1.0 http://maven.apache.org/xsd/toolchains-1.1.0.xsd"> <!&#8211; | With toolchains you can refer to installations on your system. This | way you don&#8217;t have to hardcode paths in your pom.xml. | | Every toolchain consist of 3 elements: | * type: the type of tool. An often used value is &#8216;jdk&#8217;. Toolchains-aware | plugins should document which type you must use. | | * provides: A list of key/value-pairs. | Based on the toolchain-configuration in the pom.xml Maven will search for | matching <provides/> configuration. You can decide for yourself which key-value | pairs to use. Often used keys are &#8216;version&#8217;, &#8216;vendor&#8217; and &#8216;arch&#8217;. By default | the version has a special meaning. If you configured in the pom.xml &#8216;1.5&#8217; | Maven will search for 1.5 and above. | | * configuration: Additional configuration for this tool. | Look for documentation of the toolchains-aware plugin which configuration elements | can be used. | | See also https://maven.apache.org/guides/mini/guide-using-toolchains.html | | General example <toolchain> <type/> <provides> <version>1.0</version> </provides> <configuration/> </toolchain> | JDK examples <toolchain> <type>jdk</type> <provides> <version>1.5</version> <vendor>sun</vendor> </provides> <configuration> <jdkHome>/path/to/jdk/1.5</jdkHome> </configuration> </toolchain> <toolchain> <type>jdk</type> <provides> <version>1.6</version> <vendor>sun</vendor> </provides> <configuration> <jdkHome>/path/to/jdk/1.6</jdkHome> </configuration> </toolchain> &#8211;> <toolchain> <type>jdk</type> <provides> <version>17</version> </provides> <configuration> <jdkHome>D:\J2EE\Java\jdk-17</jdkHome> </configuration> </toolchain> <toolchain> <type>jdk</type> <provides> <version>8</version> </provides> <configuration> <jdkHome>D:\J2EE\Java\jdk1.8.0_311</jdkHome> </configuration> </toolchain> </toolchains> 修改项目的 pom.xml 文件 <profiles> <profile> <id>dev</id> <activation> <activeByDefault>false</activeByDefault> </activation> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-toolchains-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <goals> <goal>toolchain</goal> </goals> </execution> </executions> <configuration> <toolchains> <jdk> <version>8</version> </jdk> </toolchains> </configuration> </plugin> </plugins> </build> </profile> </profiles> 修改完我们在编译的时候只需要运行: mvn clean compile -Pdev 即可,这样编译就会自动用 jdk8 也不是系统环境变量配置的,当然还有更简单的,我们可以修改 pom.xml 中的配置,只需要把: ...

December 25, 2025 · 3 min · 522 words · Bridge Li