Ubuntu server下搭建Maven私服Nexus

Ubuntu server下搭建Maven私服Nexus

1、下载

  通过root用户进去Ubuntu server

  $  cd /opt

  $  wget http://download.sonatype.com/nexus/oss/nexus-2.10.0-02-bundle.tar.gz

  2、启动

  环境准备,启动nexus,必须先完成JDK环境的配置。

  $  cd /opt/

  $  tar -zxvf nexus-2.10.0-02-bundle.tar.gz

  $  cd /opt/nexus-2.10.0-02/bin

  $  vi nexus

  在启动文件nexus中增加nexus的启动用户,否则没权限启动,将报错如下:

  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

  所以要解决此问题,需要在启动文件nexus,加上RUN_AS_USER=root,如图所示

  

  保存退出之后,即可启动nexus,命令如下:

  $  ./nexus start  

  Starting Nexus OSS...
  Started Nexus OSS.

  表示启动成功,访问地址:http://ip:8081/nexus

  3、配置nexus

  登录nexus,右上角Log In,默认登录用户名密码:admin/admin123

  登录之后,我们可以点击左边菜单栏Repositories进入详细配置

  可以自己重新创建一个私服配置,这里我们用系统在带的Public Repositories

  

  关于仓库的类型介绍  

  hosted 类型的仓库,内部项目的发布仓库

  releases 内部的模块中release模块的发布仓库

  snapshots 发布内部的SNAPSHOT模块的仓库

  3rd party 第三方依赖的仓库,这个数据通常是由内部人员自行下载之后发布上去

  proxy 类型的仓库,从远程中央仓库中寻找数据的仓库

  group 类型的仓库,组仓库用来方便我们开发人员进行设置的仓库


  但是需要将其他几个代理的库配置映射到Public中,分别将Apache Snapshots、Central、Codehaus Sanpshots下Download Remote Indexes选项选择【true】,保存即可,默认是false,如下图

  

  把将类型为Proxy的库Apache Snapshots、Central、Codehaus Sanpshots配置到Public Repositories下,如下图:

  

  然后分别将Apache Snapshots、Central、Codehaus Sanpshots更新Index,在每一个库上面右键操作Repair Index

  

  最后将Public Repositories操作Repair Index

  然后接可以测试maven仓库了。

  4、远程测试maven仓库

     通常我们是修改本地的maven安装目录下的配置文件setting.xml,将本地仓库更换成私服仓库地址,保证本客户端上所有的项目都将使用私服,配置setting.xml如下:在<profiles></profiles>加入

<profile>
    <id>env-dev</id>
    <repositories>
      <repository>
        <id>nexus</id>
        <name>nexus Repositories</name>
        <url>http://192.168.10.50:8081/nexus/content/groups/public</url>
        <snapshots>
          <enabled>true</enabled>
        </snapshots>
        <releases>
          <enabled>true</enabled>
        </releases>
      </repository>
    </repositories>
	<!--插件本地仓库-->
	<pluginRepositories> 
	  <pluginRepository> 
	    <id>nexus</id> 
	    <url>http://192.168.10.50:8081/nexus/content/groups/public</url> 
	    <releases>
		<enabled>true</enabled>
	    </releases>
	    <snapshots>
		<enabled>true</enabled>
	    </snapshots>
	  </pluginRepository>
	</pluginRepositories>
</profile>

并在</profiles>之后中加入激活

<activeProfiles>
    <activeProfile>env-dev</activeProfile>        
</activeProfiles>

 如果 构建的项目需发布项目到私有库,在setting.xml配置发布的账户

<servers>
    <server>
        <id>nexus-releases</id>
        <username>deployment</username>
        <password>deployment123</password>
     </server>
      <server>
          <id>nexus-snapshots</id>
          <username>deployment</username>
           <password>deployment123</password>
      </server>
</servers>

在项目中,则需要项目的pom.xml文件中添加如下,项目中id需和server中id一致:

<distributionManagement>
  <repository>
	<id>nexus-releases</id>
	<name>Nexus Release Repository </name>
	<url> http://192.168.10.50:8081/nexus/content/repositories/releases/ </url>
  </repository>
  <snapshotRepository>
	<id>nexus-snapshots</id>
	<name>Nexus Snapshot Repository</name>
	<url> http://192.168.10.50:8081/nexus/content/repositories/snapshots/ </url>
  </snapshotRepository>
</distributionManagement> 

  然后执行maven  clean deploy,检测是否发布成功~在私服上,通过左边的Artifact Search 搜索窗口,查看jar包是否在私服下。

  5、三方jar包入maven私服

  比如 有些第三方jar包,在maven中心仓库没有,这个时候就需要手动在本地将jar上传上去,以供本地其他用户使用,简单截图介绍

  

6.安装nexus为service   
以root用户身份,设置为系统服务 
cd /etc/init.d 
cp /usr/local/nexus/bin /nexus ./nexus 
chmod 755 /etc/init.d/nexus
在Red Hat, Fedora, and CentOS中增加nexus服务
$ cd /etc/init.d
增加nexus服务
$ chkconfig --add nexus
添加运行级别3、4、5
$ chkconfig --levels 345 nexus on
启动nexus
$ service nexus start
验证关系成功启动
$ tail -f /usr/local/nexus/logs/wrapper.log
编辑/etc/init.d/nexus的文本
NEXUS_HOME="/usr/local/nexus"
PIDDIR="${NEXUS_HOME}"  
RUN_AS_USER = "nexus" 

7.镜像配置

如果仓库X可以提供仓库Y存储的所有内容,那么就可以认为X是Y的一个镜像。换句话说,任何一个可以从仓库Y获得的构件,都胡够从它的镜像中获取。举个例子,http://maven.net.cn/content/groups/public/ 是中央仓库http://repo1.maven.org/maven2/ 在中国的镜像,由于地理位置的因素,该镜像往往能够提供比中央仓库更快的务。因此,可以配置Maven使用该镜像来替代中央仓库。编辑settings.xml,代码如下:

<settings>
  ...
  <mirrors>
    <mirror>
      <id>maven.net.cn</id>
      <name>one of the central mirrors in china</name>
      <url>http://maven.net.cn/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
  </mirrors>
  ...
</settings>
该例中,<mirrorOf>的值为central,表示该配置为中央仓库的镜像,任何对于中央仓库的请求都会转至该镜像,用户也可以使用同样的方法配置其他仓库的镜像。另外三个元素id,name,url与一般仓库配置无异,表示该镜像仓库的唯一标识符、名称以及地址。类似地,如果该镜像需认证,也可以基于该id配置仓库认证。
关于镜像的一个更为常见的用法是结合私服。由于私服可以代理任何外部的公共仓库(包括中央仓库),因此,对于组织内部的Maven用户来说,使用一个私服地址就等于使用了所有需要的外部仓库,这可以将配置集中到私服,从而简化Maven本身的配置。在这种情况下,任何需要的构件都可以从私服获得,私服就是所有仓库的镜像。这时,可以配置这样的一个镜像,如例:
 <settings>
  ...
  <mirrors>
    <mirror>
      <id>central</id>
      <name>Internal Repository Manager</name>
      <url>http://192.168.10.50:8081/nexus/content/groups/public</url>
      <mirrorOf>*</mirrorOf>
    </mirror>
  </mirrors>
  ...
</settings>
该例中<mirrorOf>的值为星号,表示该配置是所有Maven仓库的镜像,任何对于远程仓库的请求都会被转至http://192.168.10.50:8081/nexus/content/groups/public。如果该镜像仓库需要认证,则配置一个Id为internal-repository的<server>即可。为了满足一些复杂的需求,Maven还支持更高级的镜像配置:
1).<mirrorOf>*</mirrorOf>
匹配所有远程仓库。
2).<mirrorOf>external:*</mirrorOf>
匹配所有远程仓库,使用localhost的除外,使用file://协议的除外。也就是说,匹配所有不在本机上的远程仓库。
3).<mirrorOf>repo1,repo2</mirrorOf>
匹配仓库repo1和repo2,使用逗号分隔多个远程仓库。
4).<mirrorOf>*,!repo1</miiroOf>
匹配所有远程仓库,repo1除外,使用感叹号将仓库从匹配中排除。

需要注意的是,由于镜像仓库完全屏蔽了被镜像仓库,当镜像仓库不稳定或者停止服务的时候,Maven仍将无法访问被镜像仓库,因而将无法下载构件。

阿里云maven 仓库

<mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>*</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

转载:http://www.cnblogs.com/candle806/p/4076325.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
搭建Maven私服可以使用Sonatype Nexus,它是一个功能强大的仓库管理器,可以帮助我们创建和管理Maven私服。下面是基于Nexus搭建Maven私服的详细步骤: 1. 准备环境:首先,确保你已经安装了Java环境,并且可以正常运行MavenNexus软件。 2. 下载和解压缩Nexus软件:从Sonatype官方网站下载最新版本的Nexus软件,并解压缩到合适的目录中。 3. 启动Nexus:使用命令行窗口进入Nexus软件的目录,然后执行 `./bin/nexus start` 命令来启动Nexus服务器。 4. 访问Nexus控制台:在浏览器中输入 `http://localhost:8081` 访问Nexus控制台,默认用户名和密码都是 `admin`,登录成功后,可以看到Nexus的管理界面。 5. 创建Maven仓库:在Nexus控制台中,点击左侧菜单的 `Repositories`,然后点击 `Create repository` 来创建新的Maven仓库。根据需要选择 `Hosted repository` 或者 `Proxy repository`,然后填写相应的配置信息,如仓库名称、URL、布局等。 6. 配置Maven项目:在你的Maven项目的pom.xml文件中添加Nexus私服的配置信息,包括ID、URL等,用来指定将Maven构建输出部署到Nexus仓库中。 7. 部署和使用:使用 `mvn deploy` 命令将项目打包并部署到Nexus私服中,当其他开发人员或者CI/CD服务器需要下载依赖时,只需要修改项目的settings.xml文件,指定Nexus私服的URL和凭据即可。 8. 其他设置:你还可以在Nexus控制台里进行其他设置,比如用户管理、访问控制、仓库代理等。 总结一下,使用Nexus搭建Maven私服非常简单,只需要几个基本的步骤即可完成。通过搭建Maven私服,我们可以有效地管理和共享项目的依赖包,提升团队的开发效率和项目的可维护性。这对于生产环境中的项目非常重要。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值