您的位置:首页 > 编程语言 > Java开发

Maven学习总结系列八:使用Nexus创建私服

2017-12-18 22:13 429 查看
在前面的文章中,我们已经了解了Maven的核心概念,我们已经可以正常的使用Maven来构建项目了。

 

但使用中遇到问题:

1.在公司开发团队开发,有可能会遇到不能连外网的情况,使我们无法下载Maven构件

2.公司的开发团队比较大,下载的Maven构件也比较多,每人都需要连接外网的中央仓库去下载构件,外网带宽占用很大,下载速度很慢,影响工作效率。

3.一些第三方构件数量多,各项目使用比较杂,无法通过中央仓库下载,直接复制JAR包到项目上不好管理。

 

解决方案设想:

有没有可能在局域网内创建一个类似于Maven中央仓库的私有仓库,这个私有仓库将从Maven中央仓库或其它远程仓库下载的构件缓存起来,当用户需要时,先从私有仓库查,没有再由私有仓库向远程仓库找,下载到私有仓库。

同时允许用户上传自己的构件到私有仓库。

这样就解决了中央仓库负荷高,外网带宽占用高的问题,又可以统一使用Maven机制管理第三方构件。

 

目前有几个软件实现了上面的功能,它们是专门的Maven仓库管理软件(Maven私服):

1.Apache的Archiva
2.JFrog的Artifactory
3.Sonatype的Nexus
 

我们这章讲的是使用Nexus来构建Maven私服。

 

1.1 Nexus的仓库与仓库组

Nexus包含了各种类型的仓库概念,包括代理仓库(proxy)、宿主仓库(hosted)、仓库组(group)等。每一种仓库都提供了丰富实用的配置参数,用户根据需求定制。

 

代理仓库:主要是提供下载缓存构件和插件、如果只是从远程仓库下载构件和插件、那么代理仓库完全足够。

宿主仓库:主要用于存放项目部署的构件、或者第三方构件,用于提供下载。

仓库组:将相同策略的仓库聚合,并通过一致的地址提供服务。(因为项目有可能依赖的中央仓库的release插件和某个宿主仓库中release的插件,为了方便可以配置仓库组作为仓库,而不去单独配置两个仓库)。

 

仓库关系图:

远程仓库:指配置在代理仓库(proxy)的实际仓库(如:互联网上的中央仓库,JBOSS仓库,Alibaba仓库等)

 


从图上可以看到,Maven可以直接从宿主仓库(hosted)下载构件,也可以从代理仓库下载构件,而代理仓库会间接地从远程仓库下载并缓存构件。

仓库组的作用,它实际是将相同性质的仓库聚合起来,对向提供一个访问地址,统一管理这些仓库,它没有实际内容,当有请求时,它会转向其包含的宿主仓库或代理仓库获得实际构件的内容。

 

1.2 Nexus内置的仓库

下面是Nexus默认的仓库:

 


我们可以看到,它有四种类型的仓库:

代理仓库(proxy)、宿主仓库(hosted)、仓库组(group),虚拟(virtual)。

但由virtaulrepository是为maven1服务的,而我们已使用maven2/3,所以不再介绍它。

 

对于4种类型的仓库,Maven内置了一些仓库,这些仓库用户可以不用在配置了。此外仓库还有一个属性Policy(策略),表示仓库为发布(Release)版本还是快照(Snapshot)版本仓库。即Release的仓库只保存发布版本,而Snapshot仓库只保存快照版本。

 

下面介绍一下Nexus默认的仓库:

Central:        代理中央仓库、策略为Release、只会下载和缓存中央仓库中的发布版本构件。

Release:        策略为Release的宿主仓库、用来部署组织内部的发布版本内容。

Snapshot:      策略为Snapshot的宿主仓库、用来部署组织内部的快照版本内容。

3rdparty:    策略为Release的宿主仓库类型、用来部署无法从公共仓库获取的第三方发布版本构件、如oracle连接驱动jar包。

ApacheSnapshot:     策略为Snapshot的代理仓库、用来代理ApacheMaven仓库的快照版本构件。

Public Repositories:该仓库将上述三个hosted类型的仓库和central仓库聚合并通过该URL对外提供服务。

 


 

