您的位置:首页 > 运维架构 > Linux

Linux下使用nexus搭建maven私服

2017-03-03 10:48 786 查看
Maven 仓库的分类:(maven的仓库只有两大类)

1.本地仓库

2.远程仓库,在远程仓库中又分成了3种:

2.1 中央仓库

2.2 私服

2.3 其它公共库

有个maven私服可以很方便地管理我们的jar包和发布构建到远程仓库,本文就介绍了如何在Linux下一步步使用nexus搭建maven私服。

私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件。有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库;否则,私服请求外部的远程仓库,将构件下载到私服,再提供给本地仓库下载。

首先安装好jdk 、maven

我这里安装的JDK 、maven版本信息如下:

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

[root@localhost bin]# java -version

java version “1.7.0_79”

Java(TM) SE Runtime Environment (build 1.7.0_79-b15)

Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

[root@localhost bin]# mvn -v

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T08:41:47-08:00)

Maven home: /home/data/maven

Java version: 1.7.0_79, vendor: Oracle Corporation

Java home: /home/xuan/soft/jdk1.7.0_79/jre

Default locale: en_US, platform encoding: UTF-8

OS name: “linux”, version: “3.10.0-327.el7.x86_64”, arch: “amd64”, family: “unix”

这里附上,环境配置:

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

java

export JAVA_HOME=/home/data/java

export PATH=JAVAHOME/bin:PATH

export CLASSPATH=.:JAVAHOME/lib/dt.jar:JAVA_HOME/lib/tools.jar

maven

export MAVEN_HOME=/home/data/maven

export MAVEN_HOME

export PATH=PATH:{MAVEN_HOME}/bin

nexus

export RUN_AS_USER=root

下载安装

nexus下载地址:http://www.sonatype.org/nexus/go

解压后会在同级目录中,出现两个文件夹:nexus-oss-webapp-1.8.0和sonatype-work,前者包含了nexus的运行环境和应用程序,后者包含了你自己的配置和数据。

nexus-2.12.0-01-bundle.tar.gz

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

tar -zxvf nexus-2.12.1-01-bundle.tar.gz -C /home/xuan/soft/nexus

[root@localhost soft]# tar -zxvf nexus-2.12.1-01-bundle.tar.gz

nexus-2.12.1-01/

nexus-2.12.1-01/nexus/

为了方便更新和切换版本,创建链接,更新或者切换版本只需更新链接即可:

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

tar -zxvf nexus-2.12.1-01-bundle.tar.gz -C /home/xuan/soft/nexus

[root@localhost nexus]# pwd

/home/xuan/soft/nexus

[root@localhost nexus]# ls

nexus-2.12.1-01 sonatype-work

[root@localhost nexus]#

ln -s /home/xuan/soft/nexus /home/data/nexus

启动nexus

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

[root@localhost bin]# pwd

/home/data/nexus/nexus-2.12.1-01/bin

[root@localhost bin]# ./nexus

Usage: ./nexus { console | start | stop | restart | status | dump }

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

$ ./nexus start

Starting Nexus OSS…

Started Nexus OSS.

启动成功:

查看控制台:

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

[root@localhost bin]# ./nexus console

WARNING - NOT RECOMMENDED TO RUN AS ROOT

Running Nexus OSS…

Nexus OSS is already running.

查看nexus日志:

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

[root@localhost logs]# pwd

/home/data/nexus/nexus-2.12.1-01/logs

[root@localhost logs]# tail -f ./wrapper.log

jvm 1 | 2016-05-20 06:50:10,655-0700 INFO [qtp1294005448-58] anonymous org.apache.http.impl.execchain.RetryExec - I/O exception (java.net.SocketException) caught when processing request to {s}->https://sonatype-download.global.ssl.fastly.net:443: Connection reset

配置nexus

访问网址:http://yourhostname:8081/nexus

访问网址:http://192.168.140.128:8081/nexus

右上角以admin登陆,默认用户名/密码:admin/admin123。

安装过程中可能遇到的错误:

1、进入启动目录,启动时不成功 提示

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.

大概意思就是要在环境配置export RUN_AS_USER=root,临时配置

也可以在vi /etc/profile 文件末尾里添加export RUN_AS_USER=root 永久配置

2、访问不了虚拟机的,需要关闭防火墙

centos从7开始默认用的是firewalld,这个是基于iptables的,虽然有iptables的核心,但是iptables的服务是没安装的。

所以你只要停止firewalld服务即可:

sudo systemctl stop firewalld.service && sudo systemctl disable firewalld.service

如果你要改用iptables的话,需要安装iptables服务:

sudo yum install iptables-services

sudo systemctl enable iptables && sudo systemctl enable ip6tables

sudo systemctl start iptables && sudo systemctl start ip6tables

下一步进行Maven 与私服的测试:

~/.m2/settings.xml

在maven的setting.xml文件增加使用nexus私服配置:

maven 私服地址 http://192.168.1.102:8081/nexus/content/groups/public/

maven 中央仓库地址:http://repo1.maven.org/maven2 (没有私服镜像可以中央仓库的)

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://192.168.1.102:8081/nexus/content/groups/public/</url>
</mirror>


<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://central</url>
<releases><enabled>true</enabled><updatePolicy>always</updatePolicy></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-release</id>
<name>Spring Maven Release Repository</name>
<url>http://repo.springsource.org/libs-release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled><updatePolicy>always</updatePolicy></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>


nexus

releases

admin

admin123

snapshots

admin

admin123

测试效果图:

1、在项目的pom.xml文件 加入maven依赖测试

2、私服上的依赖

3、项目本地的依赖

参考文档:

http://tianweili.github.io/blog/2015/03/17/linux-nexus-maven-private-server/

maven 使用私服的参考文档:

http://blog.csdn.NET/liujiahan629629/article/details/39272321

本地上传依赖到nexus服务器的两种方式:

1、通过网页上传

2、通过maven的方式depoly

在项目的pom.xml 加入如下配置 (这里不做详细介绍,可以参考上面的资料)

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

releases

Nexus Release Repository

http://192.168.1.102:8081/nexus/content/repositories/releases/

snapshots

Nexus Snapshot Repository

http://192.168.1.102:8081/nexus/content/repositories/snapshots/

运行命令:mvn package deploy 它会根据项目的pom.xml文件的配置如下 在私服生成些war包 (暂不知道在那些场景应用)

com.xuan

spring

war

0.0.1-SNAPSHOT

springmvc_hibernate

在私服生成

/com/xuan/spring/xxxxx.pom

/com/xuan/springxxxxx.war

参考文档

http://my.oschina.Net/lujianing/blog/297128
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: