1. Nexus下载安装
-
下载解压
安装
nexus.exe /run
nexus.exe /install
net start nexus
net stop nexus
- 修改密码
复制/sonatype-work/nexus3/admin.password
文件中的密码 - 访问
http://127.0.0.1:8082/
登录用户名 admin
密码 上一步复制到的密码
备注:
(1) 登录进去后, admin.password文件就消失了!
(2) 要马上设置新密码为admin123-
新建阿里云镜像仓库作为maven-public首选的请求目标
2. Maven配置文件settings.xml修改
共有四处修改, server, mirror, profile, activeProfile, 完整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 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>H:\BaiduNetdiskDownload\maven_lib</localRepository>
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
<!-- 配置修改1 -->
<server>
<id>nexus-releases</id> <!-- 此id与mirror.id相同 -->
<username>admin</username> <!-- 登录Nexus的用户名 -->
<password>admin123</password> <!-- 登录Nexus的密码 -->
</server>
<server>
<id>nexus-snapshots</id> <!-- 此id与mirror.id相同 -->
<username>admin</username> <!-- 登录Nexus的用户名 -->
<password>admin123</password> <!-- 登录Nexus的密码 -->
</server>
</servers>
<mirrors>
<!-- 配置修改2 -->
<mirror>
<id>nexus-releases</id> <!-- 此id为重要标识, 跟后面的profile和activeProfile呼应, 也跟Maven工程里的上传目标呼应 -->
<mirrorOf>*</mirrorOf>
<url>http://localhost:8082/repository/maven-public/</url>
</mirror>
<mirror>
<id>nexus-snapshots</id> <!-- 此id为重要标识, 跟后面的profile和activeProfile呼应, 也跟Maven工程里的上传目标呼应 -->
<mirrorOf>*</mirrorOf>
<url>http://localhost:8082/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<!-- 配置修改3 -->
<profile>
<id>nexus-releases</id> <!-- 此id要与mirror.id相同 -->
<repositories>
<repository>
<id>central</id>
<url>http://maven.aliyun.com/nexus/content/groups/public</url> <!-- 此为阿里云Maven镜像, 当Nexus失效时使用 -->
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://maven.aliyun.com/nexus/content/groups/public</url> <!-- 此为阿里云Maven镜像, 当Nexus失效时使用 -->
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>nexus-snapshots</id> <!-- 此id与mirror.id相同 -->
<repositories>
<repository>
<id>central</id>
<url>http://maven.aliyun.com/nexus/content/groups/public</url> <!-- 此为阿里云Maven镜像, 当Nexus失效时使用 -->
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://maven.aliyun.com/nexus/content/groups/public</url> <!-- 此为阿里云Maven镜像, 当Nexus失效时使用 -->
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!-- 配置修改4 -->
<activeProfiles>
<activeProfile>nexus-releases</activeProfile> <!-- 此value与profile.id相同 -->
<activeProfile>nexus-snapshots</activeProfile> <!-- 此value与profile.id相同 -->
</activeProfiles>
</settings>
3. IDEA工程配置, 支持打包上传和下载引入
- 有两个工程: group和NexusDownload, 我们将配置使得NexusDownload可以使用group打出来的包, group包含两个子Module: module_henry和module_dongyan
- group配置pom.xml, 增加
<project>
...
<!-- 上传路径配置 -->
<distributionManagement>
<repository>
<id>nexus-releases</id> <!-- 与Maven的settings.xml中的mirror.id呼应 -->
<name>Internal Release</name>
<url>http://localhost:8082/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Internal Snapshots</name>
<url>http://localhost:8082/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>
- module_henry中类HelloHenry如下
package com.luban.maven;
public class HelloHenry {
public String getName() {
return "Henry";
}
public static void main(String[] args) {
System.out.println("Hello, Henry!");
}
}
-
使用mvn deploy上传group工程打包的结果
NexusDownload中, 在pom.xml中添加依赖
<project>
...
<dependencies>
<dependency>
<groupId>com.luban.maven</groupId>
<artifactId>module_henry</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
- 然后即可以直接使用group的子Module: module_henry中的类HelloHenry
package com.luban.maven;
public class NexusDownload {
public static void main(String[] args) {
HelloHenry helloHenry = new HelloHenry();
System.out.println(helloHenry.getName());
}
}
输出结果为
Henry