Maven私服nexus使用

本文主要介绍如何使用maven发布和使用dependence


nexus下载安装

nexus下载地址:官网
nexus安装教程:官网
注意:nexus依赖于JDK1.8环境,安装前请确保已经配置了 JAVA_HOME 环境变量。

其他注意事项:

  1. 配置nexus相关端口号(可以不改,默认为8081):
    • 进入etc目录:
      $ pwd
      /opt/nexus/nexus-3.63.0-01/etc
      
    • 编辑 nexus-default.properties
      $ vim nexus-default.properties
      application-port=18081
      
    • 则web访问地址则变为:http://localhost:18081
  2. nexus默认账号:
    2.x 版本安装后,默认用户名密码:admin/admin123
    3.x 版本安装后,默认用户名是 admin,密码则在指定文件下:/opt/nexus/sonatype-work/nexus/admin.password

deploy本地类库到nexus

  1. 修改Maven的settings.xml文件,添加相关认证信息:

    <servers>
        <!--release group认证信息-->
    	<server>
    	  <!--自定义,唯一即可-->
          <id>releases</id>
          <!--nexus账号:修改为自己的-->
          <username>admin</username>
          <!--nexus密码:修改为自己的-->
          <password>admin123</password>
        </server>
    	<!--snapshots group认证信息-->
        <server>
          <id>snapshots</id>
          <username>admin</username>
          <password>admin123</password>
        </server>	
    </servers>
    

    注意,settings文件中已经包含了servers标签,所以添加时只需要在该标签下追加server相关标签即可。

  2. 创建一个Maven项目(maven-deploy),pom.xml 内容修改如下:

    <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 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.len.maven</groupId>
      <artifactId>maven-deploy</artifactId>
      <version>1.0-SNAPSHOT</version>
      <name>Archetype - maven-deploy</name>
      <url>http://maven.apache.org</url>
    
      <!-- 配置deploy私服地址,根据version的后缀决定deploy位置 -->
      <distributionManagement>
        <!-- 开发版本发布地址 -->
        <snapshotRepository>
          <!--注意:这里的id需要跟settings.xml配置的server的id一致-->
          <id>snapshots</id>
          <name>Snapshot Repository</name>
          <url>http://localhost:18081/repository/maven-snapshots/</url>
        </snapshotRepository>
    
        <!-- 正式版本发布地址 -->
        <repository>
          <!--注意:这里的id需要跟settings.xml配置的server的id一致-->
          <id>releases</id>
          <name>Release Repository</name>
          <url>http://localhost:18081/repository/maven-releases/</url>
        </repository>
      </distributionManagement>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.2.2</version>
          </plugin>
        </plugins>
      </build>
    
    </project>
    
    • 项目中version信息决定deploy的位置,如 1.0-SNAPSHOT将会deploy到snapshot组下;而1.0-RELEASE将会deploy到release组下。
    • 配置的id将会关联到上一步在settings.xml文件配置的server信息。
    • url配置的私服的地址,可以在nexus页面直接复制:
      在这里插入图片描述
  3. 发布应用:

    $ pwd
    /IdeaProjects/tuoxin/maven/maven-deploy
    $ mvn clean deploy
    .....省略具体过程日志信息
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  13.457 s
    [INFO] Finished at: 2024-01-07T15:05:47+08:00
    [INFO] ------------------------------------------------------------------------
    
  4. 页面查看dependence信息:
    在这里插入图片描述

deploy第三方类库到nexus

对于需要引用的第三方类库,但还没有具体的pom坐标信息,此时,由于项目是第三方的,无法直接修改项目 pom.xml 来实现 deploy。这时可以使用 cmd 的方式来 deploy,下面以达梦数据库的驱动为例演示如何 deploy 第三方类库到nexus:

$ ls
DmJdbcDriver18.jar

$ mvn deploy:deploy-file -DgroupId=com.dameng -DartifactId=DmJdbcDriver -Dversion=1.18 -Dpackaging=jar -Dfile=DmJdbcDriver18.jar -Durl=http://localhost:18081/repository/maven-releases/ -DrepositoryId=releases
......此处省略具体的打包过程
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.015 s
[INFO] Finished at: 2024-01-07T15:51:41+08:00
[INFO] ------------------------------------------------------------------------

接着,访问 http://localhost:18081/#browse/browse:maven-public:com,就可以看到对应的库已经在仓库中。
在这里插入图片描述

引入nexus依赖

类库上传到nexus后,其他项目就可以从nexus下载这些依赖并使用。此时可以使用3种方式配置具体的私服地址,具体使用时只需要配置其中一种方式即可。

settings.xml 配置 mirror

直接修改 settings.xml,这种方式会对全局 maven 起作用,即本地上的所有maven项目都将从 nexus 下载依赖。具体配置如下:

<mirrors>
	<!--nexus私有仓库-->
    <mirror>
      <id>nexusmaven</id>
      <mirrorOf>*</mirrorOf>
      <name>nexus私有仓库</name>
      <url>http://localhost:18081/repository/maven-public</url>
    </mirror>
<mirrors>

settings.xml 配置 profile

直接修改 settings.xml,这种方式会对全局 maven 起作用,即本地上的所有maven项目都将从 nexus 下载依赖。这种方式相比较于mirror,好处是可以同时配置多个profile,并同时启用多个profile。具体配置如下:

<profiles>
    <profile>
      <id>dev</id>
      <!-- 自定义私有仓库地址 -->
      <repositories>
        <repository>
            <id>nexus</id>
            <name>Nexus Repository</name>
            <url>http://localhost:18081/repository/maven-public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
      </repositories>
      <!--插件nexu私服地址-->
      <pluginRepositories>
        <pluginRepository>
            <id>public</id>
            <name>Public Repository</name>
            <url>http://localhost:18081/repository/maven-public/</url>
        </pluginRepository>
      </pluginRepositories>
    </profile>
<profiles>

<activeProfiles>
    <activeProfile>dev</activeProfile>
    <!--多个profile可以继续追加激活-->
</activeProfiles>

项目中配置repositories

这种方式相对于修改 settings.xml的好处是该配置只会影响本项目的maven依赖下载,而不会对本地的其他项目造成影响。只需要在 pom.xml 添加如下配置:

  <!-- 自定义私有仓库地址 -->
  <repositories>
    <repository>
        <id>nexus</id>
        <name>Nexus Repository</name>
        <url>http://localhost:18081/repository/maven-public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
  </repositories>
  <!-- 自定义插件私有仓库地址 -->
  <pluginRepositories>
    <pluginRepository>
        <id>public</id>
        <name>Public Repository</name>
        <url>http://localhost:18081/repository/maven-public/</url>
    </pluginRepository>
  </pluginRepositories>
  • 41
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值