- 1、安装自己的 Nexus 服务器
安装方法这里就不做介绍了,网上下载一下直接安装就可以了,也可以下载 war 包的,放在 tomcat 类型的 web server 下面,直接运行。
Nexus 可以安装在 windows 上,也可以安装在 Linux 上面。
- 2、配置 Maven 的 settings.xml 文件
首先,在 节点下,配置自己的 server
<servers>
<server>
<id>releases</id>
<!-- 默认的用户名和密码 -->
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
其次,配置镜像,让所有的 jar 包都在自己私服的镜像上面去找,如果自己的镜像上面没有,私服会自己通过默认设置的代理在公共镜像上去查找
<mirrors>
<!-- 阿里云仓库-国内建议使用 -->
<mirror>
<id>alimaven</id>
<!-- <mirrorOf>central</mirrorOf> -->
<mirrorOf>*</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
<!--<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<name>Nexus mirror</name>
<url>http://localhost:8081/nexus/content/groups/public</url>
</mirror>-->
</mirrors>
然后,配置其使用的环境 Profiles:
<profiles>
<profile>
<id>dev</id>
<repositories>
<repository>
<id>nexus</id>
<!-- 这里的 url 是 public 镜像的路径,提供的对外的接口,两个都是一样的 -->
<url>http://localhost:8081/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://localhost:8081/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
同时,设置默认的使用环境:
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
- 3、如果需要将自己的 jar 包上传至私服,需要在自己的项目的 pom.xml 配置文件中做如下配置:
<project>
<distributionManagement>
<repository>
<!-- 这里的 id 需要使用 settings.xml 中 servers 节点配置的 -->
<id>releases</id>
<name>Releases</name>
<!-- 这里的url是nexus中releases镜像的路径 -->
<url>http://192.168.xx.xx:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Snapshots</name>
<!-- 这里的url是nexus中snapshots镜像的路径 -->
<url>http://192.168.xx.xx:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>
- 4、最后,通过 maven 的 deploy 命令,将自己的 jar 上传至私服。
run as > maven build > 输入命令 clean deploy > 运行