搭建私服可以做什么? 
1、如果公司开发组的开发环境全部内网,这时如何连接到在互联网上的Maven中央仓库呢? 
2、如果公司经常开发一些公共的组件,如何共享给各个开发组,使用拷贝方式吗?如果这样,公共库升级了怎么办?

当然可以解决的问题可能不止上面两点,下面来介绍在Linux中搭建自己的Maven私服,使用Nexus。

一、下载和安装

下载

网址:http://www.sonatype.org/nexus/go/ 

下载包:nexus-2.12.0-01-bundle.tar.gz 
解压包:tar -zxvf nexus-2.12.0-01-bundle.tar.gz 
默认端口为8081,如需修改请查看配置文件 conf/nexus.properties 
它本身不建议在root用户下使用,如果我们需要在root用户下启动服务,要先配置 bin/nexus 文件中的 RUN_AS_USER=root

我下载的是zip包,解压后进入\nexus-2.1.2-bundle\nexus-2.1.2\bin\jsw\,根据操作系统类型选择文件夹,我选的是windows-x86-32文件夹,进入后可看到如下所示bat文件。

安装

将下载Bundle文件解压,会得到如下两个子目录:

nexus-webapp-1.7.2/:该目录包含了Nexus运行所需要的文件,如启动脚本,依赖jar包等。

sonatype-work/:该目录包含Nexus生成的配置文件、日志文件、仓库文件等。

其中第一个目录是运行Nexus所必需的。

第二个目录不是必须的,Nexus会在运行的时候动态创建改目录,不过它的内容对于各个Nexus实例是不一样的,因为不同用户在不同机器上使用的Nexus会有不同的配置和仓库内容。

当用户需要备份Nexus的时候,默认备份sonatype-work/目录,因为该目录包含了用户特定的内容,而nexus-webapp-1.7.2目录下的内容是可以从安装包直接获得的。

ps:最简单的方式就是将两个文件全部拷贝,然后直接安装使用。

二、私服的启动和配置

启动

[root@localhost nexus-maven]# cd nexus-2.12.0-01/bin/
[root@localhost bin]# ./nexus start
****************************************
WARNING - NOT RECOMMENDED TO RUN AS ROOT
****************************************
Starting Nexus OSS...
Started Nexus OSS.
[root@localhost bin]# ./nexus status
****************************************
WARNING - NOT RECOMMENDED TO RUN AS ROOT
****************************************
Nexus OSS is running (34504).
[root@localhost bin]#

启动后访问首页: http://192.168.19.130:8081/nexus/index.html

登录默认账号/密码 admin/admin123 

打开 Repositories 将列表中所有Type为proxy 的项目的 Configuration 中的 Download Remote Indexes 设置为True 

将Releases仓库的Deployment Policy设置为*Allow ReDeploy 

设置 deployment 账户密码 
 

然后在Central 仓库上右键然后点击 Repair Index 下载中心仓库的索引文件,若干时间后,可以点击下边的 Browse Index 即可看见下载的索引文件。


当然我们也避免不了会使用到一些第三方的 jar ,而这些jar包也不存在于互联网上的maven中央仓库中,这时我们可以手工添加jar 到我们的私服中。 
添加第三方 jar 如下: 

如果需要删除,如下: 

三、本地项目配置引用私服

在项目的 pom.xml 中配置私库地址,pom.xml 的下面添加:

 <!-- 私有仓库 -->
<repositories>
<repository>
<id>public</id> <!--这个ID需要与你的组group ID一致-->
<name>Public Repository</name>
<url>http://192.168.19.130:8081/nexus/content/groups/public</url>
</repository>
</repositories> <!-- 打包发布 -->
<distributionManagement>
<repository>
<id>releases</id><!--这个ID需要与你的release仓库的Repository ID一致-->
<url>http://192.168.19.130:8081/nexus/content/repositories/releases</url>
</repository> <snapshotRepository>
<id>snapshots</id><!--这个ID需要与你的snapshots仓库的Repository ID一致-->
<url>http://192.168.19.130:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>

