前言
最近一段时间在开发自己的网站,发现好多功能都会涉及到一些相同的工具处理。基于想把日常常见的一些功能工具打包成Jar包然后一直沿用统一编码工具规范的想法。决定给自己搭建一个maven私人服务器来实现jar包的保存以及使用的相关管理工作。 所以,搞起吧~
准备
- 确保centos 上已经安装了java。可以使用下面的命令
rpm -qa |grep java
或者
which java
前者是搜索java是否存在,后者则是假设java已经存在的情况下搜索其安装目录
如果查询不到包含java的相关信息,执行以下命令:
yum install java
一路安装完成即可,最后使用 java -version 查询看是否能够得到版本信息,可以的话则成功。不行的话百度另行试试吧。
- 下载Nexus,进入官网https://www.sonatype.com/download-oss-sonatype下载即可(官网速度较慢,。如果要快可以百度下其他下载方式,这里不多赘述)。这里我选择的是最新的版本nexus-3.16.2-01-unix.tar.gz
PS:如果想要通过wget的方式直接下载,请参考https://help.sonatype.com/repomanager3/download选择对应的包进行下载 - 解压文件,解压以后会生成两个文件,nexus-3.16.2-01以及sonatype-work
tar -zxvf nexus-3.16.2-01-unix.tar.gz
- 进入 nexus-3.16.2-01 文件夹,执行bin/目录下的nexus命令。
cd nexus-3.16.2-01/bin/
nexus start
在浏览器输入http://ip:port:8081,看到下面的页面即表示nexus安装成功
PS: 8081为默认的启动端口,如果不想使用8081或者8081被占用需要更改别的端口号时。可以进入于bi同级目录的etc文件夹下,修改nexus-default.properties
cd /usr/soft/nexus/nexus-3.16.2-01/etc/
vim nexus-default.properties
将 application-port修改成自己想要的端口(0-65535,部分端口不可用,自行查阅)
- 在浏览器打开的页面中,点击sign in登录,默认账户密码是admin/admin123。
- 修改密码。点击右上角人形图标,进入到个人信息修改页面,在里面找到change password选项,按照要求修改密码即可。
- 按照图中操作,进入到repository设置页面
其中aliyun-repository、maven-third-party是个人添加的。
aliyun-repository是连接到阿里maven中央仓库的代理。负责从阿里仓库获取我们需要的部分依赖jar包。maven-third-party是用于存储第三方jar包或者自己定义的jar的。
aliyun-repository配置如下:
选择Maven2 proxy代理类型。具体group,hosted,proxy的区别。可以借鉴下面这个博主(xiliangMa)的讲解,我就不多说了。
Nexus 3 Docker仓库(hosted、proxy、group)模式 讲解
具体仓库名可以自己定义,关键在代理链接的填写
http://maven.aliyun.com/nexus/content/groups/public/
最后点击完成即可。
然后再点击maven-public,进入其管理页面,将左侧的aliyun-repositroy移至右侧同时调整位置到第一位。
至于maven-third-party,参考下面的截图配置即可
- nexus私服的搭建基本结束了。下面就是结合maven进行使用了。
如果不熟悉maven,请自行补充学习,不多赘述。修改maven安装目录下 的conf文件下的setting.xml文件。
在servers里面添加对应的ID以及账户密码。
<server>
<id>maven-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<!--测试版-->
<server>
<id>maven-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>maven-third-party</id>
<username>admin</username>
<password>admin123</password>
</server>
然后在morrors添加
<mirror>
//该ID要和下面的ID对应上
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://ip:port/repository/maven-public/</url>
ip:port是你的服务器地址加端口
</mirror>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>maven-central</id>
<url>http://repo1.maven.org/maven2/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>maven-central</id>
<url>http://repo1.maven.org/maven2/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
最后在settings里面添加
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
激活该节点
<?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">
<localRepository>E:\maven-repoo</localRepository>
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
<server>
<id>maven-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<!--测试版-->
<server>
<id>maven-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>maven-third-party</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://localhost:8081/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>maven-central</id>
<url>http://repo1.maven.org/maven2/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>maven-central</id>
<url>http://repo1.maven.org/maven2/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
接下来在使用maven进行项目构建时,会从私服获取到相对应的依赖,如果私服没有,会自动从阿里云代理仓库以及maven中央仓库获取相应的依赖并且更新存储到私服
9. IDEA通过maven打包推送到私服。
在maven项目的pom.xml中添加下面的代码
<distributionManagement>
<repository>
<id>maven-releases</id>
<name>Internal Release</name>
<url>http://localhost:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>maven-snapshots</id>
<name>Internal Snapshots</name>
<url>http://localhost:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<repositories>
<repository>
<id>maven-public</id>
<url>http:/localhost:8081/repository/maven-public/</url>
</repository>
</repositories>
打包时直接通过maven插件进行即可。使用deploy直接打包推送到私服。
注意:打包分为快照(snapshot )和发行版(release )两种包。具体区别可以参考Maven 中 jar 包的 Snapshot 和 Release 版本区别
打包的时候
version为snapshot 时,会自动推送到maven-snapshots文件夹下存储
version为release时,则推送到maven-releases下
10.如果是推送到第三方maven-third-party下,则操作如下
mvn deploy:deploy-file -DgroupId=cn.test -DartifactId=commons -Dversion=1.0.0 -Dpackaging=jar -Dfile=D:\jar\1.0.0.RELEASE.jar -Durl=http://localhost:8081/repository/maven-snapshots/ -DrepositoryId=maven-third-party
DgroupId、DartifactId、Dversion:构成了该jar包在仓库中的位置
Dpackaging:表示打包类型.
Dfile:表示需要上传的jar包的绝对路径.
Durl:私服上第三方仓库的地址
DrepositoryId:服务器的表示id,就是我们在setting.xml文件中配置的serverId
又或者你觉得记不住太麻烦,直接在界面上传吧,按照下面的步骤填写相关信息