前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >为你的项目配置Maven私服Nexus

为你的项目配置Maven私服Nexus

作者头像
Java后端技术
发布2018-08-09 10:39:03
1.3K0
发布2018-08-09 10:39:03
举报
文章被收录于专栏:Java后端技术栈Java后端技术栈

一、Nexus简介

1、Nexus概述

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

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

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

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

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

2、Maven仓库

2.1、仓库结构图

2.2、本地仓库

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

2.3、远程仓库

远程仓库概述

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

中央仓库

中央仓库是默认的远程仓库,如果不做任何特殊配置那么将会从中央仓库下载依赖,这在 $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>

镜像

镜像及将的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>

这里即是将仓库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>

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

私服

私服一般采用nexus部署,这里添加其他远程仓库的步骤如下:

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>

• 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>

3、发现下载成功即可

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

<server> <id>deploymentRepo</id> <username>repouser</username> <password>repopwd</password>

</server>

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

二、Nexus安装

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-01

sonatype-work

注:安装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=8086

application-host=0.0.0.0

nexus-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/nexus nexus-work=${bundleBasedir}/../sonatype-work/nexus runtime=${bundleBasedir}/nexus/WEB-INF

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

注意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.

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

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

5.设置nexus开机启动:

cp nexus /etc/init.d/ chkconfig --add nexuschk

config nexus on

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>

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>

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>

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

4、验证安装结果

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

mvn clean mvn deploy

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

mvn clean mvn install -D maven.test.skip=true

可以看到Terminal中输出一下信息:

表示正将编译之后的jar上传到maven仓库中

2、查看远程Nexus

版权声明

Java后端技术所推送文章,为本人原创、网上收集或其他作者投稿,对于网上收集部分除非确实无法确认,我们都会注明作者和来源。部分文章推送时未能与原作者取得联系。若涉及版权问题,烦请原作者联系我们,我们会在24小时内删除处理,谢谢!^_^ QQ:1573876303。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-10-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Java后端技术 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 中央仓库
  • 私服
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档