配置maven私服nexus

来源:互联网 发布:软件运营方案书 编辑:程序博客网 时间:2024/05/16 11:04

1、Nexus简介

1.1 Nexus概述

Nexus是一个强大的Maven仓库管理器,它极大地简化了自己内部仓库的维护和外部仓库的访问。

利用Nexus你可以只在一个地方就能够完全控制访问和部署在你所维护仓库中的每个Artifact。

Nexus是一套“开箱即用”的系统不需要数据库,它使用文件系统加Lucene来组织数据。Nexus使 
用ExtJS来开发界面,利用Restlet来提供完整的REST APIs,通过m2eclipse与Eclipse集成使用。

Nexus支持WebDAV与LDAP安全身份认证。

Nexus是一个Maven仓库管理器,用来搭建私有仓库服务器。建立公司/组织的私有仓库的的好处 
是便于管理,节省公网带宽,利用内网下载依赖项速度快,还有一个非常有用的功能就是能有效管 
理内部项目的SNAPSHOT版本,实现各个模块间的共享.

1.2 Maven仓库

1.2.1 仓库结构图

这里写图片描述

1.2.2 本地仓库

本地仓库默认是用户目录下的.m2\repository,用户级的配置文件settings.xml也放在这里。

这里写图片描述

这里写图片描述

1.2.3 远程仓库

1.3.1 远程仓库概述

maven的远程仓库有多种存在形式,中央仓库,其他远程仓库,镜像,私服

1.3.2 中央仓库

中央仓库是默认的远程仓库,如果不做任何特殊配置那么将会从中央仓库下载依赖,这在 
$M2_HOME/lib/maven-model-builder-3.0.4.jar里的org/apache/maven/model/pom-4.0.0.xml里做了指定,如下:

<repositories><repository><id>central</id><name>Central Repository</name><url>http://repo.maven.apache.org/maven2</url><layout>default</layout><snapshots><enabled>false</enabled></snapshots></repository></repositories>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

1.3.3 镜像

镜像及将的maven依赖请求转发至相应服务器,配置如下:

<mirror>    <id>mirrorId</id>    <mirrorOf>repositoryId</mirrorOf>    <name>Human Readable Name for this Mirror.</name>    <url>http://my.repository.com/repo/path</url></mirror>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

这里即是将仓库id为repositoryId的所有请求转发至http://my.repository.com/repo/path镜像服务器镜像更为常用的作法是结合私服,如下配置:

<mirror>    <id>mirrorId</id>    <mirrorOf>*</mirrorOf>    <name>Human Readable Name for this Mirror.</name>    <url>http://my.repository.com/repo/path</url></mirror>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

这里即是将对所有仓库的请求转发至私服http://my.repository.com/repo/path

1.3.4 私服

私服一般采用nexus部署

1.3.5 添加其他远程仓库

1、添加一个其他的远程库只需在项目的pom.xml文件中添加以下配置即可

<repositories>    <repository>        <id>jboss</id>        <name>JBoss Repository</name>        <url>http://repository.jboss.org/nexus/content/groups/public/</url>        <snapshots>        <enabled>false</enabled>        </snapshots>        <releases>            <enabled>true</enabled>        </releases>        <layout>default</layout>    </repository></repositories>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

• snapshots false表示关闭jboss远程仓库的snapshots版本下载 
• releases true表示打开jboss远程仓库的release版本下载

2、测试下jboss仓库是否配置正确,添加一个jboss仓库独有的依赖即可,这里选用xom1.2,配置 
如下

<dependency>    <groupId>xom</groupId>    <artifactId>xom</artifactId>    <version>1.2</version></dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

3、发现下载成功即可 
4、远程服务器的验证,只需在settings.xml添加如下代码即可

<server>    <id>deploymentRepo</id>    <username>repouser</username>    <password>repopwd</password></server>
  • 1
  • 2
  • 3
  • 4
  • 5

注:id要与配置的远程服务器id对应比如jboss.

2、安装与配置Nexus

2.1 安装与基本配置

1.下载安装包 
从官网下载最新的nexus安转包:http://www.sonatype.org/nexus/archived/ 
本文下载的是nexus-2.11.2-06-bundle.tar.gz 版本 
然后 tar xzvf nexus-2.11.2-06-bundle.tar.gz 解压该安装包,解压后会出现这两个文件夹

nexus-2.11.4-01sonatype-work
  • 1
  • 2

这里写图片描述

注:安装nexus,本地仓库默认的jar包存储位置是:/sonatype-work/nexus/storage/central。由于 
Central仓库占用存储较大,所以要注意存储位置。

2.修改配置文件1:/home/nexus/nexus-2.11.4-01/conf/nexus.properties

………………# Sonatype Nexus# ==============# This is the most basic configuration of Nexus.# Jetty section# 配置端口信息application-port=8086application-host=0.0.0.0nexus-webapp=${bundleBasedir}/nexus#配置IP端口访问#http://192.168.10.56:8086/nexus/可修改为: /nexus-webapp-context-path=/nexus# Nexus section#配置work目录地址,Maven仓库JAR缓存地址,可修改其他地址eg:${bundleBasedir}/nexus-work/nexusnexus-work=${bundleBasedir}/../sonatype-work/nexusruntime=${bundleBasedir}/nexus/WEB-INF
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

3:修改配置文件2:nexus-2.11.4-01/bin/nexus

………………#NEXUS_HOME=".."#如果目录改变配置,如果修改了运行路径NEXUS_HOME="/home/nexus/nexus-2.11.1-01"……………………#RUN_AS_USER=#nexus启动用户RUN_AS_USER=jhd
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

注意RUN AS USER一般不推荐设置为root。 
如果开始是以root用户安装 且设置了RUN AS USER为root启动。再去设置RUN AS USER用户的时候 
会出现一系列的错误,主要是由于权限限制可以使用 
sudo chmod -R a+x /usr/local/nexus 设置权限 (这里的目录是nexus的安装位置,例如我的 
sudo chmod -R a+x /home/software/nexus-2.11.2-06-bundle/) 
还需要设置下 /var/run/目录的权限 否则会出现pid无法写入的错误

4.启动nexus服务:

[root@CentOS-Slave1 bin]# ./nexus start****************************************WARNING - NOT RECOMMENDED TO RUN AS ROOT****************************************Starting Nexus OSS...Started Nexus OSS.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

如果启动成功通过访问用浏览器访问:http://ip:8086/nexus/ 会看到以下管理界面:

这里写图片描述

Nexus的默认用户名为admin 密码为admin123

5.设置nexus开机启动:

cp nexus /etc/init.d/chkconfig --add nexuschkconfig nexus on
  • 1
  • 2
  • 3

2.2 本地Maven使用Nexus

1.maven使用本地库 
在maven的setting.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>C:/Users/xuliugen/.m2/repository</localRepository>  <servers>        <server>            <id>releases</id>            <username>admin</username>            <password>admin123</password>        </server>        <server>            <id>snapshots</id>            <username>admin</username>            <password>admin123</password>        </server>    </servers>    <mirrors>        <mirror>            <id>UFindNexus</id>            <mirrorOf>*</mirrorOf>            <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>        </mirror>    </mirrors>    <profiles>        <profile>            <id>UFindNexus</id>            <repositories>                <repository>                    <id>UFindNexus</id>                    <url>http://127.0.0.1:8081/nexus/content/groups/public</url>                    <releases>                        <enabled>true</enabled>                        <updatePolicy>daily</updatePolicy>                        <checksumPolicy>warn</checksumPolicy>                    </releases>                    <snapshots>                        <enabled>true</enabled>                        <checksumPolicy>fail</checksumPolicy>                    </snapshots>                </repository>            </repositories>            <pluginRepositories>                <pluginRepository>                    <id>UFindNexus</id>                    <url>http://127.0.0.1:8081/nexus/content/groups/public</url>                    <releases>                        <enabled>true</enabled>                        <checksumPolicy>warn</checksumPolicy>                    </releases>                    <snapshots>                        <enabled>true</enabled>                        <checksumPolicy>fail</checksumPolicy>                    </snapshots>                </pluginRepository>            </pluginRepositories>        </profile>    </profiles>    <activeProfiles>        <activeProfile>UFindNexus</activeProfile>    </activeProfiles></settings>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

