欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Maven私服搭建(Nexus Repository Manager 3)

程序员文章站 2022-03-20 08:31:13
下载和安装 下载地址:https://help.sonatype.com/repomanager3/download 注意:Nexus Repository Manager 3是一个Java服务器应用程序,安装需要 jdk1.8 以上的版本。 下载解压后,用命令行到解压目录的bin目录下运行 nex ......

下载和安装

下载地址:

Maven私服搭建(Nexus Repository Manager 3)

注意:nexus repository manager 3是一个java服务器应用程序,安装需要 jdk1.8以上的版本。

下载解压后,用命令行到解压目录的bin目录下运行 nexus.exe /run(linux运行./nexus run),启动完成后会显示“started sonatype nexus”:

-------------------------------------------------

started sonatype nexus oss 3.16.2-01

-------------------------------------------------

访问nexus管理后台

nexus管理后台地址:
点击右上角sign in登录,默认账号和密码为:admin/admin123
Maven私服搭建(Nexus Repository Manager 3)

在repositories 仓库管理界面中有多种默认的仓库,也可以添加新的仓库,本实例直接使用默认的仓库:
maven-central,type为proxy,表示代理仓库。代理仓库用来代理远程仓库(maven-central代理的是超级pom中配置的maven*仓库),当在下载组件时,如果代理仓库搜索不到,则会把请求转发到远程仓库从远程仓库下载。从远程仓库下载后会缓存到代理仓库,下次还有该组件的请求则会直接到代理仓库下载,不会再次请求远程仓库。

maven-releases/maven-snapshots,type为hosted,表示为宿主仓库。宿主仓库主要用来部署团队内部使用的内部组件,默认的maven-releases和maven-snapshots分别用来部署团队内部的发布版本组件和快照版本组件。
Maven私服搭建(Nexus Repository Manager 3)

配置代理仓库

配置settings.xml:

<settings>
   <!-- 配置镜像,此处拦截所有远程仓库的请求到代理仓库-->
  <mirrors>
    <mirror>
      <id>nexus</id>
      <mirrorof>*</mirrorof>
      <url>http://localhost:8081/repository/maven-central/</url>
    </mirror>
  </mirrors>
  <!-- 配置远程库和远程插件库-->
  <profiles>
    <profile>
      <id>nexus</id>
      <!-- maven用于填充构建系统本地存储库的远程仓库集合-->
      <repositories>
        <repository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
     <!-- 类似于repositories元素,指定maven可以在哪里找到maven插件的远程仓库位置-->
     <pluginrepositories>
        <pluginrepository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginrepository>
      </pluginrepositories>
    </profile>
  </profiles>
  <!-- 激活profiles配置 -->
  <activeprofiles>
    <activeprofile>nexus</activeprofile>
  </activeprofiles>
</settings>

创建maven项目,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/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>

  <groupid>com.nocoffee</groupid>
  <artifactid>coffee-api</artifactid>
  <version>0.0.1-snapshot</version>
  <packaging>jar</packaging>

  <name>coffee-api</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
      <groupid>junit</groupid>
      <artifactid>junit</artifactid>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

执行mvn clean,执行mvn clean需要下载maven-clean-plugin插件,通过browse界面可以看到因为执行mvn clean而下载的maven-clean-plugin.jar:
Maven私服搭建(Nexus Repository Manager 3)

注意:如果界面为空,表示没有下载,原因是之前下载过该插件到本地仓库,需要把本地仓库的maven-clean-plugin插件删除,我的本地仓库路径为d:\reporsitory,所以需要删掉文件夹:d:\reporsitory\org\apache\maven\plugins\maven-clean-plugin,然后重新构建即可。

配置宿主仓库

settings.xml增加如下配置:

<servers>
     <server>
        <id>nexus</id>
        <username>admin</username>
        <password>admin123</password>
    </server>
</servers>

配置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/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>

  <groupid>com.nocoffee</groupid>
  <artifactid>coffee-api</artifactid>
  <version>0.0.1-snapshot</version>
  <packaging>jar</packaging>

  <name>coffee-api</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
      <groupid>junit</groupid>
      <artifactid>junit</artifactid>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  
  <distributionmanagement>
        <repository>
        <id>nexus</id>
        <name>maven-releases</name>
        <url>http://localhost:8081/repository/maven-releases/</url>
        </repository>
        <snapshotrepository>
        <id>nexus</id>
        <name>maven-snapshots</name>
        <url>http://localhost:8081/repository/maven-snapshots/</url>
        </snapshotrepository>
   </distributionmanagement>
</project>

执行mvn clean deploy将项目打包并发布到宿主仓库,构建成功后到browse中maven-snapshots库查看(因为项目版本为0.0.1-snapshot,是带snapshot的快照版本):
Maven私服搭建(Nexus Repository Manager 3)

maven-releases库

需要将项目版本改成发布版本,在pom.xml中0.0.1-snapshot去掉-snapshot,改为0.0.1。重新执行mvn clean deploy:
Maven私服搭建(Nexus Repository Manager 3)

注意:maven-releases库默认不能重新发布,需要可重新发布则需要修改该仓库配置。
测试重新发布到maven-releases库,执行mvn clean deploy将会构建失败:

[info] ------------------------------------------------------------------------
[info] build failure
[info] ------------------------------------------------------------------------
[info] total time: 4.112 s
[info] finished at: 2019-06-10t16:34:29+08:00
[info] final memory: 18m/164m
[info] ------------------------------------------------------------------------
[error] failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project coffee-api: failed to deploy artifacts: could not transfer artifact com.nocoffee:coffee-api:jar:0.0.1 from/to nexus (http://localhost:8081/repository/maven-releases/): failed to transfer file: http://localhost:8081/repository/maven-releases/com/nocoffee/coffee-api/0.0.1/coffee-api-0.0.1.jar. return code is: 400, reasonphrase: repository does not allow updating assets: maven-releases. -> [help 1]
[error] 
[error] to see the full stack trace of the errors, re-run maven with the -e switch.
[error] re-run maven using the -x switch to enable full debug logging.
[error] 
[error] for more information about the errors and possible solutions, please read the following articles:
[error] [help 1] http://cwiki.apache.org/confluence/display/maven/mojoexecutionexception

将maven-releases库中deployment pollcy改为allow redeploy既可:
Maven私服搭建(Nexus Repository Manager 3)