Maven私服

来源:互联网 发布:牵机药 知乎 编辑:程序博客网 时间:2024/06/12 00:33

需求:正式开发,不同的项目组开发不同的工程。maven-dao工程开发完毕,发布到私服。maven-service从私服下载dao。

公司在自己的局域网内搭建自己的远程仓库服务器,称为私服,私服服务器即是公司内部的maven远程仓库,每个员工的电脑上安装maven软件并且连接私服服务器,员工将自己开发的项目打成jar并发布到私服服务器,其他项目组从私服服务器下载所依赖的构建(jar)。

私服还充当一个代理服务器,当私服上没有jar包会从互联网中央仓库自动下载

这里写图片描述

搭建私服环境

Nexus是Maven仓库管理器,通过nexus可以搭建maven仓库,同时nexus还提供强大的仓库管理功能,构建搜索功能等。

访问其官网http://www.sonatype.org/nexus/,点击DOWNLOADS,点击NEXUS REPOSITORY MANAGER OOS,点击All platforms - Nexus Repository Manager OSS 2.x - bundle.zip,开始下载其安装包nexus-2.14.4-03-bundle.zip。

安装

将其解压,cmd进入其bin目录,执行nexus.bat install。当显示wrapper | nexus installed,则代表安装成功。之后可以在服务中看到有nexus服务。

卸载

cmd进入其bin目录,执行nexus.bat uninstall,查看服务列表nexus已被删除。

启动和停止

方法1:cmd进入bin目录,执行nexus.bat start。停止的话执行nexus.bat stop

方法2:直接在服务列表启动nexus服务。或停止nexus服务。

查看nexus的配置文件conf/nexus.properties

# Jetty sectionapplication-port=8081      #nexus的访问端口配置application-host=0.0.0.0   #nexus主机监听配置(不用修改)nexus-webapp=${bundleBasedir}/nexus   #nexus工程目录nexus-webapp-context-path=/nexus      #nexus的web访问路径# Nexus sectionnexus-work=${bundleBasedir}/../sonatype-work/nexus   #nexus仓库目录runtime=${bundleBasedir}/nexus/WEB-INF    #nexus运行程序目录

访问http://localhost:8081/nexus/

使用Nexus内置账户admin/admin123登陆,点击右上角的Log in,输入账号和密码登陆
这里写图片描述

登陆之后点击左侧的Repositories.

nexus的仓库有4种类型:

  • hosted:宿主仓库,部署自己的jar到这个类型的仓库,包括release和snapshot两部分,Release是公司内部发布版本仓库、Snapshots是公司内部测试版本仓库。
  • proxy:代理仓库,用于代理远程的公共仓库,如maven中央仓库,用户连接私服,私服自动去中央仓库下载jar包或者插件。
  • group:仓库组,用来合并多个hosted/proxy仓库,通常我们配置自己的maven连接仓库组。当你的项目希望在多个repository使用资源时就不需要多次引用了,只需要引用一个group即可。
  • virtual(虚拟):兼容Maven1版本的jar或者插件

nexus仓库默认在D:\Java\nexus-2.14.4-03-bundle\sonatype-work\nexus\storage

  • central:代理仓库,代理中央仓库
  • apache-snapshots:代理仓库,存储snapshot构建,代理地址http://repository.apache.org/snapshots/
  • central-m1:virtual类型仓库,兼容Maven1版本的jar或者插件
  • releases:本地仓库,存储releases构件
  • snapshots:本地仓库,存储snapshots构件
  • thirdparty:第三方仓库,这里指的是可以让你添加自己的第三个库,比如有些构建在中央仓库是不存在的。比如你在中央仓库找不到Oracle的JDBC驱动,这个时候我们就需要自己添加到thirdparty。
  • public:仓库组

这里写图片描述

将项目发布到私服

企业中多个团队协作开发通常会将一些公用的组件、开发模块等发布到私服供其他团队或模块开发人员使用。

这里我们将maven-dao工程打成jar包发布到私服。

配置

第一步:需要在客户端配置maven环境,并修改settings.xml文件,配置连接私服的用户和密码。

此用户名和密码用于私服校验,因为私服需要知道上传的账号和密码是否和私服中的账号和密码一致。

    <servers>        <server>            <id>releases</id>            <username>admin</username>            <password>admin123</password>        </server>        <server>            <id>snapshots</id>            <username>admin</username>            <password>admin123</password>        </server>           </servers>

第二步:配置项目的pom.xml

配置私服仓库的地址,本公司的自己的jar包会上传到私服的宿主仓库,根据工程的版本号决定上传到哪个宿主仓库,如果版本为release则上传到私服的release仓库,如果版本为snapshot则上传到私服的snapshot仓库

    <distributionManagement>        <repository>            <id>releases</id>            <url>http://localhost:8081/nexus/content/repositories/releases</url>        </repository>        <snapshotRepository>            <id>snapshots</id>            <url>http://localhost:8081/nexus/content/repositories/snapshots</url>        </snapshotRepository>    </distributionManagement>

这里写图片描述

配置文件中的id和Configuration中的Repository ID对应。

之后在eclipse中选择项目,执行deploy命令,就可以把他发布到私服上去了。之后就可以在私服中看到上传的东西了。

这里写图片描述