1.5 配置Maven从Nexus下载构件

远程仓库对本项目有效:在项目的POM.XML中配置远程仓库

大家还记录前章节在pom.xml中配置一个JBOSS的Maven仓库了吗?

--------------------------------------------------------------------------------------------

<repositories>

<repository>

<id>jboss</id>

<name>JBossRepository</name>

<url>http://repository.jboss.com/maven2/</url>
<releases>

<updatePolicy>daily</updatePolicy><!--never,always,interval n -->

<enabled>true</enabled>

<checksumPolicy>warn</checksumPolicy><!--fail,ignore -->

</releases>

<snapshots>

<enabled>false</enabled>

</snapshots>

<layout>default</layout>

</repository>

</repositories>

--------------------------------------------------------------------------------------------

 

我们把public repositories仓库组的地址作为私服的访问地址:

对于本地仓库来说,其实“私服”就是一个远程仓库,配置的方式是跟其它远程仓库是一样的。



--------------------------------------------------------------------------------------------

<repositories>

<repository>

<id>nexus</id>

<name>Nexus Repository</name>

<url>http://192.168.10.100:8080/nexus2/content/groups/public/</url>

<layout>default</layout>

<snapshots>

<enabled>true</enabled>

</snapshots>

<releases>

<enabled>true</enabled>

</releases>

</repository>

</repositories>

<pluginRepositories>

<pluginRepository>

<id>nexus</id>

<name>Nexus Repository</name>

<url>http://192.168.10.100:8080/nexus2/content/groups/public/</url>

<layout>default</layout>

<snapshots>

<enabled>true</enabled>

</snapshots>

<releases>

<enabled>true</enabled>

</releases>

</pluginRepository>

</pluginRepositories>

--------------------------------------------------------------------------------------------

 

 

远程仓库对所有Maven项目有效:在settings.xml的profile元素中配置

配置中使用一个ID为nexus的profile,在profile中配置了相关的仓库信息,同时配置中又使用activeProfile元素将这个profile激活,这样当执行Maven构建时,激活的profile会将仓库配置应用到项目中去。

-------------------------------------------------------------------------------------------

<settings>

 ...

  <profiles>  

    <profile>

      <id>nexus</id>

       <repositories>

        <repository>

          <id>nexus</id>
          <name>NexusRepository</name>
         <url>http://192.168.10.100:8080/nexus2/content/groups/public</url>
         <layout>default</layout>
          <snapshots>
           <enabled>true</enabled>
          </snapshots>
         <releases>
           <enabled>true</enabled>
          </releases>
        </repository>
      </repositories>

  <pluginRepositories>

        <pluginRepository>

            <id>nexus</id>
            <name>NexusRepository</name>
           <url>http://192.168.10.100:8080/nexus2/content/groups/public</url>
           <layout>default</layout>
            <snapshots>
             <enabled>true</enabled>
            </snapshots>
            <releases>
             <enabled>true</enabled>
            </releases>
       </pluginRepository>
   </pluginRepositories>

  </profile>

</profiles>

 

  <!--激活Nexus仓库配置  -->

  <activeProfiles>

   <activeProfile>nexus</activeProfile>

  </activeProfiles>

….

</settings>

-------------------------------------------------------------------------------------------

 

在使用中的问题:

现在本机基本上从Nexus私服下载构件,但有时我们发现,有时本机还是会直接访问(不经过nexus)中央仓库。

 

如果你使用eclipse开发Maven项目,在下载构件时,可以从progress中观察maven是从哪个仓库(私服还是maven中央仓库)下载构件。

 


Maven解决方案:

为了让Maven下载请求都仅通过Nexus,以全面发挥私服的作用,我们可以使用以前讲过的Maven镜像配置,用于拦截所有的构件下载请求,全部都转到私服去下载。

 

注:镜像配置不能配置在pom.xml中,只能在settings.xml中配置。(我测试到将mirror元素配置在pom.xml中会报错)

将镜像配置在项目的settings.xml中

-----------------------------------------------------------------------------------

<settings>

 ...

<!--配置镜像-->

 <mirror>

     <id>nexus</id>

     <mirrorOf>*</mirrorOf>

      <name>nexus mirror for allrepositry</name>

      <url>http://192.168.10.100:8080/nexus2/content/groups/public</url>

</mirror>

...

</settings>

-----------------------------------------------------------------------------------

配置一个mirror元素就可以,因为用配置profile来配置repository,所以连profileActive元素也不用配置了。这样简单的配置,我们就可以将所有的请求转向id为nexus为url。

使用镜像的方式,我们连仓库都不用配置了,因为所有构件的请求(包含依赖与插件)都会自动唯一地址http://192.168.10.100:8080/nexus2/content/groups/public。

 

注:由于镜像仓库完全屏蔽了被镜像仓库,当镜像仓库不稳定或者停止服务的时候,Maven仍将无法访问被镜像仓库,因而将无法下载构件。POM.XML会报miss artifact的错误,这个时候就要检查一下是不是下载构件的远程仓库没有被代理了。

 

案例:如果通过简单的配置实现通过私服的URL接口管理所有仓库的目的,让maven统一通过这个URL下载构件。

实现思想:

1.在nexus中创建proxy代理仓库,指向需要关联的远程仓库,如JBOSS,Snoatype,maven central等这些远程仓库。

2.创建三个hosted仓库,releases,snapshots,3rd-part。分别用于存放自主开发的构件发布版本,快照版本构件,第三方构件。

3.创建一个仓库组public repository,将创建的proxy,hosted(releases,snapshots,3rd-part)聚合到这个组管理。对外只公布public repository的URL接口,通过这个入口统一下载构件。

4.配置开发环境的settings.xml

修改两个元素:

a.指定一个本地仓库的路径,默认会放在C盘的用户目录下,不建议

b.配置一个mirror元素,拦截转发所以下载构件请求到public repository仓库组的URL

 

实现操作:

1.目前使用默认的仓库,public repositories包含了需要的仓库

 


 

2.配置settings.xml

就改两个地方,使用的使用标准的settings.xml默认配置

a.指定一个本地仓库的路径

 <localRepository>D:\Workspaces\maven_repository</localRepository>

b.配置一个mirror元素,拦截转发所以下载构件请求到public repository仓库组的URL

<mirrors>

 <mirror>

      <id>nexus</id>

      <mirrorOf>*</mirrorOf>

      <name>Human Readable Name for thisMirror.</name>

     <url>http://192.168.10.100:8080/nexus2/content/groups/public/</url>

    </mirror>

</mirrors>

 

3.修改完后,在Maven-User Settings中指定我们修改后的配置文件,文件名我们可以随便取的。

点击:Apply,eclipse就会自动按最新的配置更新所有项目的依赖了。

 


注:如果配置文件有问题,指定后,eclipse会提示错误在上面,这个时候,配置文件是无效的(相当于未指定)
 


 

 

 

问题:有些时候,我们希望可以开启central中央仓库对于快照下载的支持

方案:我们可以在settings.xml中创建id为central(与超级pom.xml设定的中央仓库重名),目的是为了覆盖它的设定,开启对快照的支持。

Settings.xml配置如下:

-----------------------------------------------------------------------------------

<settings>

 ...

<!--配置镜像-->

 <mirror>

     <id>nexus</id>

     <mirrorOf>*</mirrorOf>

      <name>nexus mirror for allrepositry</name>

      <url>http://192.168.10.100:8080/nexus2/content/groups/public</url>

    </mirror>

    <!--配置Nexus仓库-->

    <profile>

      <id>nexus</id>

       <repositories>

        <repository>

          <id>central</id>

          <name>CentralRepository</name>

          <url>https://central</url>

         <layout>default</layout>

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

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

        </repository>

      </repositories>

<pluginRepositories>

       <pluginRepository>

           <id>central</id>

            <name>CentralpluginRepository</name>

            <url>https://central</url>

           <layout>default</layout>

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

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

       </pluginRepository>

      </pluginRepositories>

    </profile>

  </profiles>

  <!--激活Nexus仓库配置  -->

  <activeProfiles>

   <activeProfile>nexus</activeProfile>

  </activeProfiles>

...

</settings>

-----------------------------------------------------------------------------------

说明:

1.首先仓库名称取为central,会覆盖超级Pom中的中央仓库配置。我们可以看到https://central是一个没有用的地址,由于所有仓库请求都转向了镜像配置的地址,所以配置的仓库和插件仓库的url无效,所以这个URL是没有意义的。

2.主要目的:开启对快照版本下载的支持,当Maven需要下载发布版或者快照版构件的时候,它首先检查central,看该类型构件是否支持,得到正面回答后,再根据镜像匹配规则转而访问私服仓库地址。(此时私服仓库有可能支持快照也有可能不支持,主要看私服仓库配置)。

 

1.6 部署构件到nexus

上面说了这么多,也只是完成使用一个接口来统一下载构件而已。但如果是我们开发的构件或第三方构件需要上传到私服中呢,又该如何处理?

 

有两种方案上传构件:

1.使用maven部署构件到Nexus

2.手工在nexus网站上传构件

 

1.6.1 使用maven部署构件到Nexus

目的:

将开发生成的快照版本构件直接部署到Nexus中策略为Snaphost的宿主仓库中。

项目正式发布的构件则部署到Nexus中策略为Release的宿主仓库中。

 

完成步骤:

1.在构件项目的pom.xml中指定发布版本与快照版本分别要发布到那个仓库。

2.在settings.xml中配置认证信息,nexus默认是admin/admin123

 

构件项目的POM.XML配置如下:

需要指定发布的仓库,一个releases仓库,一个snapshots仓库。

---------------------------------------------------------------------------------------------

 <!--配置项目生成的构件部署到Nexus私服上 -->:

<project>

……

<distributionManagement>

      <repository>

       <id>nexus-releases</id>

        <name>NexusReleaseRepository</name>     

        <url>http://127.0.0.1:8080/nexus/content/repositories/releases/</url>

      </repository>

     <snapshotRepository>

         <id>nexus-snapshots</id>

        <name>NexusSnapshotsRepository</name>     

        <url>http://127.0.0.1:8080/nexus/content/repositories/snapshots/</url>

     </snapshotRepository>

  </distributionManagement>



  </project>

---------------------------------------------------------------------------------------------

 

在settings.xml配置仓库认证信息

因为不是所有人都可以部署构件到nexus仓库中的,关于认证信息就是拥有管理该仓库的用户,在下面有讲。

<settings>

 ....

  <servers>

 <!--配置nexus仓库认证信息-->

<server>

<id>nexus-releases</id> 

        <username>admin</username> 

        <password>admin123</password> 

    </server> 

    <server> 

        <id>nexus-snapshots</id> 

        <username>admin</username> 

        <password>admin123</password> 

    </server>

...

</settings>

 

使用mvn命令

在项目根目录下执行部署命令,或者在开发工具中执行部署命令

mvn cleandeploy

 


 

 

 

[INFO] Scanning forprojects...

[INFO]                                                                        

[INFO]------------------------------------------------------------------------

[INFO] Buildingtest_downartifact 0.0.1-SNAPSHOT

[INFO]------------------------------------------------------------------------

[INFO]

[INFO] ---maven-clean-plugin:2.5:clean (default-clean) @ test_downartifact ---

[INFO] DeletingD:\Workspaces\eclipse-jee-oxygen_x86_64\test_downartifact\target

[INFO]

[INFO] ---maven-resources-plugin:2.6:resources (default-resources) @ test_downartifact---

[WARNING] Usingplatform encoding (UTF-8 actually) to copy filtered resources, i.e. build isplatform dependent!

[INFO] skip nonexisting resourceDirectoryD:\Workspaces\eclipse-jee-oxygen_x86_64\test_downartifact\src\main\resources

[INFO]

[INFO] ---maven-compiler-plugin:3.1:compile (default-compile) @ test_downartifact ---

[INFO] Nothing tocompile - all classes are up to date

[INFO]

[INFO] ---maven-resources-plugin:2.6:testResources (default-testResources) @test_downartifact ---

[WARNING] Usingplatform encoding (UTF-8 actually) to copy filtered resources, i.e. build isplatform dependent!

[INFO] skip nonexisting resourceDirectoryD:\Workspaces\eclipse-jee-oxygen_x86_64\test_downartifact\src\test\resources

[INFO]

[INFO] ---maven-compiler-plugin:3.1:testCompile (default-testCompile) @ test_downartifact---

[INFO] Nothing tocompile - all classes are up to date

[INFO]

[INFO] ---maven-surefire-plugin:2.12.4:test (default-test) @ test_downartifact ---

[INFO] No tests torun.

[INFO]

[INFO] ---maven-jar-plugin:2.4:jar (default-jar) @ test_downartifact ---

[WARNING] JAR willbe empty - no content was marked for inclusion!

[INFO] Building jar:D:\Workspaces\eclipse-jee-oxygen_x86_64\test_downartifact\target\test_downartifact-0.0.1-SNAPSHOT.jar

[INFO]

[INFO] ---maven-install-plugin:2.4:install (default-install) @ test_downartifact ---

[INFO]InstallingD:\Workspaces\eclipse-jee-oxygen_x86_64\test_downartifact\target\test_downartifact-0.0.1-SNAPSHOT.jartoD:\Workspaces\maven_repository\com\nexus\test\test_downartifact\0.0.1-SNAPSHOT\test_downartifact-0.0.1-SNAPSHOT.jar
[INFO]Installing D:\Workspaces\eclipse-jee-oxygen_x86_64\test_downartifact\pom.xml toD:\Workspaces\maven_repository\com\nexus\test\test_downartifact\0.0.1-SNAPSHOT\test_downartifact-0.0.1-SNAPSHOT.pom
[INFO]

[INFO] ---maven-deploy-plugin:2.7:deploy (default-deploy) @ test_downartifact ---

[INFO] Downloading: http://127.0.0.1:8080/nexus/content/groups/public/org/codehaus/plexus/plexus-utils/1.5.6/plexus-utils-1.5.6.jar
[INFO] Downloaded: http://127.0.0.1:8080/nexus/content/groups/public/org/codehaus/plexus/plexus-utils/1.5.6/plexus-utils-1.5.6.jar(245 KB at 92.8 KB/sec)

[INFO] Downloading: http://127.0.0.1:8080/nexus/content/repositories/snapshots/com/nexus/test/test_downartifact/0.0.1-SNAPSHOT/maven-metadata.xml
[INFO] Uploading: http://127.0.0.1:8080/nexus/content/repositories/snapshots/com/nexus/test/test_downartifact/0.0.1-SNAPSHOT/test_downartifact-0.0.1-20171108.150043-1.jar
[INFO] Uploaded: http://127.0.0.1:8080/nexus/content/repositories/snapshots/com/nexus/test/test_downartifact/0.0.1-SNAPSHOT/test_downartifact-0.0.1-20171108.150043-1.jar (4 KB at 17.7 KB/sec)

[INFO] Uploading: http://127.0.0.1:8080/nexus/content/repositories/snapshots/com/nexus/test/test_downartifact/0.0.1-SNAPSHOT/test_downartifact-0.0.1-20171108.150043-1.pom
[INFO] Uploaded: http://127.0.0.1:8080/nexus/content/repositories/snapshots/com/nexus/test/test_downartifact/0.0.1-SNAPSHOT/test_downartifact-0.0.1-20171108.150043-1.pom (2 KB at 101.4 KB/sec)

[INFO] Downloading: http://127.0.0.1:8080/nexus/content/repositories/snapshots/com/nexus/test/test_downartifact/maven-metadata.xml
Nov 08, 201711:00:43 PM okhttp3.internal.platform.Platform log

WARNING: Aconnection to http://127.0.0.1:8080/ wasleaked. Did you forget to close a response body? To see where this wasallocated, set the OkHttpClient logger level to FINE:Logger.getLogger(OkHttpClient.class.getName()).setLevel(Level.FINE);

[INFO] Uploading: http://127.0.0.1:8080/nexus/content/repositories/snapshots/com/nexus/test/test_downartifact/0.0.1-SNAPSHOT/maven-metadata.xml
[INFO] Uploaded: http://127.0.0.1:8080/nexus/content/repositories/snapshots/com/nexus/test/test_downartifact/0.0.1-SNAPSHOT/maven-metadata.xml
(781 B at 42.4 KB/sec)

[INFO] Uploading: http://127.0.0.1:8080/nexus/content/repositories/snapshots/com/nexus/test/test_downartifact/maven-metadata.xml
[INFO] Uploaded: http://127.0.0.1:8080/nexus/content/repositories/snapshots/com/nexus/test/test_downartifact/maven-metadata.xml
(291 B at 14.2 KB/sec)

[INFO]------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO]------------------------------------------------------------------------

[INFO] Total time:5.049 s

[INFO] Finished at:2017-11-08T23:00:43+08:00

[INFO] Final Memory:10M/243M

[INFO]------------------------------------------------------------------------

 

 


 



 

 


 

1.6.2 手动部署第三方构件到Nexus

对于一些开源或者有版权的jar文件,他们在远程仓库没有,所以只能将他们先下载下来,再手动上传到nexus私服上。

 

确定Nexus中要部署的3rd-party宿主仓库连接

该宿主仓库一般转为用来存放第三方构件的仓库。

 


http://127.0.0.1:8080/nexus/content/repositories/thirdparty/

 

我们将progress公司的progress.jar包上传到3rd-party repositry 中

 

Open the jar package by winrar to get the version

OpenEdge Release11.4.0.018  as of Wed May 13 20:30:17 EDT2015

 


 

 


 





Artifact upload finished successfully

 


 



Let search the OpenEdge jar to get the Maven XML

 


Add below the xml configure in pom.xml of test_downartifact

<dependency>

 <groupId>com.progress.openedge</groupId>

 <artifactId>openedge-jdbc</artifactId>

 <version>11.4.0.018-release</version>

</dependency>

----------------------------------------------------------

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>${junit.version}</version>

<scope>test</scope>

</dependency>

<dependency>
<groupId>com.progress.openedge</groupId>
<artifactId>openedge-jdbc</artifactId>
<version>11.4.0.018-release</version>
</dependency>
</dependencies>

----------------------------------------------------------

 

 


 

1.7 Nexus的权限管理

Nexus权限管理目前主流的权限管理机制是一样的。

1.权限(Privilege):表示是允许操作的功能列表。

2.角色(Role):角色用于管理权限,即可以指定某些权限给某个角色。

3.用户(User):用户不能直接与权限关联,只能通过成为某个角色的成员,以获得这个角色的权限。

 

Case. Sometimes, wedon't hope the project A influence the project B. In turn, the project B don't influence the project A.

So,we will create the a hosted type repository for them respectively to sovle this problem.

 


 

 


 

Built theprivileges such as create, delete ,update and query the artifact base on theboth of repository.

 

The first, let's view  the repository targets

 


 



Selectthe Foo_Releases Repository and All(Maven2), it mean that created privilegeswill match with the all path for Foo_Releases.

 

 


In thisway, we create the privileges for Foo_Snapsshots Repository.

 


 

 

Now, wecan view the privileges which has just created.

 


 

Thesecond , let's create a role which contain above the privileges.

 


 

 






 

Createthe user of project A, and arrange the role named Foo_Developer to it.

 


 

 


 

 

Now, just only the role named Foo_Developer cancreate , update, delete and query the artifact in the both Foo_release andFoo_snapshots repository.

Otheruser just only can view and download the artifact from the repository.

 

1.8 Nexus的调度任务

Nexus提供了一系列可配置的调度任务来方便用户管理系统,用户可以设定这些任务运行方式,例如:每天、每周、手动等,任务调度会在适当的时候在后台运行,当然用户还是能够在界面观察他们的状态。

 

Create a new Task:

 


 



You canlearn more information of nexus via Support Tools

 

 

1.9 关于Nexus的安装

Downloadthe Nexus

https://www.sonatype.com/download-oss-sonatype

 

Nexus提供了两种安装方式,内嵌Jetty的捆绑包(bundle)和WAR包。前者解压后即可单独运行,只要系统中安装了JRE,后者需要一个Servlet容器来运行.

 

把下载的nexus-2.14.5-02.war拷贝到tomcat下,进入tomcat的bin目录下启动startup.bat ,等tomcat将nexus-2.14.5-02.war 全部编译好之后,在浏览器地址栏输



 


 

http://127.0.0.1:8080/nexus-2.14.5-02

 


 

 

 

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  maven java 管理 nexus