Nexus-3.16x版本构建Maven私有仓库的使用

3 篇文章 0 订阅
3 篇文章 0 订阅

之前文章已经对Nexus-3.16x版本如何搭建局域网内的私有仓库做了说明,这篇文章我们主要介绍关于如何使用我们的私有仓库以及可能遇到的一些问题。

Windows环境下使用Nexus-3.16x版本构建Maven私有仓库

一、环境

系统环境:Windows10    JDK版本:1.8.0_11     Maven版本:3.3.9     Nexus版本:3.16.0-01    IDE版本:IntelliJ IDEA 2017.2.5

二、本地Maven的配置

前提:在配置使用环境之前我们一定要明确,几种Maven仓库的调用优先级,这一点十分重要,在明确了这一点之后,项目上进行配置时就会相对可以灵活很多,这里直接给出结论。

Maven中的localRepository(本地仓库) > Maven中的profile(远程仓库) > Pom中的repository> Maven中的mirror(远程仓库)

说明:IDE在构建项目时,首先在Maven配置文件settings.xml所配置的的localRepository(本地仓库)路径下中查找jar依赖文件,若未找到相应依赖,则按照本地Maven所配置的profile(远程仓库)节点远程查找依赖,此时若找到则下载到本地仓库进行项目构建,若未找到则按照项目中的配置文件pom.xml中所配置的repository节点查找依赖,若找到则下载到本地仓库进行项目构建;若未找到则按照Maven中的配置文件settings.xml中所配置的mirror节点查找依赖若找到则下载到本地仓库进行项目构建,若未找到则项目构建失败。

1. 修改Maven安装目录【..\apache-maven-3.3.9\conf】的配置文件【settings.xml】

2. 相关配置如下,解析已在文件中注明

<?xml version="1.0" encoding="UTF-8"?>
<settings
    xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <!--配置本地Maven仓库Jar存储位置-->
    <localRepository>D:\Java\maven\repository</localRepository>
    <pluginGroups></pluginGroups>
    <proxies></proxies>
    <!--配置Nexus服务节点-->
    <servers>
        <server>
            <id>nexus-releases</id>
            <username>Nexus用户名</username>
            <password>Nexus密码</password>
        </server>
        <server>
            <id>nexus-snapshots</id>
            <username>Nexus用户名</username>
            <password>Nexus密码</password>
        </server>
    </servers>
    <!--配置私库服务器-->
    <mirrors>
        <mirror>
            <id>nexus</id>
            <mirrorOf>central</mirrorOf>
            <name>nexus repository</name>
            <url>http://IP:端口号/repository/maven-public/</url>
        </mirror>
    </mirrors>
    <!--配置文件-->
    <profiles>
        <!--配置具体指向Nexus私服的仓库-->
        <profile>
            <id>env-dev</id>
            <repositories>
                <repository>
                    <id>maven-public</id>
                    <url>http://IP:端口号/repository/maven-public/</url>
                    <releases>
                        <enable>true</enable>
                    </releases>
                    <snapshots>
                        <enable>true</enable>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>maven-public</id>
                    <url>http://IP:端口号/repository/maven-public/</url>
                    <releases>
                        <enable>true</enable>
                    </releases>
                    <snapshots>
                        <enable>true</enable>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
        <!--配置具体指向JDK解析-->
        <profile>
            <id>jdk-1.8</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
            </activation>
            <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
            </properties>
        </profile>
    </profiles>
    <!--设置起作用的配置-->
    <activeProfiles>
        <activeProfile>env-dev</activeProfile>
    </activeProfiles>
</settings>

三、IntelliJ IDEA及项目pom.xml配置

IntelliJ IDEA配置

1. 设置【Maven】

 项目pom.xml配置

1. 在pom.xml的【project】节点下,添加如下配置:

备注:repository的id节点需要和Maven配置文件settings.xml中server的id一致

 <!--配置阿里云搭建的国内镜像http://maven.aliyun.com-->
 <repositories>
    <repository>
        <id>nexus-aliyun</id>
        <name>nexus-aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

<!--以server指定的用户登录进行上传部署,普通游客没有权限部署-->
<distributionManagement>
    <repository>
        <id>nexus-releases</id>
        <name>Releases</name>
        <url>http://IP:端口号/repository/maven-releases</url>
    </repository>
    <!--version需要以-SNAPSHOT 结尾-->
    <snapshotRepository>
        <id>nexus-snapshots</id>
        <name>Snapshot</name>
        <url>http://IP:端口号/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

 附加

此外还有一种相对简单地配置方式,即在项目pom.xml文件中不配置repository节点,直接在Maven的配置文件setting.xml中配置mirror节点。即直接获取我们私库中的数据,流程变成:Maven中的localRepository(本地仓库) > Maven中的mirror(远程仓库)

配置如下:

1.Maven的配置文件setting.xml

<?xml version="1.0" encoding="UTF-8"?>
<settings
    xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <!--配置本地Maven仓库Jar存储位置-->
    <localRepository>D:\Java\maven\repository</localRepository>
    <pluginGroups></pluginGroups>
    <proxies></proxies>
    <!--配置Nexus服务节点-->
    <servers>
        <server>
            <id>nexus-releases</id>
            <username>Nexus用户名</username>
            <password>Nexus密码</password>
        </server>
        <server>
            <id>nexus-snapshots</id>
            <username>Nexus用户名</username>
            <password>Nexus密码</password>
        </server>
    </servers>
    <mirrors>
        <mirror>
            <id>nexus</id>
            <name>nexus repository</name>
            <url>http://IP:端口号/repository/maven-public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
    </mirrors>
</settings>

2.项目配置文件pom.xml

<!--以server指定的用户登录进行上传部署,普通游客没有权限部署-->
    <distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>Releases</name>
            <url>http://192.168.15.175:8088/consmation/nexus/repository/maven-consmation.releases/</url>
        </repository>
        <!--version需要以-SNAPSHOT 结尾-->
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Snapshot</name>
            <url>http://192.168.15.175:8088/consmation/nexus/repository/maven-consmation.snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

四、Nexus3.xx版本第三方jar上传

官方文档说明:https://help.sonatype.com/repomanager3/user-interface/uploading-components

前提条件

1. jar文件上传到私库的类型只能是hosted类型

2. 所操作的角色必须对目标私库读写权限

方式一:命令行执行

命令行,例:

mvn deploy:deploy-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.4 -Dpackaging=jar -DrepositoryId=nexus-3rd-party -Dfile=D:\Code\ojdbc6-11.2.0.4.jar -Durl=http://IP:端口号/repository/maven-3rd.party/ -DgeneratePom=false

说明

DgroupId、DartifactId、Dversion:构成了该jar包在pom.xml的坐标位置。
Dpackaging:打包类型。
Dfile:待上传的jar包的本地绝对路径。注:此处的jar包位置不能与本地仓库下的jar包路径一样。
Durl:私服上第三方仓库的地址。
DrepositoryId:服务器的唯一id,即在Maven配置文件setting.xml中所配置的serverId。例如上文所配置的nexus-releases。

方式二:管理界面执行

1. 登录Nexus,选择所要上传的私库。

2. 填写相关的必选项及上传文件

五、常见问题

1.在IDEA中使用maven plugin的deploy命令将项目打包部署到私库中时,出现如下错误:

Failure to find commons-logging:commons-logging:jar:1.2 in 私库url was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced -> [Help 1]

问题原因:意为在私库中未存在该jar依赖,当前jar依赖被占用等问题。

解决方法:将本地Maven仓库数据全部删除,重新在IDEA中导入依赖即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值