目前maven私有仓库都是通过nexus来搭建的,所以本文也以nexus这个工具来搭建maven私有仓库
maven下载组件包的过程示意:
一、maven私有仓库搭建
注:最新版本为3.x,不过大多数公司仍然使用2.x,所以这里以安装2.x为例
1、下载源码
下载地址:https://www.sonatype.com/download-oss-sonatype
wget https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.14.5-02-bundle.tar.gz
2、解压并启动服务
官方文档:https://help.sonatype.com/display/NXRM2
(1)解压
tar -zxvf nexus-2.14.5-02-bundle.tar.gz
会出现两个文件夹
- nexus-2.14.5-02
- sonatype-work
(2)移动文件夹
cd /usr/local/src
mv nexus-2.14.5-02 /usr/local/nexus
mv sonatype-work /usr/local/sonatype-work
chown -R shixinke:shixinke /usr/local/nexus
chown -R shixinke:shixinke /usr/local/sonatype-work
(3)修改nexus的配置
cd /usr/local/nexus
vim conf/nexus.properties
application-port=8082
- 注:这里可以修改nexus的访问端口,默认为8081
(3)启动nexus
bin/nexus start
在控制台有Started Nexus OSS.等日志输出时,可以在浏览器中打开 http://ip:port/nexus
访问,如http://192.168.0.100:8082/nexus/
,如果成功访问,则表明启动成功(这个启动需要一定的时间,请耐心等候)
二、nexus常用操作
1、仓库类型
- hosted 类型的仓库,内部项目的发布仓库。代表宿主仓库,用来发布一些第三方不允许的组件,比如oracle驱动、比如商业软件jar包
- releases 内部的模块中release模块的发布仓库
- snapshots 发布内部的SNAPSHOT模块的仓库
- 3rd party 第三方依赖的仓库,这个数据通常是由内部人员自行下载之后发布上去
- proxy 类型的仓库,从远程中央仓库中寻找数据的仓库。表代理远程的仓库,最典型的就是Maven官方中央仓库、JBoss仓库等等
- group 类型的仓库,组仓库用来方便我们开发人员进行设置的仓库
2、登录
默认用户名有admin,密码为admin123,需要登录后修改
3、更新中央仓库的索引
(1)自动更新
将远程仓库修改为阿里云的仓库,默认的apache的仓库因为网络的问题更新起来比较慢
阿里云中央仓库地址:http://maven.aliyun.com/nexus/content/repositories/central/
(2)手动更新
上面的方式通过网络更新更新索引,比较慢,下面使用手动更新索引(推荐)
- 第一步:下载索引文件:
进入:http://repo.maven.apache.org/maven2/.index/,下载以下2个文件(在页面最底部)
第二步:下载生成索引的工具indexer-cli,到http://search.maven.org搜索indexer-cli(org.apache.maven.indexer),通过maven下载或者手动下载jar包
第三步:将下载的索引文件和indexer-cli放到同一个文件夹(/usr/local/src),通过indexer-cli生成索引:
/usr/local/nexus/bin/nexus stop
cd /usr/local/src
java -jar indexer-cli-6.0.0.jar -u nexus-maven-repository-index.gz -d indexer
注:
(a) 更新索引前请关闭nexus服务,另外将自动下载远程的索引的选项设置
(b)生成索引可能需要花费一定的时间(大约2分钟左右),请耐心等待
- 第四步:将生成的索引入入nexus索引目录中
rm -rf /usr/local/sonatype-work/nexus/indexer/central-ctx/*
cp /usr/local/src/indexer/* /usr/local/sonatype-work/nexus/indexer/central-ctx/
/usr/local/nexus/bin/nexus start
- 第五步:浏览索引,即可查看已经有相关的索引的,搜索”spring”即可查看到有相关的包了
三、将本地代码发布到私有仓库中
1、修改maven配置文件,将私有仓库加入到配置文件中
maven配置文件分为全局配置文件${MAVEN_HOME}/conf/settings.xml和用户配置文件~/.m2/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">
<pluginGroups>
<!-- pluginGroup
| Specifies a further group identifier to use for plugin lookup.
<pluginGroup>com.your.plugins</pluginGroup>
-->
</pluginGroups>
<proxies>
<!-- proxy
| Specification for one proxy, to be used in connecting to the network.
|
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
-->
</proxies>
<!-- 私服仓库用户名和密码 -->
<servers>
<server>
<id>shixinke-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>shixinke-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<mirrors>
</mirrors>
<profiles>
<!--仓库地址配置,与上面的Server的id要对应-->
<profile>
<id>shixinke</id>
<repositories>
<repository>
<id>shixinke-release</id>
<name>local private nexus</name>
<url>http://10.2.82.56:8091/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>shixinke-snapshots</id>
<name>local private nexus</name>
<url>http://10.2.82.56:8091/nexus/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>shixinke-release</id>
<name>local private nexus</name>
<url>http://10.2.82.56:8091/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>shixinke-snapshots</id>
<name>local private nexus</name>
<url>http://10.2.82.56:8091/nexus/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<!--make the profile active all the time -->
<activeProfile>shixinke</activeProfile>
</activeProfiles>
</settings>
2、在发布jar项目的pom.xml中添加仓库配置
在project节点下面添加如下内容:
<distributionManagement>
<repository>
<id>shixinke-releases</id>
<name>Release Repository</name>
<url>http://10.2.82.56:8091/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>shixinke-snapshots</id>
<name>Snapshot Repository</name>
<url>http://10.2.82.56:8091/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
3、编写jar包程序,如:
package com.shixinke.farm.client.data;
import java.io.Serializable;
import java.util.Date;
/**
* @author shixinke [ishixinke@qq.com]
* @date 17-12-13.
*/
public class UserData implements Serializable {
private static final long serialVersionUID = 4202390739709155471L;
private int userId;
private String account;
private String password;
private String salt;
private String nickname;
private String email;
private String mobile;
private Date lastLoginTime;
private String lastLoginIp;
private Short status;
private Date createTime;
private Date updateTime;
public int getUserId() {
return userId;
}
public String getAccount() {
return account;
}
public String getPassword() {
return password;
}
public String getSalt() {
return salt;
}
public String getNickname() {
return nickname;
}
public String getEmail() {
return email;
}
public String getMobile() {
return mobile;
}
public Date getLastLoginTime() {
return lastLoginTime;
}
public String getLastLoginIp() {
return lastLoginIp;
}
public Short getStatus() {
return status;
}
public Date getCreateTime() {
return createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUserId(int userId) {
this.userId = userId;
}
public void setAccount(String account) {
this.account = account;
}
public void setPassword(String password) {
this.password = password;
}
public void setSalt(String salt) {
this.salt = salt;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public void setEmail(String email) {
this.email = email;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public void setLastLoginTime(Date lastLoginTime) {
this.lastLoginTime = lastLoginTime;
}
public void setLastLoginIp(String lastLoginIp) {
this.lastLoginIp = lastLoginIp;
}
public void setStatus(Short status) {
this.status = status;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}
4、发布jar包
在idea编辑器中依次打开 “View”->”Tool Windows”->”Maven Porjects”打开maven面板,或者通过maven的部署插件来做
在maven面板中点击deploy即可:
5、到私服仓库查看刚部署的jar包
6、在其他项目中就可以使用刚部署的包了
<dependency>
<groupId>com.shixinke.farm.client</groupId>
<artifactId>farm-client</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>