Maven学习- nexus

来源:互联网 发布:诺基亚925下载软件 编辑:程序博客网 时间:2024/06/06 00:32

服务端

首先,为了有专业感,我在阿里云搞了个服务器(cent),在cent上装个nexus,启动它

为了方便,管它叫myrepos.mvn.com吧

本地

我通过eclipse建了个新项目smi_web

首先我要演示如果让smi_web通过cent上的nexus获得依赖包,而不是通过maven中央工厂

基本做法

第一步:修改本地setting.xml文件

要加入一些内容,如下

<profiles>        <profile>            <id>nexusProfile</id>            <repositories>                <repository>                    <id>nexusFactory</id>                    <name>nexusRepository</name>                    <url>http://myrepos.mvn.com/:8081/nexus/content/groups/public/</url>                    <releases>                        <enabled>true</enabled>                    </releases>                    <snapshots>                        <enabled>true</enabled>                    </snapshots>                </repository>            </repositories>        </profile>  </profiles><activeProfiles>    <activeProfile>nexusProfile</activeProfile>  </activeProfiles>
<profile>是啥呢,它是本地maven找依赖包的工厂,<profile>包含<id>和<repositories>
<activeProfiles>呢,每个profile要能正常使用,就需要激活,activeProfiles就负责激活profile

注:activeProfile的内容profile中id的内容

ok,这样就好了,可以通过myrepos.mvn.com来获得依赖jar包了

但是,如果myrepos.mvn.com挂掉了,或者别的原因导致本地不能从myrepos.mvn.com上下载依赖包,那么本地maven还会从中央工厂下载,怎么解决呢。下面说

升级做法, <mirrors>

依然还是setting.xml文件,加入<mirrors>

<mirrors>    <mirror>      <id>mirrorId</id>      <!-- mirrorOf里的内容是profile-repositories-repository-id的内容,可以配置多个,用逗号间隔,一般用*表示所有工厂都走这个镜像 -->      <mirrorOf>*</mirrorOf>      <name>Human Readable Name for this Mirror.</name>      <url>http://myrepos.mvn.com:8081/nexus/content/groups/public/</url>    </mirror>  </mirrors>
加入了镜像,那么之前对profile的激活也就可以删掉了,

  <activeProfiles>    <!-- <activeProfile>nexusProfile</activeProfile> -->  </activeProfiles>
把这个激活注释掉,一切都走mirrors

明天接着写

发布操作

好,目前我们配置好了maven的依赖环境,那么怎么让我们自己开发的项目打成包,上传到nexus上呢。

第一步:在nexus上操作,建立自己的工厂

进入repository,点ADD

然后加入如下内容,这是我建立的一个自定义工厂,id是SmiReleases,这是release版本,会接收version是release的包(之前已经创建好了,看下configuratio好了)


增加完之后,在add一个snapshots的版本,这是snapshots工厂,会接收version是snapshots版本的包,内容如下


第二步:在nexus上操作,为工厂添加权限

刚才我们建立了SmiRelease,接下来为它添加权限,进入security - privileges,按下图操作(SmiSnapshots的权限添加同样操作,不再贴图了)

按照上图的描述,填写完后保存,就会在Privileges的列表中,看到SmiRelease的权限


第三步:在nexus上操作,为Privileges添加Role

那个啥,直接上图吧

第四步:在nexus上操作,增加一个User


-----------


好啦,这样就设置完成啦,接下来就要配置本地maven的setting.xml了

第五步:在本地操作,修改setting.xml

那啥,在setting.xml里,添加如下内容
 <servers>    <server>      <!-- 这个smi_release要记好了,下文pom.xml文件中会用到 -->      <id>smi_release</id>      <!-- 这个smi和smi123是上面配置nexus user中配置的 -->      <username>smi</username>      <password>smi123</password>    </server>        <server>      <!-- 这个smi_snapshots要记好了,下文pom.xml文件中会用到 -->      <id>smi_snapshots</id>      <username>smi</username>      <password>smi123</password>    </server>  </servers>

第六步:在本地操作,修改工程中的pom.xml文件

 <distributionManagement>       <repository>           <!-- 这里的smi_release与上文的那个啥,setting.xml中的<server>里的id对应 -->           <id>smi_release</id>           <name>smi</name>           <!-- 这里的url,就是我们之前在nexus里创建的自己的工厂的url,这是release版本 -->           <url>http://myrepos.mvn.com:8081/nexus/content/repositories/SmiReleases/</url>       </repository>       <snapshotRepository>          <id>smi_snapshots</id>          <name>smi</name>          <!-- 这里是snapshots版本 -->          <url>http://myrepos.mvn.com:8081/nexus/content/repositories/SmiSnapshots/</url>       </snapshotRepository>   </distributionManagement>









0 0
原创粉丝点击