使用Nexus搭建Maven局域网服务器

 原因

使用Maven进行项目的声明周期管理,方便对项目依赖和插件进行管理,同时便于项目的统一管理,提升项目开发效率。但是实际工作中会有以下问题:
    1  因为安全考虑,有些公司不给开发人员提供外网。因此不能使用maven访问远程的仓库地址。

2  大家同时上网,导致网速比较慢,maven下载构件效率比较低。

3  由于版权的问题,有些第三方jar包无法从远程仓库下载。

4  有些jar包是公司内部使用的,自然也无法从远程maven仓库下载。

怎么办呢?

   使用Nexus搭建私服

所谓私服,是一种特殊的远程服务器,代理广域网上的远程仓库,供局域网内Maven用户使用。Nexus就是其中一种。

需要下载maven构件时候, 先从私服请求,不存在则从外部远程仓库下载,缓存到私服后供下载。

 对于 一些无法从外部下载的构件,就可以上传到私服供下载使用。

使用Nexus搭建Maven局域网服务器

使用步骤:

1  JDK  Maven等环境配置正确后,下载nexus对应版本

  2  拷贝到目录下,执行bin目录下命令:nexus.exe /run

  3  启动后访问http://localhost:8081。

  功能简介:

1 搜索构件:

使用Nexus搭建Maven局域网服务器

1 Repository私服仓库相关:

备注:  登录默认用户名是 admin 密码是 admin123

使用Nexus搭建Maven局域网服务器

默认仓库说明:

(1) maven-central:  

代理maven中央库,默认从https://repo1.maven.org/maven2/拉取jar

(2) maven-releases:

公司私有构件库,存放release发型jar

(3) maven-snapshots:

公司级私有构件库,保存快照(调试版本)jar

(4) maven-public:

共有仓库分组,把上面三个仓库组合在一起对外提供服务,方便在本地配置文件中使用。

repository的类型:

(1) hosted

本地仓库,通常我们会部署自己的构件到这一类型的仓库。比如公司的构件。

(2) proxy

代理仓库,它们被用来代理远程的公共仓库,如maven中央仓库。

(3) group

仓库组,用来合并多个hosted/proxy仓库,当你的项目希望使用多个repository资源时就不需要多次引用了,只需要引用一个group即可。

创建新仓库,以创建一个保存第三方构件库为例:

使用Nexus搭建Maven局域网服务器

为方便使用,通常加入maven-public组:

使用Nexus搭建Maven局域网服务器

构件下载配置:

(1) 配置当前项目从私服下载构件:

<!-- 配置项目下载构件库--> à

<repositories>

  <repository>

  <id>nexus</id>

  <name>nexus</name>

  <url>http://ip地址:8081/repository/maven-public/</url>

  <releases>

      <enabled>true</enabled>

    </releases>

    <snapshots>

      <enabled>true</enabled>

    </snapshots>

  </repository>

</repositories>

<!—从私服下载项目插件-->

<pluginRepositories>  

        <pluginRepository>  

            <id>nexus</id>  

            <name>nexus</name>             

            <url>http://ip地址:8081/repository/maven-public/</url>  

            <releases>  

                <enabled>true</enabled>  

            </releases>  

            <snapshots>  

                <enabled>true</enabled>  

            </snapshots>  

        </pluginRepository>  

</pluginRepositories>  

(2) 对当前用户所有项目均有效:在settings.xml

<!—用户级配置,必须**才能生效-->

<profiles>

<profile>

<id>nexusRes</id>

<repositories>

<repository>

<id>nexus</id>

<name>Nexus epository</name>

<url>http://localhost:8081/repository/maven-public/</url>

<layout>default</layout>

<snapshotPolicy>always</snapshotPolicy>

</repository>

</repositories>

</profile>

</profiles>

<!—**该配置-->

<activeProfiles>

<activeProfile>nexusRes</activeProfile>

</activeProfiles>

构件发布配置:

  前提:登录私服是需要权限认证的,在settings.xml中配置

<!--nexus默认是需要登陆操作的,在此配置权限认证信息,id必须分别对应pom.xml中<distributionManagement>元素中release和snapshot的id -->

<servers>

<server>

<id>releases</id>

<username>admin</username>

<password>admin123</password>

</server>

<server>

<id>snapshots</id>

<username>admin</username>

<password>admin123</password>

</server>

</servers>

(1) 非当前项目构件,使用命令行方式

mvn deploy:deploy-file -DgroupId=oracle -DartifactId=oracle -Dversion=0.0.0 -Dpackaging=jar  -Dfile=e:\ojdbc6.jar -Durl=http://localhost:8081/repository/maven-releases/ -DrepositoryId=releases

其中:url是构件发布的私服仓库地址,repositoryId必须对应settings.xml中配置的server的id

使用Nexus搭建Maven局域网服务器

(1) 发布当前项目构件,在pom.xml中设置

<!-- 发布构件至nexus私服,id必须和settings中一致 -->

<distributionManagement>

<repository>

<id>releases</id>

<name>maven-releases</name>

<url>http://localhost:8081/repository/maven-releases/</url>

</repository>

<!--项目版本号中有SNAPSHOT标识的,会发布到Nexus Snapshots Repository, 否则发布到Nexus Release Repository-->

<snapshotRepository>

<id>snapshots</id>

<name>maven-snapshots</name>   <url>http://localhost:8081/repository/maven-snapshots/</url>

</snapshotRepository>

</distributionManagement>