2、如果只想在某个项目中使用本地库则在该项目的pom.xml中

<?xml version="1.0" encoding="UTF-8"?><settings>    <mirrors>        <mirror>            <!--This sends everything else to /public -->            <id>nexus</id>            <mirrorOf>*</mirrorOf>            <!-- <url>http://repo1.maven.org/maven2/</url> -->            <url>http://127.0.0.1:8086/nexus/content/groups/public</url>        </mirror>    </mirrors>    <profiles>        <profile>            <id>nexus</id>            <!--Enable snapshots for the built in central repo to direct -->            <!--all requests to nexus via the mirror -->            <repositories>                <repository>                    <id>central</id>                    <url>http://repo1.maven.org/maven2/</url>                    <releases>                        <enabled>true</enabled>                    </releases>                    <snapshots>                        <enabled>true</enabled>                    </snapshots>                </repository>            </repositories>            <pluginRepositories>                <pluginRepository>                    <id>central</id>                    <url>http://central</url>                    <releases>                        <enabled>true</enabled>                    </releases>                    <snapshots>                        <enabled>true</enabled>                    </snapshots>                </pluginRepository>            </pluginRepositories>        </profile>    </profiles>    <activeProfiles>        <!--make the profile active all the time -->        <activeProfile>nexus</activeProfile>    </activeProfiles></settings>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

2.3 如何部署到远程Nexus

1、在项目文件Pom中添加

<distributionManagement>    <repository>        <id>releases</id>        <name>Nexus Release Repository</name>        <url>http://ip:8081/nexus/content/repositories/releases/</url>    </repository>    <snapshotRepository>        <id>snapshots</id>        <name>Nexus Snapshot Repository</name>        <url>http://ip:8081/nexus/content/repositories/snapshots/</url>    </snapshotRepository></distributionManagement>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

其中id为在setting.xml文件中server所对应的id,一般为默认,无需修改

2.4 验证安装结果

1、在Idea中使用Maven Termianl(快捷键:Alt+F12) 
执行命令:

mvn cleanmvn deploy
  • 1
  • 2

如果在编译的时候还会执行test,请执行:

mvn cleanmvn install -D maven.test.skip=true
  • 1
  • 2

可以看到Terminal中输出一下信息: 
这里写图片描述

表示正将编译之后的jar上传到maven仓库中 
2、查看远程Nexus

这里写图片描述

这里写图片描述

3、其他高级配置

3.1 添加jboss 代理资源库

先了解下nexus的资源库类型:

  • Hosted Repository - nexus本机的资源库(相当于nexus所在服务器硬盘上已经存在的jar、pom文件库)

  • Proxy Repository - 代理库,很多开源官方组织都在互联网上公布了Repository,供全世界的开发者使用,nexus中设置该类型的资源库后,其它使用nexus的开发者,表面上是在请求nexus服务器上的jar包,但nexus实质是背后是在请求这些互联网资源,首次会把服务器上没有的资源,从网上download到nexus服务器(以文件形式保存到硬盘上),然后再返回给开发者,下次再有同样的pom/jar请求时,直接从服务器硬盘上返回,不再请求互联网Repository Group - 资源组,比如我们有很多资源库:资源库A、资源库B…,可以把它们再成组合一个特殊的资源C,然后其它开发人员,在maven里配置资源C地址,这样maven在向资源C请求 
    jar包里,实质会搜索资源A、资源B

1、maven默认的官方资源库http://repo1.maven.org/maven2/,很多第三方的jar包没有,所以我再添加一个jboss的资源库

2、点击左侧的Repositories,然后再点击右侧的Add,会弹出下拉菜单,选择Proxy Repository

这里写图片描述

3、接下来,参考下图填写: 
这里写图片描述

• Respository ID这里填写:jboss-public-repository-group (可以自已改名字) 
• Respository Name这里填写:JBoss Public Maven Repository Group (可以自已改名字) 
• 其它项保持默认 
Remote Storage Location这里填写 
:https://repository.jboss.org/nexus/content/groups/public-jboss/ (这是jboss公布的公开资 
源库)

这里写图片描述

3.2 如何将本地jar添加到Nexus中

手动上传第三方jar包到nexus的步奏: 
1)按下图方式进行 
这里写图片描述

2)按下图完成上传 
这里写图片描述

3)点击保存完成 
这里写图片描述

3.3 下载jboss eap 7.x的离线资源包

1、首次使用nexus时,私服服务器上实质上没有任何jar包的缓存,所以第一次使用时,其实跟直接到互联网上下载没啥区别,只有后面再请求相同的jar包,服务器上才会有jar文件缓存,为了加快速度,我们可以直接上jboss官网下载离线资源包。 
官网下载地址(需要进行翻墙):https://www.jboss.org/products/eap/get-started/(null)

这里写图片描述

下载:Maven Repository (304mb) 
或者直接在linux使用进行下载:(版本为7.0)

wgethttps://access.cdn.redhat.com/content/origin/files/sha256/c4/c4748ff06357795a93f4e4a4356807ffbce571afc803a4e5b798c25d2caf5343/jboss-eap-7.0.0.Alpha-maven-repository.zip?_auth_=1450331107_f186398691e2aae898896dacc16a001c
  • 1
  • 2
  • 3
  • 4

2、然后解压到指定目录,建议放在 %nexus_home%\sonatype-work\nexus\storage\ 下: 
例如我的完整路径为:/home/xuliugen/software/nexus-2.11.2-06-bundle/sonatype- 
work/nexus/storage

这里写图片描述

3、然后add一个hosted repository,参考下图: 
这里写图片描述

上图中的:Override Local Storage Location一ing根据实际填写2步中解压的文件,如果是Linux系统,请输入完整的路径,如:

/home/xuliugen/software/nexus-2.11.2-06-bundle/sonatype-work/nexus/storage/boss-eap-7.0.0.Alpha-maven-repository
  • 1
  • 2

4、查看安装结果 
这里写图片描述

3.4 如何将repository设置为Public

这里写图片描述

如果没包括在组里,参考上图处理一下,然后保存。最后建议更新一下索引,参考下图:

这里写图片描述

3.5 使用OSChina作为Maven仓库代理

1、点击添加Proxy Repository 
这里写图片描述

2、设置配置 
其中远程地址如下:http://maven.oschina.net/content/groups/public/ 
这里写图片描述

其中ID和Name任意设置,Remote Location设置为 
http://maven.oschina.net/content/groups/public/ 
点击save即可。 
3、在刚才添加的代理Repository上边点击右键,选择Update Index即可。

这里写图片描述

3.6 解决nexus下载maven索引的问题

1、能正常访问browse remote,但是却没有看到任何jar文件 
这里写图片描述

2、这是因为在:/home/xuliugen/software/nexus-2.11.2-06-bundle/sonatype- 
work/nexus/indexer/central-ctx目录下缺少nexus-maven-repository-index文件 
这里写图片描述

所以需要下载该文件, 
下载地址:https://gitee.com/chendeyuan/nexus-maven-repository-index.git 
解压之后放在该目录下

原创粉丝点击