- 问题
- 通过中央仓库来获取所需构件,实际开发中往往是存在问题
- 网速慢,下载jar包时间久,工作效率低
- 不利于公共构件的管理和维护
- 通过中央仓库来获取所需构件,实际开发中往往是存在问题
- 解决方案
- 搭建maven私服
- 本地仓库下载速度优于远程仓库下载
- 可自行进行构件的管理和维护
- 第三方jar包
- 项目模块所依赖的公共构件
- 搭建maven私服
- 原理图
- Apache Archiva
- Artifactory
- Sonatype Nexus
- 强大的Maven仓库管理器,简化了内部仓库的维护和外部仓库的访问
- 不需要数据库,它使用文件系统加Lucene来组织数据
- 官网:http://www.sonatype.org/nexus/
- 提供两种安装包
- bundle(内嵌Jetty容器)
- war
- nexus-2.12.0-01-bundle.tar.gz
- http://www.sonatype.org/nexus/archived/
- 安装环境准备
- Centos 6.4 64位
- JDK 1.8
- nexus-2.12.0-01-bundle.tar.gz
- 安装配置
- 上传nexus-2.12.0-01-bundle.tar.gz到/usr/local/nexus
- 解压tar
- tar -zxvf nexus-2.12.0-01-bundle.tar.gz
- 配置文件
- conf/nexus.properties
- 启动
- /usr/local/nexus/nexus-2.12.0-01/bin/nexus start
- 注意:配置 bin/nexus (RUNASUSER=root)
-
-
- nano /usr/local/nexus/nexus-2.12.0-01/bin/nexus
- 默认端口:8081
- 默认用户名/密码:admin/admin123
- 访问:http://服务器IP:8081/nexus/
-
- 登陆
- 登陆成功后
- 开启远程索引(自动更新,非常慢,从美国下载,可以挂vpn下载)
- Central Repository
- Download Remote Indexes:True
-
- 通过scheduleTask查看索引跟新进度
- 手动更新索引
- nexus-maven-repository-index.gz
- nexus-maven-repository-index.properties
- 下载地址:http://repo.maven.apache.org/maven2/.index/
- indexer-cli-5.1.0.jar
- 下载地址:http://maven.outofmemory.cn/org.apache.maven.indexer/indexer-cli/5.1.0/
- 执行命令
- chmod 777 indexer-cli-5.1.0.jar nexus-maven-repository-index.gz nexus-maven-repository-index.properties
- java -jar indexer-cli-5.1.0.jar -u nexus-maven-repository-index.gz -d indexer
- 停止nexus
- /usr/local/nexus/nexus-2.12.0-01/bin/nexus stop
- 删除原有的索引文件
- cd /usr/local/nexus/sonatype-work/nexus/indexer/central-ctx/
- rm -r *
- 将自己手动生成的索引拷贝到nexus的/central-ctx/下
- cd /usr/local/nexus/index/indexer
- cp -r * /usr/local/nexus/sonatype-work/nexus/indexer/central-ctx/
- 重启nexus即可自动更新索引
- /usr/local/nexus/nexus-2.12.0-01/bin/nexus start
- 四种仓库类型
- Hosted Repository:本地仓库,通常我们会部署自己的构件到这一类型的仓库
- Proxy Repository:代理仓库,它们被用来代理远程的公共仓库,如 Maven 中央仓库
- 可以点击对应的仓库的Configuration页签下RemoteStorage Location 属性的值即被代理的远程仓库的路径
- VirtualRepository:虚拟仓库
- RepositoryGroup:仓库组,用来合并多个 hosted/proxy 仓库
- 两种仓库格式
- maven2
- maven1
- 两种仓库 Policy
- Release(发布版本,即稳定版本)
- Snapshot(资源快照,版本相对不稳定)
- 几个预定义仓库
- 3rdparty
- 存放第三方构件,一些无法从公共仓库获得的构件
- 在仓库的 Artifact Upload页签中可以执行上传操作
- 3rdparty
-
- ApacheSnapshots: 使用代理 ApacheMaven 仓库快照版本的构件仓库
- Central: 用来代理 Maven 中央仓库中发布版本构件的仓库,策略为 Release、只会下载和缓存中央仓库中的发布版本构件。
- Releases: 存放稳定版本的构件。比如:我们完成了一个版本下的组件开发,就可以把它发布到这里
- Snapshots: 存放快照版本的构件。
nexus常规操作
- 修改admin密码
- 在发布构件一般需要帐号和密码
- 新增用户
自动发布构件到nexus仓库
- 在工程的 pom.xml 中添加:distributionManagement
- 发布仓库(distributionManagement):负责管理构件的发布包和其他编译生成的支撑文件,那么我们自主开发的 jar 需要上传到 Nexus 的时候需要在pom中增加 distributionManagement 节点进行自动打包上传。 配置上传的仓库有两个:snapshot 快照仓库和 release发布仓库
<repository>
<id>releases</id><!--ID 需要与你的 release 仓库的 RepositoryID 一致-->
<url>http://192.168.1.6:8081/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id><!--ID 需要与你的 snapshots 仓库的 RepositoryID 一致-->
<url>http://192.168.1.6:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
- 修改本地 maven 的 settings.xml 文件,配置用户名/密码
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
<!--当然若你要上传构件到其他仓库,可依照如下代码进行配置-->
<server>
<id>thirdparty</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>central</id>
<username>admin</username>
<password>admin123</password>
</server>
- 在 idea 中,工程所在目录下执行 mvn 命令(mvn:deploy) 将最终版本的包拷贝到远程的 Repository,使得其他的开发者或者工程项目可以共享。所部署的包就自动上传到了Nexus指定的仓库里
- 也可以手动去部署
- deploy:deploy-file -DgroupId=com.itrip -DartifactId=itrip-beans -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=D:\code\idea\itripback\itrip-beans\target\itrip-beans-1.0-SNAPSHOT.jar -Durl=http://192.168.1.5:8081/nexus/content/repositories/snapshots/-DrepositoryId=snapshots -DrepositoryId=snapshots
- DgroupId 和 DartifactId 构成了该 jar 包在 pom.xml 的坐标,项目就是依靠这两个属性定位。
- Dfile 表示需要上传的 jar 包的绝对路径。
- Durl 私服上仓库的位置,打开 nexus——>repositories 菜单,可以看到该路径。
- DrepositoryId 服务器的表示 id,在 Nexus 的 configuration 可以看到。
- Dversion 表示版本信息
配置从nexus私服下载构件
- 首先配置maven镜像
- 配置镜像目的就是让 Maven 只使用私服,修改 Maven 的 setting.xml 文件
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<name>local nexus repository</name>
<url>http://192.168.1.5:8081/nexus/content/groups/public/</url>
</mirror>- 在 pom 文件不做任何配置的情况下,默认是使用 id 为 central 的Maven中央库进行配置的。那么以上配置只取代了pom中的下载仓库,覆盖了 maven 默认的配置。 mirrorOf 配置为*,表示这个镜像配置适用于所有的仓库,是所有仓库的一个镜像, maven 无论要去那个仓库下载构件都会跳转到镜像URL去下载
- 修改项目的pom文件,设置下载仓库
<repository>
<id>public</id>
<name>TeamMavenRepository</name>
<url>http://192.168.1.5:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled><!--是否支持更新-->
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>- 上述配置只对当前项目有效,若需让本机所有Maven项目均使用该 Mavne私服,应该在 setting.xml中进行配置
- 通过在settings.xml中配置profile元素, 可以让本机所有的Maven项目都使用Maven私服
<profile>
<id>public</id>
<repositories>
<repository>
<id>public</id>
<name>Nexus</name>
<url>http://192.168.1.5:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<!--用来激活指定的profile-->
<activeProfiles>
<activeProfile>public</activeProfile>
</activeProfiles>