安装多个maven 指定版本操作命令

1.Maven使用,需要不同的JDK环境,但是系统配置【一个】环境变量,故开发指定JDK环境实现

mvn 和mvn.bat

mvn是linux系统使用的启动文件

mvn.cmd是window 使用的启动文件

以下方式均通过文本编辑方式打开

mvn:

在命令的上面增加 JAVA_HOME=你的JDK地址

mvn.bat

在命令的上面增加 set JAVA_HOME=C:\test\jdk_home

2.maven版本不同,拉取jar包失败!暂时需手动下载jar包,在安装到本地仓库

 上传jar包到本地仓库

mvn install:install-file -DgroupId=com.zebra -DartifactId=ZSDK_API -Dversion=v2.12.3782 -Dpackaging=jar -Dfile=E:\perslib\ZSDK_API.jar

命令解释:

安装指定文件到本地仓库命令:mvn install:install-file

-DgroupId=<groupId> : 设置上传到仓库的包名

-DartifactId=<artifactId> : 设置该包所属的模块名

-Dversion=1.0.0 : 设置该包的版本号

-Dpackaging=jar : 设置该包的类型(很显然jar包)

-Dfile=<myfile.jar> : 设置该jar包文件所在的路径与文件名,就是你这个jar现在在哪放着

问题:两个maven各自的settings文件配置仓库不同。[虽是本地安装也会根据远程仓库/私服验证],默认的是系统环境变量配置的maven路径[默认A,但是我的项目用的B]

做法:到B的安装目录 bin文件【D:\apache-maven-3.8.6\bin】cmd 然后执行上述 mvn install 。。。。 就可以啦!

成功结果

3.maven版本不同,但是项目工具 比如:idea可配置需要的maven版本 

4.maven项目引入自定义的jar包

<dependency>
     <groupId>com.effort</groupId>     <!-- 自定义即可 -->
     <artifactId>piapi</artifactId>    <!-- jar名称 -->
     <version>1.0.0</version>          <!-- 如果jar包未指定自定义即可-->
     <type>jar</type>
     <scope>system</scope>
     <systemPath>${project.basedir}/src/main/resources/lib/piapi.jar</systemPath>  
  <!-- 使用jar所在的绝对路径 -->
</dependency>

此方法项目打包jar包一起打包。另一方式 如标题2

5.maven配置多个镜像

1.打开maven/conf/settings.xml文件 配置全局文件——中央仓库镜像

<mirrors>
    <mirror>
        <id>nexus1</id>
        <mirrorOf>central</mirrorOf>
        <url>http://ip1:8081/repository/maven-public/</url>
    </mirror>
</mirrors>

<mirrorOf>*</mirrorOf> 
匹配所有仓库请求,即将所有的仓库请求都转到该镜像上,除非增加的镜像放到*的前面就好
<mirrorOf>repo1,repo2</mirrorOf> 
将仓库repo1和repo2的请求转到该镜像上,使用逗号分隔多个远程仓库。 
<mirrorOf>*,!repo1</miiroOf> 
匹配所有仓库请求,repo1除外,使用感叹号将仓库从匹配中排除。

一般配置一个即可,其余放到pom配置

2.pom配置个别镜像

<!-- nexus私服配置 -->
<repositories>
    <repository>
        <id>nexus2</id>
        <name>Nexus Repository</name>
        <url>http://ip3:8081/repository/maven-public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <!--snapshots默认是关闭的,需要开启 ,linux系统中-->
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories> 

 3.配置描述

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<profiles>
        <profile>
            <id>sit</id>
            <activation>
                <activeByDefault>true</activeByDefault> 
                # 默认情况下会激活该 profile  sit
            </activation>
            <properties>
                <ENV>SIT</ENV> #定义了一个名为 ENV 的属性,值为 "SIT"
            </properties>
        </profile>
        <profile>
            <id>uat</id>
            <properties>
                <ENV>UAT</ENV>
            </properties>
        </profile>
        <profile>
            <id>prd</id>
            <properties>
                <ENV>PRD</ENV>
            </properties>
        </profile>
# mvn clean install -PUAT [命令来激活 uat profile]
    </profiles>

    <distributionManagement>  # 简单理解:发布
        <snapshotRepository>
            <id>swid</id> #唯一标识符为 "swid" 的快照版本远程仓库
            <url>http://192.168.0.240:8080/repository/test-rep/</url>
        </snapshotRepository>
# 当执行 mvn deploy 命令时,Maven 会将快照版本发布到这个仓库。
    </distributionManagement>


    <repositories>
        <repository>  #简单理解:获取依赖项
            <id>swid2</id>
            <name>swid2</name> #Maven 会从该仓库中获取依赖项
            <url>http://192.168.0.240:8081/repository/maven-public/</url>
            <snapshots> #启用了快照版本的检索
                <enabled>true</enabled>
            </snapshots>
            <releases> #启用了稳定版本的检索
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>



</project>

6.推荐参考

弄懂maven仓库 & 仓库优先级 & settings & pom配置关系及差异-CSDN博客

  • 28
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Maven中,可以通过配置多个JDK版本来运行项目。首先,需要确保系统中已经安装多个JDK版本,并将它们添加到系统环境变量中。接下来,可以按照以下步骤来设置多个JDK版本: 1. 打开Maven安装目录下的`conf`文件夹,找到`settings.xml`文件,并用文本编辑器打开。 2. 在`settings.xml`文件中,找到`profiles`标签,并在其中添加多个`profile`子标签,每个子标签代表一个JDK版本。 3. 在每个`profile`子标签中,添加`id`和`activation`元素,分别用于标识和激活该JDK版本的配置。 4. 在每个`profile`子标签中,添加`properties`和`jdk.home`元素,用于指定该JDK版本的路径。 5. 最后,在`profiles`标签的外部,找到`activeProfiles`元素,并在其中添加要激活的JDK版本的`profile`的`id`。 以下是一个示例的`settings.xml`配置文件,其中包含两个JDK版本的配置: ``` <settings> ... <profiles> <profile> <id>jdk1.8</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <jdk.home>C:\Program Files\Java\jdk1.8</jdk.home> </properties> </profile> <profile> <id>jdk11</id> <properties> <jdk.home>C:\Program Files\Java\jdk11</jdk.home> </properties> </profile> </profiles> ... <activeProfiles> <activeProfile>jdk1.8</activeProfile> </activeProfiles> ... </settings> ``` 在上述示例中,`jdk1.8`被设置为默认激活的JDK版本。如果需要切换到`jdk11`,只需将`activeProfile`改为`jdk11`即可。 配置完成后,使用Maven运行项目时,会自动使用激活的JDK版本。 ### 回答2: 要在Maven中设置多个JDK版本,可以按照以下步骤进行操作: 1. 打开Maven安装目录,找到conf文件夹下的settings.xml文件。 2. 使用文本编辑器打开settings.xml文件。 3. 在<profiles>标签内添加一个新的profile,用于指定JDK版本。 ``` <profiles> <profile> <id>jdk8</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> </profile> </profiles> ``` 4. 可以重复以上步骤来添加其他JDK版本的profile。 5. 保存并关闭settings.xml文件。 现在,你可以在你的Maven项目中使用不同的JDK版本了。在项目的pom.xml文件中,可以使用以下配置指定使用哪个JDK版本: ``` <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> ``` 在上述配置中,将<source>和<target>的值设置为你想要使用的JDK版本。 这样,当你构建或编译项目时,Maven将使用指定的JDK版本来执行。 ### 回答3: 在Maven的settings.xml文件中,可以通过配置多个profile来设置多个JDK版本。 首先,我们需要在settings.xml文件中添加多个profile,每个profile代表一个JDK版本,如下所示: ```xml <profiles> <profile> <id>jdk8</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <jdk.version>1.8</jdk.version> </properties> </profile> <profile> <id>jdk11</id> <properties> <jdk.version>11</jdk.version> </properties> </profile> </profiles> ``` 以上示例中,我们定义了两个profile,分别是jdk8和jdk11。其中,jdk8被设置为默认激活的profile。 接下来,我们需要在build节点的plugins节点中配置maven-compiler-plugin插件,用于指定使用的JDK版本。在这个插件的配置中,通过使用${jdk.version}来引用我们在profile中定义的jdk.version属性。 ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> </plugins> </build> ``` 通过以上配置,当我们构建项目时,Maven会根据激活的profile来选择对应的JDK版本进行编译。 要使用其他的JDK版本,我们可以通过指定命令行参数来激活对应的profile。例如,要使用jdk11的话可以使用以下命令: ```shell mvn clean install -P jdk11 ``` 以上就是如何在Maven中设置多个JDK版本的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值