maven与nexus操作点滴

来源:互联网 发布:手机绘图软件origin 编辑:程序博客网 时间:2024/06/05 02:02


maven

1.      maven的安装

下载maven,安装即可

2.      maven的配置

settings.xml文件,默认不变即可执行

3.      pom.xml文件的配置

通过eclipse建立maven项目,自动生成的pom.xml文件默认不变即可

4.      maven操作

a)       mvn clean 清空target

b)      mvn compile 编译项目

c)      mvn package 打包项目

d)      mvn install 把打包后的包放到本地repository中

e)      mvn deploy 把打包后的包提交到私服(这里用的是nexus)

f)       还有别的操作,不一一列举了

g)      ps: a,b,c,d是本地操作,e是联网操作

 

nexus

1.   nexus安装

去官网下载nexus,带jetty的版本,然后执行nexus start

2.   nexus的简单配置

配置hosts文件

访问:http://myrepos.mvn.com:8081/nexus(8081是nexus默认端口)

登录后进入repositories看到如下内容

下方的Browse Index和Browse Storage是空的,要进行更新索引才能看到内容

 

3.   nexus本地索引

选择central,点击下方的configuration,把Download Remote Indexes设置未true

保存后update index,repair index。经过漫长的等待,可以把索引下载到私服(nexus-2.12.0-01-bundle/sonatype-work/nexus/indexer),这种方式比较耗费时间,而且由于网络原因经常不成功,会让人觉得自己操作有误,然后就会拼命找问题出在哪里,结果徒劳无功。

另一种存储索引到nexus私服的方式是直接下载索引文件,然后解压到私服的相应位置

a)       进入maven中央工厂url(如:http://repo1.maven.org/maven2/),url后跟上./index/即可进入索引页面。

b)      下载该页面中的nexus-maven-repository-index.properties与nexus-maven-repository-index.gz文件

c)      下载一个indexer-cli-5.1.1.jar,把jar与上面2个文件放到同一个目录下,执行java -jar indexer-cli-5.1.1.jar -unexus-maven-repository-index.gz -d indexer即可得到索引文件,把索引文件copy到nexus-2.12.0-01-bundle/sonatype-work/nexus/indexer的指定位置,重启nexus即可获得本地索引

4.   本地storage内容

a)       至此位置,nexus基本可以用了,但是私服本地storage(nexus-2.12.0-01-bundle/sonatype-work/nexus/storage/central)中还是空的

b)      通过下文的配置与操作即可填充本地storage

maven综合nexus

1.   maven配置连接nexus

a)       settings.xml文件中增加profile

<profiles>

   

    <profile>

      <id>nexusProfile</id>

      <repositories>

        <repository>

          <id>nexus</id>

          <name>Nexus Repository</name>

          <url>http:// http://myrepos.mvn.com/:8081/nexus/content/groups/public/</url>

          <releases>

            <enabled>true</enabled>

          </releases>

          <snapshots>

            <enabled>true</enabled> <!-- 默认时关闭的,需要手动开启 -->

          </snapshots>

        </repository>

       

      </repositories>

</profile>

</profiles>

b)      激活profile,在settings.xml文件中增加

<!-- 激活profile配置 -->

  <activeProfiles>

    <activeProfile>nexusProfile</activeProfile>

  </activeProfiles>

c)      此时执行mvn compile将会通过http://myrepos.mvn.com私服下载依赖包,但是私服的storage是空的,那么会经过私服链接到默认中央库下载依赖包,并且所有下载的依赖包会在私服的storage上存储,此时查看browse storage会发现有内容了,私服本地storage(nexus-2.12.0-01-bundle/sonatype-work/nexus/storage/central)中也有内容了

d)       

e)       

2.   maven配置镜像

a)       工厂的镜像,只要mirrorOf中的工厂要被访问,就会自动来找镜像,如果镜像的url中无法访问,就不会去别的地方找了

b)      settings.xml文件中增加

<mirrors>

     <mirror>

      <id>nexusMirror</id>

      <mirrorOf>nexus,central</mirrorOf><!-- 哪些工厂要用镜像,*表示所有工厂都是用这个镜像,这是推荐做法 -->

      <name>Human Readable Name for this Mirror.</name>

      <url>http:// myrepos.mvn.com:8081/nexus/content/groups/public/</url>

    </mirror>

</mirrors>

c)      此时关闭私服,再执行mvn compile,工程就不会从默认工厂下载依赖包,会报错

 

3.   maven配置发布

a)       settings.xml文件中增加

<servers>

   

    <server>

      <id>smi-release</id>

      <username>admin</username>

      <password>admin123</password>

    </server>

   

    <server>

      <id>smi-snapshot</id>

      <username>admin</username>

      <password>admin123</password>

    </server>

  </servers>

b)  上面配置中的id要与项目pom.xml中<distributionManagement>元素中的id一致

4.   pom.xml配置发布

a)       pom.xml文件中增加

<distributionManagement>

            <repository>

                  <id>smi-release</id>

                  <name>smi release first deploy</name>

                  <url>http:// http://myrepos.mvn.com/:8081/nexus/content/repositories/releases/</url>

            </repository>

            <snapshotRepository>

                  <id>smi-snapshot</id>

                  <name>smi snapshot first deploy</name>

                  <url>http:// http://myrepos.mvn.com/:8081/nexus/content/repositories/snapshots/</url>

            </snapshotRepository>

      </distributionManagement>

<id> smi-release </id>,<id> smi-snapshot </id>要与settings.xml中<server>属性中的id一致,deploy时,会检查pom.xml的该id,匹配到setting.xml中的id取得nexus私服的帐号密码获得提交权限

 

 

0 0
原创粉丝点击