Nexus安装配置

一、下载最新版本的nexus    

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

2、官网如果下载不了,就找个zip下载,我下载的是:nexus-2.10.0-02-bundle.zip

3、 解压zip文件,出现两个文件夹:

   

4、 打开D:\tools\Nexus-oss\nexus-2.10.0-02\bin\jsw\,选择合适的系统,打开对应的文件夹:

  

 

二、安装nexus

1、安装

  点击install-nexus.bat,然后访问http://localhost:8081/nexus/ 启动后如下页面,在右上角有个Log in 的超链接,点击登录

默认的用户名是 admin 密码是 admin123

  

2、修改登录信息:

  

3、配置代理

配置一下maven的代理服务器(前提是你的电脑不能连接外网,如果可以上外网,这里也没有意思,只是介绍一下) 

  

添加你的代理服务器就可以了。

4、使用3rd party

使用3rd party这个第三方的功能,将maven仓库中没有构件的jar包上传到服务器

  

5、修改central repo的默认地址

   maven3使用nexus作为代理服务器时需要修改中央库的url地址,maven默认的中央库的地址是http://repo.maven.apache.org/maven2/,而nexus默认的中央代理库的地址是http://repo1.maven.org/maven2/,可能是maven3启用了新的中央库,解决办法是:

修改nexus的中央库central配置:

    把默认的中央库的url:http://repo1.maven.org/maven2/

    修改为: http://repo.maven.apache.org/maven2/

 

三、 在项目中配置Nexus Repository的信息

配置工程的pom.xml,使用nexus repository的jar包:

1、添加本地仓库地址

在Maven项目的pom.xml中添加如下的本地仓库地址:

<repositories> 

        <repository> 

            <id>nexus</id> 

            <name>nexus</name> 

            <url>http://10.10.10.41:8081/nexus/content/groups/public/</url> 

            <releases> 

                <enabled>true</enabled> 

            </releases> 

            <snapshots> 

                <enabled>true</enabled> 

            </snapshots> 

        </repository> 

    </repositories>

2、添加插件仓库地址:

在Maven项目的pom.xml中添加如下的本地仓库地址:

  <pluginRepositories> 

        <pluginRepository> 

            <id>nexus</id> 

            <name>nexus</name> 

               <url>http://10.10.10.41:8081/nexus/content/groups/public/</url> 

            <releases> 

                <enabled>true</enabled> 

            </releases> 

            <snapshots> 

                <enabled>true</enabled> 

            </snapshots> 

        </pluginRepository> 

</pluginRepositories>

3、分发构件至远程仓库

mvn install 会将项目生成的构件安装到本地Maven仓库,mvn deploy 用来将项目生成的构件分发到远程Maven仓库。本地Maven仓库的构件只能供当前用户使用,在分发到远程Maven仓库之后,所有能访问该仓库的用户都能使用你的构件。

所以我们需要配置工程中POM的distributionManagement来指定Maven分发构件的位置

<distributionManagement> 

        <repository> 

            <id>releases</id> 

            <name>Nexus Release Repository</name> 

            <url>http://10.10.10.41:8081/nexus/content/repositories/releases/</url> 

        </repository> 

 

        <snapshotRepository> 

            <id>snapshots</id> 

            <name>Nexus Snapshot Repository</name> 

            <url>http://10.10.10.41:8081/nexus/content/repositories/snapshots/</url> 

        </snapshotRepository> 

</distributionManagement>

四、用Nexus Repository取代Maven Central Repository

配置maven的settings.xml文件

4.1、文件存放位置

全局配置: ${M2_HOME}/conf/settings.xml

用户配置: ${user.home}/.m2/settings.xml

备注:用户配置优先于全局配置。${user.home} 和和所有其他系统属性只能在3.0+版本上使用。请注意windows和Linux使用变量的区别。

4.2、配置server

<servers>

<server>   

      <id>releases</id>   

      <username>admin</username>   

      <password>admin123</password>   

    </server>   

    <server>   

      <id>snapshots</id>   

      <username>admin</username>   

      <password>admin123</password>   

    </server>

    <server>   

      <id>nexus</id>   

      <username>admin</username>   

      <password>admin123</password>   

