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

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

2025年12月25日 发表评论 阅读评论

一般我们在公司工作中,很少有人就负责一个项目,而负责不同的项目,由于各种原因使用的 JDK 版本可能并不相同,如果存在不兼容的情况,那么本地 mvn clean compile 的时候就会报错,这个时候就需要我们去修改我们设置的环境变量,如果多个项目同时开发,那么就需要时不时切来切去,特别烦人,其实这个问题 maven 早就替大家考虑过了,我们只需要:

1. 修改 maven 的 toolchains.xml 文件


<?xml version="1.0" encoding="UTF-8"?>

<!--
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.
-->

<!--
 | 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're all using the same Maven
 |                 installation). It'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.
 |-->
<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">

  <!-- 
   | With toolchains you can refer to installations on your system. This 
   | way you don'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 'jdk'. 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 'version', 'vendor' and 'arch'. By default 
   |   the version has a special meaning. If you configured in the pom.xml '1.5' 
   |   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>
   
  -->

  <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>

2. 修改项目的 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>

3. 修改完我们在编译的时候只需要运行:


mvn clean compile -Pdev

即可,这样编译就会自动用 jdk8 也不是系统环境变量配置的,当然还有更简单的,我们可以修改 pom.xml 中的配置,只需要把:


<activeByDefault>false</activeByDefault>

修改成


<activeByDefault>true</activeByDefault>

这样我们只需要运行:


mvn clean compile

即可。

全文完,如果本文对您有所帮助,请花 1 秒钟帮忙点击一下广告,谢谢。

作 者: BridgeLi,https://www.bridgeli.cn
原文链接:http://www.bridgeli.cn/archives/805
版权声明:非特殊声明均为本站原创作品,转载时请注明作者和原文链接。
分类: Java 标签: ,
  1. 本文目前尚无任何评论.

请输入正确的验证码