ps:仓库也可以通过节点的方式,配置在setting.xml文件中。

在settings.xml 中配置 server 账户信息:

<servers>
<server>
<id>releases</id>
<username>deployment</username>
<password>lixuwu123</password><!--这个密码就是你设置的密码-->
</server>
<server>
<id>snapshots</id>
<username>deployment</username>
<password>lixuwu123</password><!--这个密码就是你设置的密码-->
</server>
</servers>

需要说明一点: 
当pom.xml中同时配置了releases仓库和snapshots仓库时。 
pom.xml文件开头的版本配置1.0.0-SNAPSHOT为build到snapshots库, 
pom.xml文件开头的版本配置1.0.0 (不带-SNAPSHOT) 的会build到releases库, 
如果只配置了releases库而版本号写的是带-SNAPSHOT的,build到最后一步会报400错误,因为它找不到对应的库。

四、测试

1、新建一个简单的maven项目,随便写个类。 
在pom.xml 文件按上面 三、本地项目配置引用私服 方法添加 私有仓库和打包发布配置 
然后使用命令 mvn deploy 发布成功后,此时我们在我们的私服中就可以看到发布后的结果,如下: 

2、再新建一个项目,或者使用已有的maven项目(最好使用别人的环境不同的电脑)。 
在pom.xml 中和第1步一样先配置私库地址,然后添加第1步发布后的 dependency 后,就可以看到jar 被正常加载到工程中了。 

自己测试实例:

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>D:/.m2/repository</localRepository> <pluginGroups></pluginGroups> <proxies></proxies> <!-- servers
| This is a list of authentication profiles, keyed by the server-id used within the system.
| Authentication profiles can be used whenever maven must make a connection to a remote server.
|-->
<servers> <server>
<!-- 这个ID要和项目pom.xml中distributionManagement下的ID一致 -->
<id>nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<!-- 这个ID要和项目pom.xml中distributionManagement下的ID一致 -->
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers> <mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</mirror>
</mirrors> <profiles>
<profile>
<id>nexus</id>
<!-- 配置仓库 -->
<repositories>
<repository>
<id>central</id>
<url>http://central</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> <!-- 激活节点nexus -->
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>

pom.xml文件:

使用eclipse新建一个quickstart的maven项目

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.li</groupId>
<artifactId>test-archetype</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>test-archetype Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies> <distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build> </project>

执行命令

clean deploy