比如我添加了一个接口,但是没有更改版本号还是0.0.1.SNAPSHOT,之后提交,不会把之前的覆盖,而是把这次的jar包新添加进去。当以后下载这个jar包的时候,会下载最新的那个。

疑问:那之前的那些jar包咋办,都放在服务器上岂不是很占用资源?

从私服下载jar包

没有配置nexus之前,如果本地仓库没有,去中央仓库下载,通常在企业中会在局域网内部署一台私服服务器,有了私服,本地项目首先去本地仓库找jar,如果没有找到则连接私服从私服下载jar包,如果私服没有jar包同时作为代理服务器从中央仓库下载jar包,这样做的好处是一方面由私服对公司项目的依赖jar包统一管理,一方面提高下载速度,项目连接私服下载jar包的速度要比项目连接中央仓库的速度快得多。

管理仓库组

nexus中包括很多仓库,hosted中存放的是企业自己发布的jar包及第三方公司的jar包,proxy中存放的是中央仓库的jar,为了方便从私服下载jar包可以将多个仓库组成一个仓库组,每个工程需要连接私服的仓库组下载jar包。

这里写图片描述

将右边“Available Repositories”中的仓库拖拽到左边的“Ordered Group Repository”中组成仓库组,上图中的仓库组包括了本地仓库、代理仓库等。

在setting.xml中配置仓库

<profile>    <!--profile的id-->    <id>dev</id>    <repositories>        <repository>            <!--仓库id,repositories可以配置多个仓库,保证id不重复-->            <id>nexus</id>            <!--仓库地址,即nexus仓库组的地址-->            <url>http://localhost:8081/nexus/content/groups/public/</url>            <!--是否下载release构件-->            <releases><enabled>true</enabled></releases>            <!--是否下载snapshots构件,默认为false-->            <snapshots><enabled>true</enabled></snapshots>        </repository>    </repositories>    <pluginRepositories>        <!--插件仓库,maven的运行依赖的插件,也需要从私服下载-->        <pluginRepository>            <!--插件仓库的id不允许重复,如果重复后边配置会覆盖前边-->            <id>public</id>            <name>Public Repositories</name>            <url>http://localhost:8081/nexus/content/groups/public/</url>        </pluginRepository>    </pluginRepositories></profile><!--使用profile定义仓库需要激活才可生效--><artiveProfiles>    <!--值为profile的id-->    <activeProfile>dev</activeProfile></artiveProfiles>

这样启动了nexus服务后,本地仓库下载jar包都是从nexus里下载,如果nexus里没有,nexus会与maven的中央仓库打交道,然后下载对应的依赖包。当关闭了nexus服务后,本地仓库就会跳过nexus,直接去maven中央仓库下载依赖包了。

如果我们不希望本地仓库直接去maven中央仓库下载,而必须要从nexus里下载依赖包,如果nexus里没有对应的依赖包,就下载不了。要实现这种效果,需要在setting里配置镜像()

    <!-- 设置 maven 的远程仓库为 nexus -->    <mirrors>        <mirror>            <id>mirrorId</id>              <!--表示访问哪些工厂时需要使用镜像-->              <!--<mirrorOf>xxx,central</mirrorOf> -->              <mirrorOf>*</mirrorOf> <!--一般用*号-->              <name>nexus repository</name>            <url>http://localhost:8081/nexus/content/groups/public/</url>        </mirror>    </mirrors>

配置了mirror标签后,profile中配置的url就没有用了,因为都会以mirror里边的为准,但还是要写url标签。

<?xml version="1.0" encoding="UTF-8"?><settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"><!--配置本地库的地址--><localRepository>D:\Java\maven\repository</localRepository><servers>    <server>        <id>releases</id>        <username>admin</username>        <password>admin123</password>    </server>    <server>        <id>snapshots</id>        <username>admin</username>        <password>admin123</password>    </server>       </servers><mirrors>        <mirror>            <id>mirrorId</id>              <!--表示访问哪些工厂时需要使用镜像-->              <!--<mirrorOf>xxx,central</mirrorOf> -->              <mirrorOf>*</mirrorOf> <!--一般用*号-->              <name>nexus repository</name>            <url>http://localhost:8081/nexus/content/groups/public/</url>        </mirror>    </mirrors><profile>    <!--profile的id-->    <id>dev</id>    <repositories>        <repository>            <!--仓库id,repositories可以配置多个仓库,保证id不重复-->            <id>nexus</id>            <!--仓库地址,即nexus仓库组的地址-->            <url>http://ww</url>            <!--是否下载release构件-->            <releases><enabled>true</enabled></releases>            <!--是否下载snapshots构件-->            <snapshots><enabled>true</enabled></snapshots>        </repository>    </repositories>    <pluginRepositories>        <!--插件仓库,maven的运行依赖的插件,也需要从私服下载-->        <pluginRepository>            <!--插件仓库的id不允许重复,如果重复后边配置会覆盖前边-->            <id>public</id>            <name>Public Repositories</name>            <url>http://ww</url>        </pluginRepository>    </pluginRepositories></profile><!--使用profile定义仓库需要激活才可生效--><artiveProfiles>    <!--值为profile的id-->    <activeProfile>dev</activeProfile></artiveProfiles></settings>
原创粉丝点击