maven私有仓库搭建之sonatype nexus篇

最近在准备开发一个java web的项目,由于项目较大需要将项目分解成几个模块给几位同学一起开发,那么必然涉及到多项目的构建问题。一起用过一段时的ant,现在刚好项目还没开始,有时间也学习一下如何使用maven。

maven在项目添加依赖后会去maven的central库https://repo.maven.apache.org/maven2下载所依赖的jar包,如果项目组的多位同学都去central库下载必然会影响效率,我们可以在局域网建一个私有库,来代理和缓存central库,这样可以提高我们得下载速度。与此同时,对于那些由于版权问题无法放大central库中的jar包,如数据库驱动等,我们也可以放到这个私有库中,给项目组其他成员使用。

准备工作

配置maven不再连接central库

首先我们配置maven,使maven不再直接使用central库,解压apache-maven-3.2.5-bin.zip,在apache-maven-3.2.5\conf\settings.xml文件的profiles标签中加入以下配置。
  1. <profile>  
  2.     <id>disable-central</id>  
  3.       <repositories>  
  4.         <repository>  
  5.             <id>central</id>  
  6.             <name>Central Repository</name>  
  7.             <url>https://repo.maven.apache.org/maven2</url>  
  8.             <layout>default</layout>  
  9.             <snapshots>  
  10.                 <enabled>false</enabled>  
  11.                 <updatePolicy>never</updatePolicy>  
  12.             </snapshots>  
  13.             <releases>  
  14.                 <enabled>false</enabled>  
  15.                 <updatePolicy>never</updatePolicy>  
  16.             </releases>  
  17.         </repository>  
  18.     </repositories>  
  19.     <pluginRepositories>  
  20.         <pluginRepository>  
  21.             <id>central</id>  
  22.             <name>Central Repository</name>  
  23.             <url>https://repo.maven.apache.org/maven2</url>  
  24.             <layout>default</layout>  
  25.             <snapshots>  
  26.                 <enabled>false</enabled>  
  27.                 <updatePolicy>never</updatePolicy>  
  28.             </snapshots>  
  29.             <releases>  
  30.                 <enabled>false</enabled>  
  31.                 <updatePolicy>never</updatePolicy>  
  32.             </releases>  
  33.         </pluginRepository>  
  34.     </pluginRepositories>  
  35. </profile>  
   <profile>
        <id>disable-central</id>
          <repositories>
            <repository>
                <id>central</id>
                <name>Central Repository</name>
                <url>https://repo.maven.apache.org/maven2</url>
                <layout>default</layout>
                <snapshots>
                    <enabled>false</enabled>
                    <updatePolicy>never</updatePolicy>
                </snapshots>
                <releases>
                    <enabled>false</enabled>
                    <updatePolicy>never</updatePolicy>
                </releases>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>central</id>
                <name>Central Repository</name>
                <url>https://repo.maven.apache.org/maven2</url>
                <layout>default</layout>
                <snapshots>
                    <enabled>false</enabled>
                    <updatePolicy>never</updatePolicy>
                </snapshots>
                <releases>
                    <enabled>false</enabled>
                    <updatePolicy>never</updatePolicy>
                </releases>
            </pluginRepository>
        </pluginRepositories>
    </profile>
这段配置的意思是既不使用central库的releases的jar包也不使用snapshots的jar包(默认也是不能使用的)。默认使用central库的配置在apache-maven-3.2.5\lib\maven-model-builder-3.2.5.jar\\org\apache\maven\model\pom-4.0.0.xml。
启用这段配置
  1. <activeProfiles>  
  2.     <activeProfile>disable-central</activeProfile>  
  3. </activeProfiles>  
 <activeProfiles>
        <activeProfile>disable-central</activeProfile>
    </activeProfiles>

eclipse中设置jdk和maven

设置jdk


设置maven目录

设置maven配置文件


新建一个maven 工程测试


这个时候由于无法连接central库,会提示新建工程失败


创建私有库

将下载的nexus-2.11.1-01-bundle.zip解压后运行bin目录下的nexus start

我的nexus是搭建在一个liunx虚拟机上的
  1. 虚拟机设为host-only模式,将主机当前使用的网络连接共享;或者虚拟机设置为NAT模式,添加端口映射(下图)
  2. 关闭虚拟机的防火墙

上图为host-only模式添加wlan共享,下图为nat模式添加端口映射

就可以访问虚拟机的nexus库,并进行管理了

默认的用户名是admin,密码是admin123,默认nexus的库都给你建好了,至于各个库的含义可以自行百度,我们使用仓库组public就可以了

添加私有库到maven

和屏蔽central库一样,在配置文件中添加如下配置
  1. <profile>  
  2.   <id>nexus</id>  
  3.   <repositories>  
  4.     <repository>  
  5.       <id>packaging</id>  
  6.       <name>Packaging</name>  
  7.       <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>  
  8.       <releases><enabled>true</enabled></releases>  
  9.       <snapshots><enabled>true</enabled></snapshots>  
  10.     </repository>  
  11.   </repositories>  
  12.  <pluginRepositories>  
  13.     <pluginRepository>  
  14.       <id>packaging</id>  
  15.       <name>Packaging</name>  
  16.       <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>  
  17.       <releases><enabled>true</enabled></releases>  
  18.       <snapshots><enabled>true</enabled></snapshots>  
  19.     </pluginRepository>  
  20.   </pluginRepositories>  
  21. </profile>  
    <profile>
      <id>nexus</id>
      <repositories>
        <repository>
          <id>packaging</id>
          <name>Packaging</name>
          <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
     <pluginRepositories>
        <pluginRepository>
          <id>packaging</id>
          <name>Packaging</name>
          <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  1. <activeProfiles>  
  2.     <activeProfile>disable-central</activeProfile>  
  3.     <activeProfile>nexus</activeProfile>  
  4. </activeProfiles>  
    <activeProfiles>
        <activeProfile>disable-central</activeProfile>
        <activeProfile>nexus</activeProfile>
    </activeProfiles>
eclipse更新配置文件后,在eclipse的maven视图中会显示配置的库

再次新建一个maven工程测试

此时就可以使用eclipse创建maven工程了
同时也可以看到在私有库的代理中缓存了所需的jar包

那么下次项目组的其他人创建工程时就可以直接使用私有库中的jar包了。

Maven学习资源链接

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值