`

Maven笔记8-Maven中使用Nexus创建私服

 
阅读更多

    搭建私服的优点:

    (1) 降低中央仓库负荷

    (2) 节省外网带宽

    (3) 加速Maven构建

    (4) 自己部署构件

1. Nexus安装

    (1) 下载:http://www.sonatype.org/nexus/,下载包:nexus-2.0.2.war

    (2) 安装

         A. WAR方式安装Nexus

         将nexus-2.0.2.war拷贝至Tomcat的部署目录:C:\Apache-tomcat-7.0.26\webapps下,

         启动Tomcat, 进入Tomcat管理首页即可;

         浏览器访问地址:http://localhost:8080/nexus

         B. Bundle方式安装Nexus

         因为Bundle方式的Nexus默认集成了Jetty容器,因此不需要其他第三方Web容器,解压下载的Bundle

         方式的Nexus文件,有下面的两个目录:

         nexus-webapp-2.0.2:包含Nexus所需要的文件,如启动脚本,依赖JAR等。

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

         WinOS下,进入nexus-webapp-2.0.2/bin/jsw/windows-x86-32运行nexus.bat脚本启动服务。

         浏览器访问地址:http://localhost:8081/nexus

         备注:

         更改访问端口:修改nexus-webapp-2.0.2/conf/plexus.properties

    (3) 登录

         Nexus默认的管理员及密码:admin/admin123

2. Nexus的仓库与仓库组

    Nexus有四种仓库类型:group,hosted,proxy,virtual,仓库格式为:maven2或maven1,仓库属性Policy

    为:Release或Snapshot.

3. 配置Maven从Nexus下载构件

    (1) 在POM中配置Nexus仓库

    <project>

         ...

         <repositories>

            <repository>

                 <id>nexus</id>

                 <name>Nexus</name>

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

                 <release><enabled>true</enabled></release>

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

             </repository>

        </repositories>

        <pluginRepositories>

             <pluginRepository>

                 <id>nexus</id>

                 <name>Nexus</name>

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

                 <release><enabled>true</enabled></release>

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

              </pluginRepository>

         </pluginRepositories>

         ...

    </project>

    上述配置只对当前项目有效,若需让本机所有Maven项目均使用Mavne私服,应该在setting.xml中进行配置。

    (2) 在setting.xml中配置Nexus仓库

    <settings>

          ...

          <profiles>

                <profile>

                    <id>nexus</id>

                    <repositories>

                        <repository>

                          <id>nexus</id>

                          <name>Nexus</name>

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

                          <release><enabled>true</enabled></release>

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

                        </repository>

                     </repositories>

                     <pluginRepositories>

                        <pluginRepository>

                            <id>nexus</id>

                            <name>Nexus</name>

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

                            <release><enabled>true</enabled></release>

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

                       </pluginRepository>

                     </pluginRepositories>

                </profile>

           </profiles>

           <activeProfiles>

               <activeProfile>nexus</activeProfiles>

           </activaProfiles>

            ...

    </settings>

    (3) 配置镜像让Maven只使用私服

    <settings>

          ...

          <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>

                          <name>http://central</name>                        

                          <release><enabled>true</enabled></release>

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

                        </repository>

                     </repositories>

                     <pluginRepositories>

                        <pluginRepository>

                            <id>central</id>

                            <name>http://central</name>                           

                            <release><enabled>true</enabled></release>

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

                       </pluginRepository>

                     </pluginRepositories>

                </profile>

           </profiles>

           <activeProfiles>

               <activeProfile>nexus</activeProfiles>

           </activaProfiles>

            ...

    </settings>

    说明:该配置中仓库及插件仓库的id均为central,即覆盖了超级POM中央仓库的配置,他们的ULR可以不做

    配置,因为所有请求都会通过镜像访问私服地址。

4. 部署构件至Nexus

    (1) 使用Maven部署构件至Nexus

    <project>

        ...

        <distributionManagement>

            <repository>

                  <id>nexus-releases</id>

                  <name>Nexus Releasse Repository</name>

                  <url>http://localhost:8081/nexus/content/repositories/release</url>

            </repository>

            <snapshotRepository>

                  <id>nexus-snapshots</id>

                  <name>Nexus Snapshots Repository</name>

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

            </snapshotRepository>

         </distributionManagement>

         ...

    </project>

    Nexus的仓库对于匿名用户时只读的,为了方便部署,需要在setting.xml配置认证信息:

    <settings>

         ...

         <servers>

             <server>

                 <id>nexus-releases></id>

                 <username>admin</username>

                 <password>*****</password>

             </server>

             <server>

                 <id>nexus-snapshots></id>

                 <username>admin</username>

                 <password>*****</password>

             </server>

          </servers>

          ...

    </settings>

    (2) 手动部署第三方构件至Nexus

    选择宿主仓库如:3rd Party,在Artifact Upload中上传。

5. 其他私服软件

    Apache的Archive和JForg的Artifactory.

分享到:
评论
2 楼 SpringsFeng 2013-04-22  
你好,这个问题可能需要重新配置一个镜像,请添加下面的镜像试试:
<mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
    <mirror>
      <id>ibiblio.org</id>
      <name>ibiblio Mirror of http://repo1.maven.org/maven2/</name>
      <url>http://mirrors.ibiblio.org/maven2/</url>
      <mirrorOf>central</mirrorOf>
      <!-- United States, North Carolina -->
    </mirror>
    <mirror>
      <id>cica.es</id>
      <url>http://ftp.cica.es/mirrors/maven2/</url>
      <mirrorOf>central</mirrorOf>
      <!-- Spain, Sevilla -->
    </mirror>
  </mirrors>
1 楼 pyzheng 2013-04-21  
我也差不多是这么建立的,但是,在http://mvnrepository.com/artifact/org.apache.cxf/cxf-api/2.7.3能见到cxf-api的2.7.3版本,但是在自己的nexus私服Central里面,只能找到2.7.1版本,请问:如何解决?
谢谢.

相关推荐

Global site tag (gtag.js) - Google Analytics