您的位置:首页 > 其它

Jenkins 持续集成平台构建之使用nexus搭建maven私有仓库

2016-06-23 11:18 971 查看
一、介绍:
上一篇文章介绍了maven的开放仓库无法满足构建需求,第一个原因是无法访问,第二个原因是不稳定,时而可以访问时而不可以,而且速度无法保证,这里介绍在本地通过nexus来搭建自己的私有仓库,这样就既稳定又快速

环境介绍:Centos 6.6 x86_64 IP: 10.0.90.26
软件版本:nexus-2.13.0-01-bundle.tar.gz
二、开始安装配置
1、下载nexus http://www.sonatype.org/nexus/go ## 翻墙下载速度会快些
2、安装
#tar xf nexus-2.13.0-01-bundle.tar.gz -C /usr/local/
#cd /usr/local
#ln -sv nexus-2.13.0-01/ nexus
启动
#cd nexus
#./bin/nexus    #查看用法
Usage: ./bin/nexus { console | start | stop | restart | status | dump }
# ./bin/nexus start
****************************************
WARNING - NOT RECOMMENDED TO RUN AS ROOT
****************************************
If you insist running as root, then set the environment variable RUN_AS_USER=root before running this script.
设置环境变量
#vi /etc/profile    ##在末尾添加如下:
export RUN_AS_USER=root
source环境变量文件,使其生效
#source  /etc/profile
重新再次启动
#./bin/nexus start
****************************************
WARNING - NOT RECOMMENDED TO RUN AS ROOT
****************************************
Starting Nexus OSS...
Started Nexus OSS.
查看端口,默认端口是8081,如下:
#netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:8081                0.0.0.0:*                   LISTEN      25425/java
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      958/sshd
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1035/master
tcp        0      0 127.0.0.1:32000             0.0.0.0:*                   LISTEN      25425/java
tcp        0      0 :::22                       :::*                        LISTEN      958/sshd
tcp        0      0 ::1:25                      :::*                        LISTEN      1035/master
查看生成的文件:
#cd /usr/local
#ll
total 60
drwxr-xr-x.  2 root root 4096 Sep 23  2011 bin
drwxr-xr-x.  2 root root 4096 Sep 23  2011 etc
drwxr-xr-x.  2 root root 4096 Sep 23  2011 games
drwxr-xr-x.  2 root root 4096 Sep 23  2011 include
drwxr-xr-x.  2 root root 4096 Sep 23  2011 lib
drwxr-xr-x.  2 root root 4096 Sep 23  2011 lib64
drwxr-xr-x.  2 root root 4096 Sep 23  2011 libexec
drwxr-xr-x.  6 root root 4096 Apr 11 11:24 logstash
lrwxrwxrwx   1 root root   16 Jun 22 18:30 nexus -> nexus-2.13.0-01/   #nexus服务主目录
drwxr-xr-x   8 1001 1001 4096 Apr 12 22:11 nexus-2.13.0-01
drwxr-xr-x.  2 root root 4096 Sep 23  2011 sbin
drwxr-xr-x.  5 root root 4096 Aug  6  2015 share
drwxr-xr-x   3 1001 1001 4096 Apr 12 22:21 sonatype-work  #是真正的仓库目录
drwxr-xr-x.  3 root root 4096 Jun  8 10:56 src
如果想修改默认端口,就到nexus配置文件中进行修改
#/usr/local/nexus/conf
#vi nexus.properties
application-port=8081   ##将8081修改为自己想要的端口
我这里不修改了,用默认端口!
修改nexus目录结构下面文件的属主和属组,因为默认是下面的属性:
#cd /usr/local/nexus
# ll
total 40
drwxr-xr-x 3 1001 1001  4096 Jun 22 18:30 bin
drwxr-xr-x 2 1001 1001  4096 Jun 22 18:30 conf
drwxr-xr-x 2 1001 1001  4096 Jun 22 18:30 lib
-rw-r--r-- 1 1001 1001 11006 Apr 12 22:11 LICENSE.txt
drwxr-xr-x 2 1001 1001  4096 Jun 22 18:36 logs
drwxr-xr-x 4 1001 1001  4096 Jun 22 18:30 nexus
-rw-r--r-- 1 1001 1001   782 Apr 12 22:11 NOTICE.txt
drwxr-xr-x 5 1001 1001  4096 Jun 22 18:37 tmp
#chown root.root ./* -R
3、在浏览器访问:
http://10.0.90.26:8081/nexus 界面如下:



右上角有一个“Log In”按钮,可以登录,默认用户名为:admin 密码:admin123,登录进去之后,可以修改密码,点击“Profile”



然后弹出如下修改密码方框,设置密码即可:



退出,重新登录,查看信息:




Configuration指向的方框中的3rd party、Snapshots、Releases这三个,分别用来保存第三方jar、项目组内部的快照、项目组内部的发布版。
4、配置定时任务
有时候,为了让nexus更加智能,需要做一些定时任务,譬如定期下载索引,加快本地mvn检索速度。以建立定期下载索引为例,在Administration选项中找到Scheduled Tasks,在窗口页面点击Add,进行配置,如下图:



三、nexus的一些简单配置
1、将Central这个Repositories设置为Health Check(默认是不进行Health Check的),配置如下图:




然后过一会儿Central就会变成如下的状态:



2、设置一个用户,为了在maven的settings.xml配置使用,方法如下:



选择“Set Password”之后,在弹出的方框中输入密码,然后选择“Set Password”就成功设置密码了。
3、测试设置自建maven中央仓库来构建
到maven服务器上配置
#cd /usr/local/maven/conf
将settings.xml修改为如下:
#cat 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"> 
<!-- <localRepository>D:\bisoft\tools\maven3\repository</localRepository>  -->

<pluginGroups>
<!-- <pluginGroup>org.mortbay.jetty</pluginGroup>
<pluginGroup>org.codehaus.cargo</pluginGroup>   -->
</pluginGroups>

<proxies>
</proxies>

<servers>
<server>
<id>releases</id>
<username>deployment</username>    ##上一步设置的用户名和密码
<password>deployment</password>
</server>
<server>
<id>snapshots</id>
<username>deployment</username>    ##上一步设置的用户名和密码
<password>deployment</password>
</server>
</servers>

<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://10.0.90.26:8081/nexus/content/groups/public/</url>  #自建仓库的url
</mirror>
</mirrors>

<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<name>Nexus</name>
<url>http://10.0.90.26:8081/nexus/content/groups/public/</url> #自建仓库的url
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus-osc</id>
<name>Nexus osc</name>
<url>http://10.0.90.26:8081/nexus/content/groups/public/</url> #自建仓库的url
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>

<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>

</settings>
保存退出后,开始测试构建!
4、将jar部署至Nexus
通过Eclipse Maven工具,或者直接操作Maven命令行,将jar部署至nexus,代码中的pom.xml文件,需要修改为自己的私服地址,如下:
<project>
...           #内容太多,省略了一些
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://<Your Nexus IP>/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://<Your Nexus IP>/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>
保存退出!
以test_appios项目为例,具体如何添加git拉取代码,如何结合maven,上一篇文章已经介绍了,不在赘述,配置好如下图:
代码管理部分:



构建部分:这里Goals选项我使用如下参数,如果自己懂得如何修改配置这些参数,可以自定义,POM选项一定要配置为pom.xml因为我们是使用maven来构建代码的!



开始构建:



然后出现淡蓝色圆点表示构建成功(构建失败是淡红色圆点),点击“#11”--“Console Output”,查看构建过程log,如下:



到Jenkins服务端查看构建成功的war格式的代码包
#/root/.jenkins/workspace/test_appios/target
#ll
total 31412
drwxr-xr-x 6 root root     4096 Jun 23 10:33 classes
drwxr-xr-x 3 root root     4096 Jun 23 10:33 generated-sources
drwxr-xr-x 2 root root     4096 Jun 23 10:33 maven-archiver
drwxr-xr-x 5 root root     4096 Jun 23 10:33 siteService
-rw-r--r-- 1 root root   218500 Jun 23 10:33 siteService-sources.jar
-rw-r--r-- 1 root root 31924278 Jun 23 10:33 siteService.war
可以看到siteService.war就是构建成功的war包。
PS:使用maven构建代码包,在源码中,java开发工程师都会定义pom.xml文件,这个文件是maven工程中的核心文件,定义了一个项目代码工程的jar依赖包,如果pom.xml中定义了依赖某一个jar包,在自定义的私有maven仓库中找不到,在构建的时候就会报错,提示jar包不存在等error信息,所以,一定要特别注意pom.xml文件。而且每个公司的项目不同,java代码也不同,而且有些构建依赖的jar包可以是通用的,但是有些jar包是java开发工程师写好的,然后使用maven命令上传到私有仓库中的,只满足本公司项目的需求!
四、其他说明
1、maven中央仓库的特性
每个公司惯用的方式不同,本篇文章介绍的只是其中一个例子,不能涵盖所有情况,而且每个公司所开发的java项目也是不同的,有时候需要开发工程师开发好一些符合公司个性的jar包,然后上传到nexus,作为maven仓库的一部分。有时候需要从互联网下载第三方的jar包,上传到nexus,所以需要根据具体的需求,来配置maven仓库
2、上传jar包
想要将一些自建jar包或者第三方jar包上传到nexus,需要通过“3rd Party”进行,如下一个例子:



点击“Select Artifact(s) to Upload”后,会弹出windows方框,选择需要上传的jar包,如下:



选择之后,会出现如下图界面:



然后选择“Add Artifact”,就会将选择的jar包同步到Artifacts下面的方框中,如下:



然后选择“Upload Artifacts”,就上传了,上传成功,会弹出一个成功的界面,如下:



3、查看上传的jar包

上传完成之后,可以验证下是否已经成功了,到“3rd party”这个第三方Repository查看,如下:



注:目录结构相信大家已经看出来是如何设置的了,就是在上传的时候,选择的“Group”、“Artifact”、“Version”。因为“Public Repositories”这个公用的Repositories是包含3rd party的,所以在“Public Repositories”下面也是可以查看到刚才上传的jar的。
4、nexus 仓库某些目录介绍
/nexus/sonatype-work/nexus/storage/ #项目中的各种jar包和项目快照等都放在 目录下,在这个目录下包括以下各种目录和存放相应文件。
/nexus/sonatype-work/nexus/storage/central #用于放置maven从中央仓库中下载下来的项目pom.xml中配置到的相关jar包;
/nexus/sonatype-work/nexus/storage/thirdparty #用于放置自己手动上传的第三方jar包
/nexus/sonatype-work/nexus/storage/releases #用于放置项目deploy后的发布版

阿里云的私有maven仓库配置参考链接:https://yq.aliyun.com/articles/6059
其他参考链接:http://snowolf.iteye.com/blog/1605811
不足之处请多多指出!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息