</server>

</servers>

备注:settings.xml中server元素下id的值必须与POM中repository下id的值完全一致。将认证信息放到settings下而非POM中,是因为POM往往是他人可见的,而settings.xml是本地的。

4.3、用Nexus取代Maven Central Repository

目前我们只是创建了一个专属的Nexus Repository,我们的项目默认还是使用的Maven Central Repository,所以这时我们需要将Maven Central Repository换成自己创建的Nexus Repository,可以通过修改~/.m2/settings.xml来达到这样的目的。

在settings.xml文件中(没有的话可以创建一个),加入以下配置:

<mirrors>

    <mirror>

      <id>nexus-releases</id>

      <mirrorOf>*</mirrorOf>

      <url>http://10.10.10.41:8081/nexus/content/groups/public</url>

    </mirror>

    <mirror>

      <id>nexus-snapshots</id>

      <mirrorOf>*</mirrorOf>

      <url>http://10.10.10.41:8081/nexus/content/groups/public-snapshots</url>

    </mirror>

</mirrors>

 

<profiles>

   <profile>

      <id>nexus</id>

      <repositories>

        <repository>

          <id>releases</id>

          <url>http://10.10.10.41:8081/nexus/content/repositories/releases</url>

          <releases><enabled>true</enabled></releases>

          <snapshots><enabled>true</enabled></snapshots>

        </repository>

        <repository>

          <id>snapshots</id>

          <url>http://10.10.10.41:8081/nexus/content/repositories/snapshots</url>

          <releases><enabled>true</enabled></releases>

          <snapshots><enabled>true</enabled></snapshots>

        </repository>

      </repositories>

      <pluginRepositories>

         <pluginRepository>

                <id>releases</id>

                 <url>http://10.10.10.41:8081/nexus/content/repositories/releases</url>

                 <releases><enabled>true</enabled></releases>

                 <snapshots><enabled>true</enabled></snapshots>

               </pluginRepository>

               <pluginRepository>

                 <id>snapshots</id>

                  <url>http://10.10.10.41:8081/nexus/content/repositories/snapshots</url>

                <releases><enabled>true</enabled></releases>

                 <snapshots><enabled>true</enabled></snapshots>

             </pluginRepository>

         </pluginRepositories>

    </profile>

 

  </profiles>

 

以上配置通过mirror和profile的方式将central Repository全部转向到我们自己的Public Repositories,包括release版本和snapshot版本(Maven默认允许从Maven Central Repository下载release版本,这是合理的,如果你使用了别人的snapshot版本,一边你在使用,一边别人在开发,别人将API签名一换,你就悲剧了)。这样一来,我们项目中的所有依赖都从Nexus的Public Repositories下载,由于其中包含了对Maven Central Repository的代理,所以此时Maven Central Repository中的类库也是可以间接下载到的。此时你可以将~/.m2/repository/中所有的内容删除掉,再在项目中执行“mvn clean install”,你将在终端中看到Maven已经开始从Nexus 的Public Repositories中下载依赖了。

 

OK,完成了nexus私服的搭建,项目组开发人员开发时,只要在项目的pom.xml文件中,添加如下pom.xml信息即可获取私服的jar.

如果添加其他的构件时,会先在nexus中下载好,以后才会下载到本地。以后,如果发现nexus已经存在某一jar包,则会直接从nexus下载,如果没有再去网络上下载。这就是搭建nexus的好处。

所以很有必要搭建nexus。

五、FAQ

5.1、was cached in the local repository

  5.1.1、问题描述:

    在使用maven私服仓库编译项目时,由于缺少jar包编译失败,然后上传缺失的jar包至nexus私服仓库,但是编译还失败了,

  出现错误:was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced ->   [Help 1]

  5.1.2、问题原因:

    Maven默认会使用本地缓存的库来编译工程,对于上次下载失败的库,maven会在~/.m2/repository/ / / /目录下创建xxx.lastUpdated文件

  5.1.3、解决办法:

    删除~/.m2/repository/ / / /目录下的*.lastUpdated文件,然后再次编译工程

 

转载于:https://www.cnblogs.com/jassa/p/6165124.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值