目录
(2)修改settings.xml文件,配置连接私服的用户和密码
(2)用管理员运行cmd,切换到D:\nexus\nexus-2.12.0-01\bin目录,上传私服
一、介绍
1.私服
公司在自己的局域网内搭建自己的远程仓库服务器,称为私服, 私服服务器就是公司内部的 maven 远程仓库
2.Nexus
Nexus 是 Maven 仓库管理器, 通过 nexus 可以搭建 maven 仓库
二、maven私服实战
1.nexus安装
(1)解压安装包
解压nexus-2.12.0-01-bundle.zip压缩包,要放到一个不含中文的目录下(这里我放在了D:\nexus)
(2)修改配置文件
查看conf文件下的nexus.properties配置文件,可以修改对应的配置(主要是改端口号,因为端口号有可能会被占用,这里我写的是8088)
# Jetty section
application-port=8088 #端口号
application-host=0.0.0.0 #允许访问的主机监听
nexus-webapp=${bundleBasedir}/nexus #nexus工程目录
nexus-webapp-context-path=/nexus #nexus访问路径
# Nexus section
nexus-work=${bundleBasedir}/../sonatype-work/nexus
runtime=${bundleBasedir}/nexus/WEB-INF
(3)nexus的安装命令
用管理员运行cmd,切换到D:\nexus\nexus-2.12.0-01\bin目录下,执行nexus.bat install 进行安装
如果出现 “ 'findstr' 不是内部或外部命令,也不是可运行的程序或批处理文件”这种错误,是因为path环境变量被修改或误删导致的,在path里新建一个C:\Windows\System32
执行 nexus.bat start 启动服务 执行 nexus.bat stop 停止服务
(4)nexus的卸载命令
用管理员运行cmd,切换到D:\nexus\nexus-2.12.0-01\bin目录下,执行nexus.bat uninstall 进行卸载
(5)访问图形化界面
打开浏览器,输入 http://localhost:8088/nexus访问
8088是我设置的端口号
(6)登录
点击Log In进行登录。用户名:admin 密码:admin123
2.将项目发布到私服
(1)在客户端配置maven环境
就是上一步,已经配置过了
(2)修改settings.xml文件,配置连接私服的用户和密码
在servers节点下进行配置
<!-- 定义稳定版本的id名称,用户名密码 -->
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<!-- 定义开发版本的id名称,用户名密码 -->
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
(3)配置pom.xml文件,配置私服仓库的地址
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8088/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8088/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
注:这里的id值要和settings.xml配置文件中的id值保持一致
(4)在该工程执行deploy命令,发布项目到私服
①稳定/线上版本
查看私服:
②SNAPSHOT快照版本
查看私服:
3.从私服下载jar包
先把自己的某个项目(我发布的Hello)发布到私服,然后删除本地仓库的jar包,再用其他项目(HelloFriend)去依赖该jar包,查看是否从私服中下载
第一种方法
在settings.xml配置文件里配置私服的镜像文件
<!-- 配置私服的镜像-->
<mirror>
<id>nexusmaven</id> <!-- id名称 -->
<mirrorOf>*</mirrorOf> <!-- 表示拦截所有的请求,都重定向到私服,从私服下载jar包,私服没有再去中央仓库下载 -->
<name>Nexus maven</name>
<!-- 私服的组地址 -->
<url>http://localhost:8088/nexus/content/groups/public/</url>
</mirror>
HelloFriend的pom.xml文件中:
<dependencies>
<!--依赖Hello这个项目-->
<dependency>
<groupId>cn.tx.maven</groupId>
<artifactId>Hello</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
第二种方法
HelloFriend的pom.xml文件中:
<dependencies>
<!--依赖Hello这个项目-->
<dependency>
<groupId>cn.tx.maven</groupId>
<artifactId>Hello</artifactId>
<version>1.0-RELEASES</version>
<scope>compile</scope>
</dependency>
</dependencies>
<!-- 加个这个 -->
<repositories>
<repository><!-- 告诉Maven可以从这个仓库下载releases和snapshots版本的jar包-->
<id>nexus</id>
<name>nexusmaven</name>
<url>http://localhost:8088/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<url>http://localhost:8088/nexus/content/groups/public/</url>
<name>pluginRepositories</name>
</pluginRepository>
</pluginRepositories>
注:这个方式需要在每一个pom.xml文件中添加相同的配置
第三种方法
在settings.xml配置文件里添加配置
<!-- 下载jar包配置 -->
<profile>
<!--profile的id -->
<id>dev</id>
<repositories>
<repository> <!--仓库id,repositories可以配置多个仓库,保证id不重复 -->
<id>nexus</id> <!--仓库地址,即nexus仓库组的地址 -->
<url>http://localhost:8079/nexus/content/groups/public/</url> <!--是否下载releases构件 -->
<releases>
<enabled>true</enabled>
</releases> <!--是否下载snapshots构件 -->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories> <!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件 -->
<pluginRepository> <!-- 插件仓库的id不允许重复,如果重复后边配置会覆盖前边 -->
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8079/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
<!--激活配置-->
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
4.第三方jar包发布到私服
(1)在settings.xml配置文件中配置用户名密码
<!-- 定义第三方的版本的id名称,用户名密码 -->
<server>
<id>thirdparty</id>
<username>admin</username>
<password>admin123</password>
</server>
(2)用管理员运行cmd,切换到D:\nexus\nexus-2.12.0-01\bin目录,上传私服
上传到私服的命令
mvn deploy:deploy-file -Dmaven.test.skip=true -DgroupId=fastjson -DartifactId=com.fastjson-Dversion=1.2.66 -Dpackaging=jar -Dfile=D:\repository\com\alibaba\fastjson\1.2.66\fastjson-1.2.66.jar -Durl=http://locahost:8088/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty
查看私服: