Nexus配置实践

来源:互联网 发布:windows安装snmp 编辑:程序博客网 时间:2024/06/05 10:09
Nexus的安装以及怎样配置proxy就不讲解了,网上随便搜索就是一堆。

我所在搜狐武汉研发这边暂时还没有Nexus可用,所以抽了时间我就搭建了一个。

安装和搭建及其简单,但是如何配置到项目呢,尤其是有多种配置方式。

 

1.想要让maven项目能够从Nexus中取jar,其实只需要在POM中配置如下一段即可:

 

  <repositories>          <repository>              <snapshots>                  <enabled>true</enabled>              </snapshots>              <id>public</id>             <name>Public Repositories</name>              <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>          </repository>      </repositories>      <pluginRepositories>          <pluginRepository>              <id>public</id>              <name>Public Repositories</name>              <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>          </pluginRepository>      </pluginRepositories>

 

 

2. 按照上述方式其实可以简单使用Nexus了,但是难道每个项目都需要这样配置一下,很麻烦,所以下面有另外一种办法,修改全局settings.xml文件,在文件里面增加如下代码:

  <profiles>    <profile>      <id>nexus</id>      <repositories>        <repository>          <id>central</id>          <url>http://central</url>          <releases><enabled>true</enabled></releases>          <snapshots><enabled>false</enabled></snapshots>        </repository>      </repositories>     <pluginRepositories>        <pluginRepository>          <id>central</id>          <url>http://central</url>          <releases><enabled>true</enabled></releases>          <snapshots><enabled>false</enabled></snapshots>        </pluginRepository>      </pluginRepositories>    </profile>    <profile>      <!--this profile will allow snapshots to be searched when activated-->      <id>public-snapshots</id>      <repositories>        <repository>          <id>public-snapshots</id>          <url>http://public-snapshots</url>          <releases><enabled>false</enabled></releases>          <snapshots><enabled>true</enabled></snapshots>        </repository>      </repositories>     <pluginRepositories>        <pluginRepository>          <id>public-snapshots</id>          <url>http://public-snapshots</url>          <releases><enabled>false</enabled></releases>          <snapshots><enabled>true</enabled></snapshots>        </pluginRepository>      </pluginRepositories>    </profile>  </profiles>    <activeProfiles>    <activeProfile>nexus</activeProfile>    <activeProfile>public-snapshots</activeProfile>  </activeProfiles>


这样配置完成后,就可以在任何maven项目中使用咱们的Nexus了。

但是这只是指能够从Nexus上下载Jar,如果我们希望自己的项目能够自动deploy到Nexus上去呢,就需要在settings.xml中再增加如下代码:

  <!--设置server的账号密码,使之可以deploy -->  <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>        <!--This sends everything else to /public -->        <id>releases</id>        <mirrorOf>central</mirrorOf>        <url>http://127.0.0.1:8081/nexus/content/groups/public</url>    </mirror>    <mirror>        <!--This is used to direct the public snapshots repo in the             profile below over to a different nexus group -->        <id>snapshots</id>        <mirrorOf>public-snapshots</mirrorOf>        <url>http://127.0.0.1:8081/nexus/content/groups/public-snapshots</url>      </mirror>  </mirrors>


同时需要在项目的POM文件里增加:

<!-- deploy path --><distributionManagement><repository><id>releases</id><name>Internal Releases</name><url>http://127.0.0.1:8081/nexus/content/repositories/releases/</url></repository><snapshotRepository><id>snapshots</id><name>Internal snapshots</name><url>http://127.0.0.1:8081/nexus/content/repositories/snapshots/</url></snapshotRepository></distributionManagement>


 

好了,这样就完成了Nexus使用配置了。