Nexus 搭建 Maven 私服

作者:範宗雲 来源:原创 发布时间:2015-04-18 归档:maven

环境 : Windows 8 Maven 3.2.5 Nexus 2.11.0
版本选择
http://www.sonatype.org/nexus/archived
nexus 2.6 及 2.6+ 版本要求 jdk 1.7 或 1.7+ 版本。根据你现有的环境来选择合适的版本。这里选择 nexus-2.11.0 ( 如果无法打开下载, 可将新开标签页地址栏中的地址复制到迅雷等进行下载 )。下载得到 nexus-2.11.0-bundle.zip 压缩包。解压缩里面的 nexus-2.11.0-02 目录, 如我的 E:\setup\dev\servers\nexus\2.11.0, 最终得到的目录结构为 E:\setup\dev\servers\nexus\2.11.0\nexus-2.11.0-02。
配置 nexus 环境
NEXUS_HOME = E:\setup\dev\servers\nexus\2.11.0\nexus-2.11.0-02
Path = .;%NEXUS_HOME%\bin
安装 nexus 服务
在命令行中执行 nexus install ( 必须以管理员身份运行 cmd, 以下同。win8 以管理员身份运行 cmd 的组合键为 ctrl + x 然后按 a )
C:\WINDOWS\system32>nexus install
wrapper | nexus installed.
启动 nexus
在命令行中执行 nexus start
C:\WINDOWS\system32>nexus start
wrapper | Starting the nexus service...
wrapper | Waiting to start...
wrapper | Waiting to start...
wrapper | Waiting to start...
wrapper | Waiting to start...
wrapper | nexus started.
访问入口地址 :http://localhost:8081/nexus
登录 nexus
默认的管理员账号为 admin, 密码为 admin123
停止 nexus
在命令行中执行 nexus stop
C:\WINDOWS\system32>nexus stop
wrapper | Stopping the nexus service...
wrapper | Waiting to stop...
wrapper | nexus stopped.
修改 nexus 账号密码
先停止 nexus, 打开 %NEXUS_HOME%\sonatype-work\nexus\conf\security.xml, 修改即可。nexus 的密码采用 SHA1 加密算法 ( 在线加密工具 ), 将加密后的 SHA1 串拷贝覆盖原来的即可。
		    <user>
		      <id>admin</id>
		      <firstName>Administrator</firstName>
		      <lastName>User</lastName>
		      <password>2899441143e28aee7287e8da3d7211258f5edbd3</password>
		      <status>active</status>
		      <email>lychie@yeah.net</email>
		    </user>
		    
修改 nexus 端口
打开 %NEXUS_HOME%\nexus-2.11.0-02\conf\nexus.properties, 修改 application-port 即可。
		    application-port=8082
		    application-host=0.0.0.0
			nexus-webapp=${bundleBasedir}/nexus
			nexus-webapp-context-path=/nexus
		    
