Nexus Maven Repositories

安装

下载地址

https://www.sonatype.com/download-oss-sonatype

nexus界面

在这里插入图片描述

Maven私库格式

maven规定了一个仓库标准,Central仓库是最大的Maven在线仓库,它遵守Maven仓库标准用于很多开源项目的发布。

Version policy–版本策略

Release

发布稳定版本的jar包,maven central用的是这种。

Snapshot

snapshot仓库适合持续开发。这些jar包名称一般以-SNAPSHOT结尾。这种仓库允许重复上传相同版本号的jar包,区别与release仓库一旦上传jar包将不可更改。

Mixed

混合类型的仓库可以部署release包和snapshot包。

Layout policy-- 结构策略

Maven制定了标准的目录结构和文件命名,但是因为历史原因,一些构建工具比如sbt或者自定义开发的构建工具没有遵循这些标准。为了保持兼容性

  • Permissive
    配置Permissive策略允许仓库的assets(上传的artifact)违反标准
  • Strict
    上传的artifact必须遵守标准。

私库结构

私库分类分成三种,proxy,hosted,group。三者之间的关系如下:
在这里插入图片描述

Settings.xml

settings.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
     https://maven.apache.org/xsd/settings-1.0.0.xsd">
  
  <interactiveMode/>
  <offline/>
  <pluginGroups/>
   <servers>
    <server>
      <id>nexus</id>
      <!--配置你的密码-->
      <username>admin</username>
      <password>admin123</password>
    </server>
  </servers>
  <mirrors>
  	<mirror>
      <!--This sends everything else to /public -->
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>http://localhost:8081/repository/maven-public/</url>
    </mirror>
  </mirrors>
  
  <proxies/>
  <profiles>
    <profile>
      <id>nexus</id>
      <!--Enable snapshots for the built in central repo to direct -->
      <!--all requests to nexus via the mirror -->
      <repositories>
        <repository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
     <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>
  <activeProfiles>
    <!--make the profile active all the time -->
    <activeProfile>nexus</activeProfile>
  </activeProfiles>
</settings>

项目如何发布到私库?

<project>
<!--省略---->
<distributionManagement>
    <repository>
      <id>nexus</id>
      <name>Releases</name>
      <url>http://localhost:8081/repository/maven-releases</url>
    </repository>
    <snapshotRepository>
      <id>nexus</id>
      <name>Snapshot</name>
      <url>http://localhost:8081/repository/maven-snapshots</url>
    </snapshotRepository>
  </distributionManagement>
</project>

私库管理规范

在这里插入图片描述
组织内部按照角色划分为多个私库,如图所示,代理私库从在线仓库下载,经过CI工具发布到Stage仓库。Stage仓库用于测试jar包稳定性,最终经过稳定性测试的jar包放到生产私库中。

批量上传本地依赖到Nexus私库

用法:

第一个参数传本地缓存目录,如/repository
第二个参数传hosted私库地址

#!/bin/bash
DEPENDENCY_DIR=$1
PRIVATE_REPOURL=$2/service/rest/v1/components?repository=snap

i=0
artifacts=$(find $DEPENDENCY_DIR | grep pom$)
for item in $artifacts
do
  dir_temp=$(dirname $item)
  #echo $dir_temp
  for file in $(ls $dir_temp)
  do
    #extension=$(echo $file |awk -F '.' '{print $2}')
    if [[ "$file" =~ pom$ ]]
    then
       pom_path=$dir_temp/$file
    fi
    if [[ "$file" =~ jar$ ]]
    then
       jar_path=$dir_temp/$file
    fi

  done
  echo "-------------------------------"
  echo $pom_path
  echo $jar_path
  if [[ -z $jar_path ]]
  then
    # only pom exist
    curl -v -u admin:icywater -X POST -F "maven2.generate-pom=false" -F "maven2.asset1=@$pom_path" -F "maven2.asset1.extension=pom" $PRIVATE_REPOURL
  else
    # pom and jar both exist
    curl -v -u admin:icywater -X POST -F "maven2.generate-pom=false" -F "maven2.asset1=@$pom_path" -F "maven2.asset1.extension=pom" -F "maven2.asset2=@$jar_path;type=application/java-archive" -F "maven2.asset2.extension=jar" $PRIVATE_REPOURL
  fi
  pom_path=""
  jar_path=""
done

备份与恢复

[root@gitlab nexus]# ls -l
total 156276
drwxr-xr-x. 9 root root       163 Sep 28 08:21 nexus-3.25.0-03
-rw-r--r--. 1 root root 160022971 Sep 28 08:21 nexus-3.25.0-03-unix.tar.gz
drwxr-xr-x. 3 root root        20 Sep 28 08:22 sonatype-work

全部文件都拷贝到另一个机器就可以了,数据存在sonatype-work里。

Reference List

  1. https://help.sonatype.com/repomanager3/formats/maven-repositories
  2. nexus小笔记
在Windows系统中,使用Nexus Maven Repository Manager进行批量上传是一个比较常见的需求。下面我将逐步解释如何完成这个任务。 首先,确保你已经安装了Maven和配置好了Maven的环境变量。然后,打开你的项目所在的文件夹,并找到项目根目录下的pom.xml文件。 1. 在pom.xml文件中添加Nexus Maven Repository Manager的配置。在<project>标签下添加如下代码: ```xml <repositories> <repository> <id>nexus</id> <url>你的Nexus仓库URL</url> </repository> </repositories> ``` 注:将“你的Nexus仓库URL”替换为你实际使用的Nexus仓库的URL。 2. 在pom.xml文件中配置需要上传的文件。在<build>标签下添加如下代码: ```xml <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>版本号</version> <configuration> <altDeploymentRepository>nexus::default::你的 Nexus 仓库 URL</altDeploymentRepository> </configuration> </plugin> </plugins> ``` 注:将“版本号”替换为Maven Deploy插件的最新版本号,将“你的 Nexus 仓库 URL”替换为你实际使用的Nexus仓库的URL。 3. 在命令行中执行以下命令,开始批量上传: ``` mvn deploy ``` 这将触发Maven执行deploy命令,并将构建的项目文件上传到配置的Nexus仓库中。 需要注意的是,执行此命令前,请确保你已经将正确的账户凭证配置在你的Maven settings.xml文件中,以便进行身份验证。 总结来说,在Windows系统中使用Nexus Maven仓库管理器进行批量上传,你需要在pom.xml文件中配置相应的Nexus仓库以及上传文件的相关信息,然后通过命令行运行`mvn deploy`命令来执行上传操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值