下载地址:https://help.sonatype.com/repomanager3/downloadapache
注意:Nexus Repository Manager 3是一个Java服务器应用程序,安装须要 jdk1.8以上的版本。api
下载解压后,用命令行到解压目录的bin目录下运行 nexus.exe /run(Linux运行./nexus run),启动完成后会显示“Started Sonatype Nexus”:缓存
------------------------------------------------- Started Sonatype Nexus OSS 3.16.2-01 -------------------------------------------------
Nexus管理后台地址:http://localhost:8081/
点击右上角Sign in登陆,默认帐号和密码为:admin/admin123
服务器
在Repositories 仓库管理界面中有多种默认的仓库,也能够添加新的仓库,本实例直接使用默认的仓库:
maven-central,Type为proxy,表示代理仓库。代理仓库用来代理远程仓库(maven-central代理的是超级POM中配置的Maven中央仓库),当在下载组件时,若是代理仓库搜索不到,则会把请求转发到远程仓库从远程仓库下载。从远程仓库下载后会缓存到代理仓库,下次还有该组件的请求则会直接到代理仓库下载,不会再次请求远程仓库。并发
maven-releases/maven-snapshots,Type为hosted,表示为宿主仓库。宿主仓库主要用来部署团队内部使用的内部组件,默认的maven-releases和maven-snapshots分别用来部署团队内部的发布版本组件和快照版本组件。
maven
配置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以下:url
<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-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的快照版本):
须要将项目版本改为发布版本,在pom.xml中
注意: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既可: