配置Nexus为maven的私服

摘要:
在实际的项目开发中,我们都认为本地机器上的所有Maven项目都可以在一个配置中使用Maven Private Server。然后我们需要使用以下方法。在上述配置中,使用ID为nexus的配置文件。该概要文件包含相关的仓库配置,activeProfiles元素用于激活nexus概要文件。这样,当Maven构建时,激活的概要文件将将仓库配置应用于项目。您可以创建与任何仓库匹配的图像。映像地址是专用服务器。这样,Maven从任何仓库下载组件的请求都将被传输到专用服务器。

1、配置Nexus为maven的私服

第一种方式:在项目的POM中如下配置

<repositories>
    <repository>
        <id>nexus_public</id>        
        <url>http://localhost:8081/nexus/content/groups/public/</url>        
        <releases>            
            <enabled>true</enabled>        
        <releases>        
        <snapshots>
          <enabled>true</enabled>        
        <snapshots>   
      </repository>
  </repositories>
  <pluginRepositories>
      <pluginRepository>
               <id>nexus_public</id>
               <url>http://localhost:8081/nexus/content/groups/public/</url> 
                <releases>
                       <enabled>true</enabled>
                 <releases>
                 <snapshots>
                       <enabled>true</enabled> 
                 </snapshots>
        </pluginRepository>
   </pluginRepositories>
 

注意:这样的配置(第一个仓库是代码库,第二个是插件库)只对当前的Maven项目有效。实际的项目开发中,我们都想着一次配置就能让本机的所有Maven项目都使用Maven私服,那么我们就要用到下面的方法。

 

第二种方式:在settings.xml中如下配置:

<settings>
...

    <profiles>
        <profile>
            <id>nexus</id>
            <repositories>
                <repository>
                    <id>nexus_public</id>
                    <url>http://localhost:8081/nexus/content/groups/public/</url>
                    <releases>
                        <enabled>trueenabled>
                    </releases>
                    <snapshots>
                    <enabled>true</enabled>
                </snapshots>    
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>nexus_public</id>
                <url>http://localhost:8081/nexus/content/groups/public/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
             </pluginRepository>
        </pluginRepositories>
    </profile>
</profiles>

<activeProfiles>
    <activeProfile>nexus</activeProfile>
</activeProfiles>

...
settings>
 

Maven提供的profile是一组可选的配置,可以用来设置或者覆盖配置默认值。有了profile,你就可以为不同的环境定制构建。

上面的配置中,使用了一个idnexusprofile,这个profile包含了相关的仓库配置,同时配置中又使用了activeProfiles元素将nexus这个profile激活,这样当执行Maven构建的时候,激活的profile会将仓库配置应用到项目中去。

通过上面的配置,我们会发现Maven不仅会Nexus下载构件外还会从中央仓库下载构件构件,为什么呢?因为超级POM文件定义了一个为central的远程仓库,如果这个ID没有被覆盖,那么请求下载构件时还可能会从central标识的远程仓库下载。既然是私服,那么我们就只希望Maven下载请求都仅仅通过Nexus。我们可以通过镜像实现这一需求。可以创建一个匹配任何仓库的镜像,镜像的地址是私服,这样Maven对任何仓库的构件下载请求都会转到私服中。

 

central的意思其实是重写了超级POM的资源库,那么这里通过重写导致覆盖,重要的是,镜像效果导致直接走镜像,而不会去理会这些资源库配置了,所以基本上url也失去了意义。不过对于快照和发布版本是否进行更新下载还是要靠这个配置来决定的,只有这个配置决定继续更新时才会走镜像下载资源,镜像其实是像在镜像内部寻找资源,如果没有则会在镜像配置的远程仓库下载资源到镜像中。

 

把上面的配置修改为如下配置:

<settings>
...
<mirrors>
    <mirror>
        <id>local_mirror</id>
        <mirrorOf>*</mirrorOf>
        <name>local_mirror</name>
        <url>http://localhost:8081/nexus/content/groups/public/</url>
    </mirror>
</mirrors>

<profiles>
    <profile>
         <id>nexus</id>
        <repositories>
        <repository>
            <id>central</id>
            <url>http://repo.maven.apache.org/maven2</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>

        <pluginRepositories>
            <pluginRepository>
                <id>central</id>
                <url>http://repo.maven.apache.org/maven2</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
             </pluginRepository>
        </pluginRepositories>

    </profile>
</profiles>

<activeProfiles>
    <activeProfile>nexus</activeProfile>
</activeProfiles>
...
</settings>
 

注意:以上两种方法都是配置下载的地址,也就是说项目需要下载插件时、下载代码时请求的地址。如果想要上传自己的代码,那么还需要如下配置,也就是部署构件到私服。

2、部署构件到私服

我们在实际开发过程是多个人的,那么总有一些公共模块或者说第三方构件是无法从Maven中央库下载的。我们需要将这些构件部署到私服上,供其他开发人员下载。用户可以配置Maven自动部署构件至Nexus的宿主仓库,也可以通过界面手动上传构件。

第一种方式:使用Maven部署构件到Nexus私服上

日常开发的快照版本部署到Nexus中策略为Snapshot的宿主仓库中,正式项目部署到策略为Release的宿主仓库中,POM的配置方式如下(这个配置文件同样可以写在settings.xml文件中):

<distributionManagement>
    <repository>
        <id>local_nexus_releases</id>
        <name>core Release Repository</name>
        <url>http://localhost:8081/nexus/content/repositories/releases/</url>
    </repository>
    <snapshotRepository>
        <id>local_nexus_snapshots</id>
        <name>core Snapshots Repository</name>
        <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
    </snapshotRepository>
</distributionManagement>
 

Nexus的仓库对于匿名用户只是只读的。为了能够部署构件,我们还需要再settings.xml中配置验证信息(其中,验证信息中service的id应该与POM中repository的id一致):

<servers>
    <server>
        <id>local_nexus_releases</id>
        <username>admin</username>
        <password>admin123</password>
    </server>
    <server>
        <id>local_nexus_snapshots</id>
        <username>admin</username>
        <password>admin123</password>
</server>
</servers>
 

第二种方式:在Nexus界面上手动部署第三方构件至私服

我们除了自己的构件要部署到Nexus私服上外,我们有可能还要将第三方构件(如:SQLServiceJDBC)部署到Nexus上。这个时候,在Nexus界面上选择一个宿主仓库(如3rd party),再在页面下方选择Artifact Upload选项卡。填写对应的Maven坐标。然后点击“Select Artifact(s) for Upload”按钮从本机选择要上传的构件,然后点击“Add Artifact”按钮将其加入到上传列表中。最后,单击页面底部的“Upload Artifact(s)”按钮将构件上传到仓库中。

配置Nexus为maven的私服第1张

上传成功之后,就可以查看结果了:

配置Nexus为maven的私服第2张

 

测试附件信息

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>oracle.ojdbc</groupId>
  <artifactId>jdbc</artifactId>
  <version>14-10.2.0</version>
  <description>POM was created from install:install-file</description>
</project>

免责声明:文章转载自《配置Nexus为maven的私服》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇图片放大镜插件——jqzoommybatis 中 foreach collection的三种用法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

使用Nexus创建私服

原文链接:http://www.cnblogs.com/helong/articles/2254446.html 注意:nexus2.6.0版本以后不支持jdk6了,必须jdk1.7或以上。 参考:http://blog.csdn.net/shandian534/article/details/8987349 -----------------------...

maven 配置多模块项目 pom modules

所有用Maven管理的真实的项目都应该是分模块的,每个模块都对应着一个pom.xml。它们之间通过继承和聚合(也称作多模块,multi-module)相互关联。那么,为什么要这么做呢?我们明明在开发一个项目,划分模块后,导入Eclipse变成了N个项目,这会带来复杂度,给开发带来不便。 为了解释原因,假设有这样一个项目,很常见的JavaWeb应用。在这个应...

Android 本地化适配:RTL(right-to-left) 适配清单

本文首发自公众号:承香墨影(ID:cxmyDev),欢迎关注。 一. 序 越来越多的公司 App,都开始淘金海外,寻找更多的机会。然而海外市场千差万别,无论是市场还是用户的使用习惯,都有诸多的不同。 当你接触一款出海 App 的时候,除了需要了解海外 Google Service 的整个生态圈,还要做好不同语言的适配。语言适配最通用的做法就是根据不同系统...

Nexus 私服无法下载远程 Jar 包问题排查指南

一、前言 前几天线上 ZooKeeper 出了一次不大不小的问题,由于缺少监控报警,导致事发第二天才引起我们的注意,所幸没有造成太大的影响。这起事情也督促我们尽快建立完备的监控体系,目前项目中使用 JavaMelody 监控应用状态,但是缺少实时报警。所以最近团队内部都在调研相关监控工具,初步选型 Sentry。 不过本篇跟 Sentry 其实没啥关系,主...

Docker学习笔记二:Docker设置镜像源

 Docker设置镜像源 1、镜像源介绍 当运行容器时,使用的镜像如果在本地中不存在,就会自动从docker镜像仓库中下载,默认从Docker Hub(https://hub.docker.com/ )公共镜像源下载; 为加快拉取镜像速度,建议设置docker国内镜像源; 2、修改docker下载的镜像源 ①. 修改文件(没有则新增) touch ...

Spring boot 项目打成war包并在idea中运行

1、 修改pom文件原来是jar改成<packaging>war</packaging> 2、 在pom文件中添加移除内置tomcat并且添加添加servlet-api的依赖。 <!--war包部署需要--> <dependency> <groupId>org.springframew...