nexus 仓库
打开 Views/Repositories 视图如下 :
nexus 仓库分为 4 种, group ( 仓库组 )、hosted ( 本地仓库 )、proxy ( 代理仓库 )、virtual ( 虚拟仓库 )。
我们自己开发的构件通常是发布到 hosted 仓库, proxy 用来代理远程的公共仓库, 一个 group 可以包含多个 hosted/proxy 仓库。
配置仓库组
Repositories --> Public Repositories --> Configuration
左边是组仓库, 右边是当前可用的仓库, 从右边挑选你想要加入到组的仓库, 点击 Save 保存即可。仓库组中仓库的顺序是有用的, 它决定了 maven 获取构件时遍历 nexus 仓库的顺序。
maven settings.xml 配置
		    <servers>
		      <server>
		        <id>nexus-releases</id>
		        <username>deployment</username>
		        <password>deployment</password>
		      </server>
		      <server>
		        <id>nexus-snapshots</id>
		        <username>deployment</username>
		        <password>deployment</password>
		      </server>
		      <server>
		        <id>nexus-thirdparty</id>
		        <username>deployment</username>
		        <password>deployment</password>
		      </server>
		    </servers>

		    <mirrors>
		      <mirror>
			    <id>nexus</id>
			    <mirrorOf>*</mirrorOf>
			    <name>nexus public repositories</name>
			    <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>
			  </mirror>
		    </mirrors>
		    
		    <profiles>
		      <profile>
			    <id>development</id>
			    <activation>
			      <jdk>1.7</jdk>
				  <activeByDefault>true</activeByDefault>
			    </activation>
			    <properties>
			      <maven.compiler.source>1.7</maven.compiler.source>
				  <maven.compiler.target>1.7</maven.compiler.target>
				  <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
			    </properties>
			    <repositories>
			      <repository>
				    <id>central</id>
				    <url>http://127.0.0.1:8081/nexus/content/repositories/central</url>
				    <releases>
				      <enabled>true</enabled>
			        </releases>
				    <snapshots>
				      <enabled>false</enabled>
				    </snapshots>
				  </repository>
			      <repository>
				    <id>nexus-thirdparty</id>
				    <url>http://127.0.0.1:8081/nexus/content/repositories/thirdparty</url>
				    <releases>
				      <enabled>true</enabled>
			        </releases>
				    <snapshots>
				      <enabled>false</enabled>
				    </snapshots>
				  </repository>
			      <repository>
				    <id>nexus-releases</id>
				    <url>http://127.0.0.1:8081/nexus/content/repositories/releases</url>
				    <releases>
				      <enabled>true</enabled>
			        </releases>
				    <snapshots>
				      <enabled>false</enabled>
				    </snapshots>
				  </repository>
			      <repository>
				    <id>nexus-snapshots</id>
				    <url>http://127.0.0.1:8081/nexus/content/repositories/snapshots</url>
				    <releases>
				      <enabled>true</enabled>
			        </releases>
				    <snapshots>
				      <enabled>true</enabled>
				    </snapshots>
				  </repository>
			    </repositories>
			    <pluginRepositories>
			      <pluginRepository>
				    <id>central</id>
				    <url>http://127.0.0.1:8081/nexus/content/repositories/central</url>
				    <releases>
				      <enabled>true</enabled>
			        </releases>
				    <snapshots>
				      <enabled>false</enabled>
				    </snapshots>
				  </pluginRepository>
			      <pluginRepository>
				    <id>nexus-thirdparty</id>
				    <url>http://127.0.0.1:8081/nexus/content/repositories/thirdparty</url>
				    <releases>
				      <enabled>true</enabled>
			        </releases>
				    <snapshots>
				      <enabled>false</enabled>
				    </snapshots>
				  </pluginRepository>
			      <pluginRepository>
				    <id>nexus-releases</id>
				    <url>http://127.0.0.1:8081/nexus/content/repositories/releases</url>
				    <releases>
				      <enabled>true</enabled>
			        </releases>
				    <snapshots>
				      <enabled>false</enabled>
				    </snapshots>
				  </pluginRepository>
			      <pluginRepository>
				    <id>nexus-snapshots</id>
				    <url>http://127.0.0.1:8081/nexus/content/repositories/snapshots</url>
				    <releases>
				      <enabled>true</enabled>
			        </releases>
				    <snapshots>
				      <enabled>true</enabled>
				    </snapshots>
				  </pluginRepository>
			    </pluginRepositories>
			  </profile>
		    </profiles>
		    
配置稍微有点长, 请稍安勿躁。
<server> 配置服务器的账户密码, 主要用于发布构件时进行权限的认证。server 的 id 必须与 repository 的 id 一致 ( 参考第3和第62行配置 ), 否则发布构件的时候会报 401, ReasonPhrase: Unauthorized。username 和 password 必须与 nexus 中已有的账户密码一致, 否则发布构件的时候一样会报 401 错误。
<mirror> 镜像配置, 具体用法参考 maven 镜像配置, 镜像地址配到 nexus 仓库组的地址 http://127.0.0.1:8081/nexus/content/groups/public。所有 maven 构件的请求, 均由该镜像来代理。
<profile> 在 Maven JDK 配置 中介绍了一小部分, 这里不再赘述。repository 配置使用的仓库, 这里配置了 4 个。点击参考这里
至此, 配置完成, 下面来介绍下如何发布自己的构件到 nexus 仓库。
发布构件到 nexus 仓库
		    <project>
			  <modelVersion>4.0.0</modelVersion>
			  <groupId>org.lychie</groupId>
			  <artifactId>myapp</artifactId>
			  <version>1.0.0</version>
			  <packaging>jar</packaging>
			  <name>myapp</name>
			  <url>http://maven.apache.org</url>
			  <dependencies>
			    <dependency>
			      <groupId>junit</groupId>
			      <artifactId>junit</artifactId>
			      <version>4.11</version>
			      <scope>test</scope>
			    </dependency>
			  </dependencies>
			  <distributionManagement>
			    <repository>
			      <id>nexus-releases</id>
			      <url>http://localhost:8081/nexus/content/repositories/releases</url>
			    </repository>
			  </distributionManagement>
			</project>
		    
主要配置在第17到22行。配置好仓库 id 和 仓库地址, 在命令行执行发布命令 mvn clean deploy
F:\dev\workspace\eclipse\ee\luna\myapp>mvn deploy
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building myapp 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] --- maven-install-plugin:2.4:install (default-install) @ myapp ---
[INFO] Installing F:\dev\workspace\eclipse\ee\luna\myapp\target\myapp-1.0.0.jar to F:\dev\repo\maven2\org\lychie\myapp\1.0.0\myapp-1.0.0.jar
[INFO] Installing F:\dev\workspace\eclipse\ee\luna\myapp\pom.xml to F:\dev\repo\maven2\org\lychie\myapp\1.0.0\myapp-1.0.0.pom
[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ myapp ---
Uploading: http://localhost:8081/nexus/content/repositories/releases/org/lychie/myapp/1.0.0/myapp-1.0.0.jar
Uploaded: http://localhost:8081/nexus/content/repositories/releases/org/lychie/myapp/1.0.0/myapp-1.0.0.jar (3 KB at 6.9 KB/sec)
Uploading: http://localhost:8081/nexus/content/repositories/releases/org/lychie/myapp/1.0.0/myapp-1.0.0.pom
Uploaded: http://localhost:8081/nexus/content/repositories/releases/org/lychie/myapp/1.0.0/myapp-1.0.0.pom (822 B at 5.4 KB/sec)
Downloading: http://localhost:8081/nexus/content/repositories/releases/org/lychie/myapp/maven-metadata.xml
Uploading: http://localhost:8081/nexus/content/repositories/releases/org/lychie/myapp/maven-metadata.xml
Uploaded: http://localhost:8081/nexus/content/repositories/releases/org/lychie/myapp/maven-metadata.xml (295 B at 1.8 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
到 nexus 查看发布结果, Repositories --> Releases --> Browse Index
至此, 我们自己开发的 jar 已经发布到 nexus 仓库, 下面来介绍如何使用这个构件。
使用 nexus 仓库中的构件
			<dependencies>
		      <dependency>
		        <groupId>org.lychie</groupId>
		        <artifactId>myapp</artifactId>
		        <version>1.0.0</version>
		      </dependency>
		    </dependencies>
			
在命令行执行编译命令 mvn compile
F:\dev\workspace\eclipse\ee\luna\myapps>mvn compile
[INFO] ------------------------------------------------------------------------
[INFO] Building myapps 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://127.0.0.1:8081/nexus/content/groups/public/org/lychie/myapp/1.0.0/myapp-1.0.0.pom
Downloaded: http://127.0.0.1:8081/nexus/content/groups/public/org/lychie/myapp/1.0.0/myapp-1.0.0.pom (822 B at 3.1 KB/sec)
Downloading: http://127.0.0.1:8081/nexus/content/groups/public/org/lychie/myapp/1.0.0/myapp-1.0.0.jar
Downloaded: http://127.0.0.1:8081/nexus/content/groups/public/org/lychie/myapp/1.0.0/myapp-1.0.0.jar (3 KB at 85.1 KB/sec)
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ myapps ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to F:\dev\workspace\eclipse\ee\luna\myapps\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
可以看到, maven 是从 nexus 的仓库组中请求所需的构件。
接着我们来做这样的一个试验 :将 maven settings.xml 配置文件中的 mirror 配置注释掉, 删除本地仓库的 org/lychie 目录, 重新执行编译命令
mvn compile
F:\dev\workspace\eclipse\ee\luna\myapps>mvn clean compile
[INFO] ------------------------------------------------------------------------
[INFO] Building myapps 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://127.0.0.1:8081/nexus/content/repositories/central/org/lychie/myapp/1.0.0/myapp-1.0.0.pom
Downloading: http://127.0.0.1:8081/nexus/content/repositories/thirdparty/org/lychie/myapp/1.0.0/myapp-1.0.0.pom
Downloading: http://127.0.0.1:8081/nexus/content/repositories/releases/org/lychie/myapp/1.0.0/myapp-1.0.0.pom
Downloaded: http://127.0.0.1:8081/nexus/content/repositories/releases/org/lychie/myapp/1.0.0/myapp-1.0.0.pom (923 B at 33.4 KB/sec)
Downloading: http://127.0.0.1:8081/nexus/content/repositories/central/org/lychie/myapp/1.0.0/myapp-1.0.0.jar
Downloading: http://127.0.0.1:8081/nexus/content/repositories/thirdparty/org/lychie/myapp/1.0.0/myapp-1.0.0.jar
Downloading: http://127.0.0.1:8081/nexus/content/repositories/releases/org/lychie/myapp/1.0.0/myapp-1.0.0.jar
Downloaded: http://127.0.0.1:8081/nexus/content/repositories/releases/org/lychie/myapp/1.0.0/myapp-1.0.0.jar (3 KB at 53.5 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
可以看到, 去掉代理以后, maven 首先到 central 仓库请求构件, 发现没有; 接着去 thirdparty 仓库请求, 发现也没有; 然后去 releases 仓库中请求, 发现有这个构件并进行了下载。而这里 maven 请求构件的顺序正是我们在 settings.xml 中配置的 repository 的先后顺序。
发布第三方构件到 nexus 仓库
首先准备好你的 jar 包, 如这里的 D:/dev/share/myapp-1.0.1.jar 1> 命令行的方式
在命令行执行发布命令 mvn deploy:deploy-file -DgroupId=org.lychie -DartifactId=myapp -Dversion=1.0.1 -Dpackaging=jar -Dfile=myapp-1.0.1.jar -Durl=http://127.0.0.1:8081/nexus/content/repositories/releases -DrepositoryId=nexus-releases
F:\dev\share>mvn deploy:deploy-file -DgroupId=org.lychie -DartifactId=myapp -Dversion=1.0.1 -Dpackaging=jar -Dfile=myapp-1.0.1.jar -Durl=http://127.0.0.1:8081/nexus/content/repositories/releases -DrepositoryId=nexus-releases
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
Uploading: http://127.0.0.1:8081/nexus/content/repositories/releases/org/lychie/myapp/1.0.1/myapp-1.0.1.jar
Uploaded: http://127.0.0.1:8081/nexus/content/repositories/releases/org/lychie/myapp/1.0.1/myapp-1.0.1.jar (3 KB at 5.0 KB/sec)
Uploading: http://127.0.0.1:8081/nexus/content/repositories/releases/org/lychie/myapp/1.0.1/myapp-1.0.1.pom
Uploaded: http://127.0.0.1:8081/nexus/content/repositories/releases/org/lychie/myapp/1.0.1/myapp-1.0.1.pom (388 B at 5.3 KB/sec)
Downloading: http://127.0.0.1:8081/nexus/content/repositories/releases/org/lychie/myapp/maven-metadata.xml
Downloaded: http://127.0.0.1:8081/nexus/content/repositories/releases/org/lychie/myapp/maven-metadata.xml (295 B at 6.1 KB/sec)
Uploading: http://127.0.0.1:8081/nexus/content/repositories/releases/org/lychie/myapp/maven-metadata.xml
Uploaded: http://127.0.0.1:8081/nexus/content/repositories/releases/org/lychie/myapp/maven-metadata.xml (326 B at 2.4 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
2> 可视化界面
Repositories --> Releases --> Artifact Upload
点击左侧的 Artifact, 再点击正下方的 Upload Artifact(s), 即可。
查看发布结果, Repositories --> Releases --> Browse Index :