(转)搭建Maven私服(使用Nexus)的更多相关文章

  1. Maven学习 (四) 使用Nexus搭建Maven私服

    为什么要搭建nexus私服,原因很简单,有些公司都不提供外网给项目组人员,因此就不能使用maven访问远程的仓库地址,所以很有必要在局域网里找一台有外网权限的机器,搭建nexus私服,然后开发人员连到 ...

  2. Maven-004-使用 Nexus 搭建 maven 私服

    从去年至今,自己一直在学习自动化测试工具,想利用自动化工具尽可能的将重复的.关键的.耗时耗力的工作实现自动化,减轻日常测试工作,提升测试效率.在学习的过程中,将 maven 作为了项目开发管理工具,进 ...

  3. Ubuntu server下搭建Maven私服Nexus

    Ubuntu server下搭建Maven私服Nexus Maven私服Nexus的作用,主要是为了节省资源,在内部作为maven开发资源共享服务器来使用. 1.下载 通过root用户进去Ubuntu ...

  4. Maven使用笔记(五)Sonatype Nexus 搭建Maven 私服

    1. 为什么使用Nexus 如果没有私服,我们所需的所有构件都需要通过maven的中央仓库和第三方的Maven仓库下载到本地, 而一个团队中的所有人都重复的从maven仓库下载构件无疑加大了仓库的负载 ...

  5. Maven——使用Nexus搭建Maven私服

    原文:http://www.cnblogs.com/xdp-gacl/p/4068967.html Maven学习总结(九)--使用Nexus搭建Maven私服 一.搭建nexus私服的目的 为什么要 ...

  6. Maven学习 使用Nexus搭建Maven私服(转)

    为什么要搭建nexus私服,原因很简单,有些公司都不提供外网给项目组人员,因此就不能使用maven访问远程的仓库地址,所以很有必要在局域网里找一台有外网权限的机器,搭建nexus私服,然后开发人员连到 ...

  7. (转)Maven学习总结(九)——使用Nexus搭建Maven私服

    孤傲苍狼只为成功找方法,不为失败找借口! Maven学习总结(九)——使用Nexus搭建Maven私服 一.搭建nexus私服的目的 为什么要搭建nexus私服,原因很简单,有些公司都不提供外网给项目 ...

  8. Linux下使用Nexus搭建Maven私服

    在开发过程中,有时候会使用到公司内部的一些开发包,显然把这些包放在外部是不合适的.另外,由于项目一直在开发中,这些内部的依赖可能也在不断的更新.可以通过搭建公司内部的Maven服务器,将第三方和内部的 ...

  9. Maven学习二:使用Nexus搭建Maven私服及相关配置

    处于安全等原因的考虑,一些企业内部网络是不允许访问外部网络的,但是项目内部搭建的项目又是Maven架构,这样就需要企业在内部网络中搭建自己的Maven仓库服务,再者一些大型企业或者内部模块化组件化划分 ...

  10. Maven学习-使用Nexus搭建Maven私服

    为什么要搭建nexus私服,原因很简单,有些公司都不提供外网给项目组人员,因此就不能使用maven访问远程的仓库地址,所以很有必要在局域网里找一台有外网权限的机器,搭建nexus私服,然后开发人员连到 ...

随机推荐

  1. Docker阿里云镜像加速

    登录阿里云docker registry sudo docker login --username=zhangsan@163.com registry.cn-hangzhou.aliyuncs.com ...

  2. linux系统最小化安装后的初始化脚本

    作为运维人员,经常会初始化系统,系统在安装过程中基本都会选择最小化安装,这样安装好的系统里会缺少很多环境. 下面分享一个系统安装后的初始化脚本: #!/bin/bash #系统时最小化安装的,这里要安 ...

  3. 基于SSH的高校网上选课系统的质量属性的实现

    我对于基于SSH的高校网上选课系统的质量属性的实现是从可用性.性能.安全性.可维护性.易用性五个方面进行的实现. 可用性方面: 实现方式:(1)当系统试图超出限制范围来进行课程查询或选课时必须进行错误 ...

  4. OSG 改变窗口大小

    viewer.realize();//需要realize,否则窗口为null osgViewer::GraphicsWindow *pWnd = dynamic_cast<osgViewer:: ...

  5. Sprint 冲刺第三阶段第一天

    1.今晚我在整理之前的代码,检查细节,然后发现游戏要返回上一界面竟然出现了问题“项目停止运行”,仔细检查没办法解决,后来百度可能是因为修改了之前文件的名字,可在AndroidManifest.xml中 ...

  6. Golang 函数

    创建函数 package main import "fmt" //有参数,有返回值 func demo(a int, s string) (int, string) { retur ...

  7. Glace:generator-jhipster, adding User entity enhancement management

    https://github.com/jhipster/generator-jhipster/issues/2538 jhipster,很好用的开发工具.国外知名度高,国内未普及,国外大公司在用. j ...

  8. Selenium Grid的Java调用方法

    java -jar selenium-server-standalone-.jar -role hub explorer http://192.168.1.173:4444/grid/console ...

  9. js面向对象高级编程

    面向对象的组成 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  10. Oracle重新获取统计信息以及SQLSERVER重建索引

    Oracle重新获取统计信息 exec dbms_stats.gather_schema_stats(ownname =>'LCoe739999',options => 'GATHER', ...