柴少的官方网站 技术在学习中进步,水平在分享中升华

Jenkins自建maven仓库(四)

#我们继续围绕了jenkins发布做优化,前面已经看到了只要涉及到mvn clean install就会涉及到相关jar包的下载,如果这时候所要访问的公网仓库有网络问题等情况,或者说我们的构建机根本不上让访问公网,这时候为了保证编译效率,那么就要自建仓库了,自建仓库之后我们也可以写一个公共组件库上传到自建的maven仓库中。

Nexus是一个强大的Maven仓库管理器,它极大地简化了自己内部仓库的维护和外部仓库的访问。利用Nexus你可以只在一个地方就能够完全控制访问和部署在你所维护仓库中的每个Artifact。Nexus是一套“开箱即用”的系统不需要数据库,它使用文件系统加Lucene来组织数据。Nexus使用ExtJS来开发界面,利用Restlet来提供完整的REST APIs,通过m2eclipse与Eclipse集成使用。Nexus支持WebDAV与LDAP安全身份认证。

一、Centos使用nexus创建maven私服

1.1 下载软件包并启动服务

下载nexus:

http://www.sonatype.org/nexus/downloads/

下载链接:https://www.sonatype.com/download-oss-sonatype?hsCtaTracking=920dd7b5-7ef3-47fe-9600-10fecad8aa32%7Cf59d5f10-099f-4c66-a622-0254373f4a92

#oss版本是免费开源版本

image.png

#wget https://sonatype-download.global.ssl.fastly.net/repository/repositoryManager/3/nexus-3.14.0-04-unix.tar.gz

#tar zxf nexus-3.14.0-04-unix.tar.gz 

# cp -rf nexus-3.14.0-04 /usr/local/

#ln -s /usr/local/nexus-3.14.0-04 /usr/local/nexus

一些配置(可选):

# vim /usr/local/nexus/bin/nexus.rc  #这里是控制启动用户的

#run_as_user=""   #默认是root用户启动,当然你可以去掉注释如:run_as_user="nexus"就变成nexus用户启动了

# vim /usr/local/nexus/bin/nexus.vmoptions   #这个文件有一些启动参数,这里修改占用内存,存储,日志的位置等

-Xms1200M
-Xmx1200M
-XX:MaxDirectMemorySize=2G
-XX:+UnlockDiagnosticVMOptions
-XX:+UnsyncloadClass
-XX:+LogVMOutput
-XX:LogFile=../sonatype-work/nexus3/log/jvm.log
-XX:-OmitStackTraceInFastThrow
-Djava.net.preferIPv4Stack=true
-Dkaraf.home=.
-Dkaraf.base=.
-Dkaraf.etc=etc/karaf
-Djava.util.logging.config.file=etc/karaf/java.util.logging.properties
-Dkaraf.data=../sonatype-work/nexus3  
-Djava.io.tmpdir=../sonatype-work/nexus3/tmp
-Dkaraf.startLocalConsole=false

# vim /usr/local/nexus/etc/nexus-default.properties   #修改端口啊监听地址在这个配置文件中

## DO NOT EDIT - CUSTOMIZATIONS BELONG IN $data-dir/etc/nexus.properties
##
# Jetty section
application-port=8081
application-host=0.0.0.0
nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml
nexus-context-path=/

# Nexus section
nexus-edition=nexus-pro-edition
nexus-features=\
 nexus-pro-feature

# vim /usr/local/nexus/bin/nexus   #修改jdk位置什么的在这个启动文件

INSTALL4J_JAVA_HOME_OVERRIDE=/usr/java/jdk   #默认这里是#INSTALL4J_JAVA_HOME_OVERRIDE=  但是如果提示找不到jdk可以修改这里

设置开机启动:

# vim /usr/lib/systemd/system/nexus.service

[Unit]                                                 
Description=nexus service
 
[Service]
Type=forking
LimitNOFILE=65536 
ExecStart=/usr/local/nexus/bin/nexus start
ExecReload=/usr/local/nexus/bin/nexus restart
ExecStop=/usr/local/nexus/bin/nexus stop
Restart=on-failure
 
[Install]
WantedBy=multi-user.target

# service nexus start  

# systemctl enable nexus

查看一下:

# tail -f /usr/local/sonatype-work/nexus3/log/nexus.log 

2018-11-05 17:19:10,474+0800 INFO  [quartz-2-thread-1]  *SYSTEM org.sonatype.nexus.repository.search.SearchFacetImpl - Rebuilding index of repository nuget.org-proxy
2018-11-05 17:19:10,478+0800 INFO  [jetty-main-1]  *SYSTEM org.eclipse.jetty.server.AbstractConnector - Started ServerConnector@5784b57f{HTTP/1.1,[http/1.1]}{0.0.0.0:8081}
2018-11-05 17:19:10,478+0800 INFO  [jetty-main-1]  *SYSTEM org.eclipse.jetty.server.Server - Started @48460ms
2018-11-05 17:19:10,478+0800 INFO  [jetty-main-1]  *SYSTEM org.sonatype.nexus.bootstrap.jetty.JettyServer - 
-------------------------------------------------

Started Sonatype Nexus OSS 3.14.0-04    #出现这个表示启动了

# netstat  -lntup|grep 8081

tcp        0      0 0.0.0.0:8081            0.0.0.0:*               LISTEN      10457/java

image.png

#点击右上角的Sign in就可以登陆了,默认管理员用户是admin,密码是admin123

博文来自:www.51niux.com

2.2 进入管理界面进行配置

用户了解:

image.png

admin:该用户拥有Nexus的全部权限,默认密码为admin123。
#deployment:该用户能够访问Nexus,浏览仓库内容、搜索、上传部署构件,但是不能对Nexus进行任何配置,默认密码为deployment123。 #这个用户显示在新版里面已经没有了
anonymous:该用户对应了所有未登录的匿名用户,它们可以浏览仓库并进行搜索。

先了解仓库:

image.png

#maven-central:maven中央库,默认从https://repo1.maven.org/maven2/拉取jar 

#从上图可以看到有三种Type类型:proxy,group.hosted

hosted,本地仓库,通常我们会部署自己的构件到这一类型的仓库,比如公司的第二方库。
proxy,代理仓库,它们被用来代理远程的公共仓库,如 maven中央仓库。
group,仓库组,用来合并多个hosted/proxy仓库,当项目希望在多个repository使用资源时就不需要多次引用了,只需要引用一个group即可。

#从上图看到现在只预留了两个本地仓库:

Releases: 这里存放我们自己项目中发布的构建, 通常是Release版本的, 比如自己做了一个Web Server的项目, 生成的构件为webserver.war, 我们就可以把这个构建发布到Nexus的Releases本地仓库
Snapshots: 这个仓库非常的有用, 它的目的是让我们可以发布那些非release版本, 非稳定版本, 比如我们在trunk下开发一个项目,在正式release之前你可能需要临时发布一个版本给其他同事使用, 
因为同事也正在依赖你的模块开发, 那么这个时候就可以发布Snapshot版本到这个仓库, 同事就可以通过简单的命令来获取和使用这个临时版本.
#当然还是可以创建一个3rd Party第三方库,就是需要创建。3rd Party: 顾名思义,第三方库,这里的是指可以添加自己的第三方库, 比如有些构件在中央仓库是不存在的.比如在中央仓库找不到Oracle 的JDBC驱动, 这个时候就需要自己添加到3rd party仓库.

#主要是这个maven-pubic:

类型为group,这个简单理解为仓库的集合,下面的仓库就是可以加入到这个集合的元素。对外可以只提供一个单独的url,如上图显示为:http://192.168.1.104:8081/repository/maven-public/
大部分的终端用户,只需要配置上面这一个单独的聚合后的url,而不用单独配置多个仓库。用户也不需要知道某个jar包具体来源于maven 中央仓库,或者是Apache Snapshots,或者是我们自己添加的其他仓库。
这样的好处在于:如果我们要新增某个仓库(如开源中国、阿里云),客户端不需要做任何更改,只需要在nexus上将要新增的仓库加入到对外提供服务的仓库 group中就可以。

增加新的代理源:

image.png

image.png

image.png

 逐个增加常用代理:

1. aliyun

http://maven.aliyun.com/nexus/content/groups/public

2. apache_snapshot

https://repository.apache.org/content/repositories/snapshots/

3. apache_release

https://repository.apache.org/content/repositories/releases/

4. atlassian

https://maven.atlassian.com/content/repositories/atlassian-public/

5. central.maven.org

http://central.maven.org/maven2/

6. datanucleus

http://www.datanucleus.org/downloads/maven2

7. maven-central (安装后自带,仅需设置Cache有效期即可)

https://repo1.maven.org/maven2/

8. nexus.axiomalaska.com

http://nexus.axiomalaska.com/nexus/content/repositories/public

9. oss.sonatype.org

https://oss.sonatype.org/content/repositories/snapshots

10.pentaho

https://public.nexus.pentaho.org/content/groups/omni/

设置maven-public:

image.png

image.png

博文来自:www.51niux.com

设置私用仓库可重复发送:

Nexus安装后自带maven-releases,maven-snapshots两个仓库,用于将生成的jar包发布在这两个仓库中,在实际开发中需要将maven-releases设置为可以重复发布。

image.png

#maven-snapshots缺省是可以重新部署的。

2.3 maven配置使用私有仓库

# vim /usr/local/apache-maven/conf/settings.xml   #先来个最简单的使用配置

  <mirrors>
     <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>http://192.168.1.104:8081/repository/maven-public/</url>
    </mirror>
  </mirrors>

image.png

#通过jenkins再发布的结果也可以看出来现在走的是私服下来相关依赖包了。

#更多settings.xml的配置:https://helpcdn.aliyun.com/document_detail/57890.html

#maven的setting.xml详解:http://www.cnblogs.com/hwaggLee/p/4579418.html


作者:忙碌的柴少 分类:Jenkins 浏览:9120 评论:0
留言列表
发表评论
来宾的头像