目录
一、私服搭建
在局域网内搭建的maven的远程仓库服务器称为私服。
1. 下载nexus
Nexus是Maven仓库管理器,可以搭建maven仓库,同时提供仓库管理功能,构件搜索功能等。
下载链接:http://www.sonatype.org/nexus/archived/
2. 安装nexus
下载后解压,cmd管理员模式下进入bin目录,执行命令:nexus.bat install
3. 启动nexus
方式1:cmd进入bin目录,执行nexus.bat start
方式2:windows服务列表那里启动
查看nexus的配置文件conf/nexus.properties,如下图:
访问:http://localhost:8081/nexus
可以使用Nexus内置账户admin=admin123登录。
二、Nexus的仓库类型
Nexus的仓库有4种类型:
1. hosted:
宿主仓库,部署自己的jar到这个类型的仓库,包括releases和snapshot,以及引用第三方的jar包。
2. proxy:
代理仓库,用于代理远程的公共仓库,如maven中央仓库,用户连接私服,私服自动去中央仓库下载jar包或者插件。
3. group:
仓库组,用来合并多个hosted/proxy仓库,通常配置maven去连接仓库组。
4. virtual:
兼容Maven1版本的jar或者插件。
Nexus仓库默认在sonatype-work\nexus\storage目录下:
• apache-snapshots:代理仓库,存储snapshots构件,代理地址https://repository.apache.org/snapshots/
• central-m1:virtual类型仓库,兼容Maven1 版本的jar或者插件
• releases:本地仓库,存储releases构件。
• snapshots:本地仓库,存储snapshots构件。
• thirdparty:第三方仓库
• public:仓库组
三、将项目打包发布到私服
- 需求
例子:假设多团队分别开发dao、service、web,某个团队开发完dao将dao发布到私服供service团队使用。
- 配置
①在客户端(即进行部署dao工程的计算机)上配置maven环境,并修改setting.xml文件,配置连接私服的用户和密码。
<servers>
标签下加入:
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
注:
releases:发布版本项目仓库。
snapshorts:测试版本项目仓库。
②配置项目pom.xml
配置私服仓库的地址,本公司的自己的jar包会上传到私服的hosted仓库,根据工程的版本号决定上传到哪个hosted仓库,如果版本为release则上传到release仓库,版本为snapshorts同理。
现在在dao模块的pom.xml文件加入:
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
注:
pom.xml这里 和 settings.xml 配置 对应!
③发布
将项目dao模块打成jar包发布到私服。启动Nexus(这里还需要启动mysql),对dao工程执行deploy命令。
可以看到如下图:
四、从私服上下载Jar包
需求
例子:从私服下载dao工程的jar包。在settings.xml中配置仓库
<profies>
标签下加入:
<profile>
<!--profile的id-->
<id>dev</id>
<repositories>
<repository>
<!--仓库id,repositories可以配置多个仓库,保证id不重复-->
<id>nexus</id>
<!--仓库地址,即nexus仓库组的地址-->
<url>http://localhost:8081/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:8081/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
使用profile定义仓库需要激活才能生效:
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
配置成功后可通过eclipse查看Effective Pom中的repositories验证是否配置成功: