maven私服的setting文件设置

<!--
xuze added by:添加了一些注释,利于新人理解
Date:2011年7月18日
-->
<settings>
  <!-- 配置镜像 -->
  <mirrors>      
    <mirror>
      <!-- 此镜像一般用来作为公司内部开发的版本快照,作为public-snapshots仓库的镜像地址 -->
      <!-- 镜像的id,id用来区分不同的mirror元素。 --> 
      <id>nexus-public-snapshots</id>
      <!-- 被镜像的服务器的id。例如,如果我们要设置了一个Maven中央仓库(http://repo1.maven.org/maven2)的镜像,
          就需要将该元素设置成central。这必须和中央仓库的id “central”完全一致。 -->
      <mirrorOf>public-snapshots</mirrorOf>
      <!-- 该镜像的URL。 --> 
      <url>http://repos.d.xxx.com/nexus/content/groups/public-snapshots</url>
    </mirror>
    
    <mirror>
      <!-- 此镜像一般用来作为公司第三方引用基础类库镜像,是所有仓库的镜像地址 -->
      <id>nexus</id>
      <!-- 为*表示为所有的仓库做镜像,有了这个配置,所有的构建都会包含public组,如果你想包含public-snapshots组,
          你必须添加public-snapshots这个Profile,通过在命令行使用如下的 -P 标志:$ mvn -P public-snapshots clean install -->
      <mirrorOf>*</mirrorOf>
      <url>http://repos.d.xxx.com/nexus/content/groups/public</url>
    </mirror>    
  </mirrors>
  
  <!-- settings.xml中的profile元素是pom.xml中profile元素的裁剪版本。它包含了activation, repositories, pluginRepositories 和 properties元素。
      这里的profile元素只包含这四个子元素是因为这里只关心构建系统这个整体(这正是settings.xml文件的角色定位),而非单独的项目对象模型设置。  
    如果一个settings中的profile被激活,它的值会覆盖任何其它定义在POM中或者profile.xml中的带有相同id的profile。 --> 
  <profiles>
    <profile>
      <id>development</id>
      <!-- 仓库。仓库是Maven用来填充构建系统本地仓库所使用的一组远程项目。而Maven是从本地仓库中使用其插件和依赖。
          不同的远程仓库可能含有不同的项目,而在某个激活的profile下,可能定义了一些仓库来搜索需要的发布版或快照版构件。有了Nexus,这些应该交由Nexus完成 -->
      <repositories>
        <repository>
          <id>central</id>
          <!-- 虚拟的URL形式,指向镜像的URL,因为所有的镜像都是用的是nexus,这里的central实际上指向的是http://repos.d.xxx.com/nexus/content/groups/public -->
          <url>http://central</url>
          <!-- 表示可以从这个仓库下载releases版本的构件-->
          <releases><enabled>true</enabled></releases>
          <!-- 表示可以从这个仓库下载snapshot版本的构件 -->
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
     
     <!-- 插件仓库。仓库是两种主要构件的家。第一种构件被用作其它构件的依赖。这是中央仓库中存储大部分构件类型。
         另外一种构件类型是插件。Maven插件是一种特殊类型的构件。由于这个原因,插件仓库独立于其它仓库。
        pluginRepositories元素的结构和repositories元素的结构类似。每个pluginRepository元素指定一个Maven可以用来寻找新插件的远程地址。 -->  
     <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</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形式,指向镜像的URL,这里指向的是http://repos.d.xxx.com/nexus/content/groups/public-snapshots -->
          <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>
  
  <!-- 激活的Profile。activation元素并不是激活profile的唯一方式。settings.xml文件中的activeProfile元素可以包含profile的id,
      任何在activeProfile中定义的profile id,不论环境设置如何,其对应的profile都会被激活。如果没有匹配的profile,则什么都不会发生。
    profile也可以通过在命令行,使用-P标记和逗号分隔的列表来显式的激活(如,-P test)。
    要了解在某个特定的构建中哪些profile会激活,可以使用maven-help-plugin(mvn help:active-profiles)。 -->  
  <activeProfiles>
      <!-- 没有显示激活public-snapshots -->
    <activeProfile>development</activeProfile>
  </activeProfiles>

<!-- 自定义本地仓库地址,其默认值为~/.m2/repository -->
<localRepository>/data/maven-repository</localRepository>

  <!-- 发布的服务器和密码,暂时未限制权限 -->
   <servers>
    <server>
      <!-- 发布的位置在POM中配置,以ID为关联,有很多公用的信息需要配置在POM文件里,最佳实践是定义一个公司级别的root pom -->
      <id>archiva.internal</id>
      <username>maven</username>
      <password>1q2w3e4r</password>
    </server>
    <server>
      <id>archiva.snapshots</id>
      <username>maven</username>
      <password>1q2w3e4r</password>
    </server>
  </servers>
</settings>




分发构件至远程仓库

mvn install 会将项目生成的构件安装到本地Maven仓库,mvn deploy 用来将项目生成的构件分发到远程Maven仓库。本地Maven仓库的构件只能供当前用户使用,在分发到远程Maven仓库之后,所有能访问该仓库的用户都能使用你的构件。

我们需要配置POM的distributionManagement来指定Maven分发构件的位置,如下:

<project>    
  ...    
  <distributionManagement>    
    <repository>    
      <id>nexus-releases</id>    
      <name>Nexus Release Repository</name>    
      <url>http://127.0.0.1:8080/nexus/content/repositories/releases/</url>    
    </repository>    
    <snapshotRepository>    
      <id>nexus-snapshots</id>    
      <name>Nexus Snapshot Repository</name>    
      <url>http://127.0.0.1:8080/nexus/content/repositories/snapshots/</url>    
    </snapshotRepository>    
  </distributionManagement>    
  ...    
</project>

Maven区别对待release版本的构件和snapshot版本的构件,snapshot为开发过程中的版本,实时,但不稳定,release版本则比较稳定。Maven会根据你项目的版本来判断将构件分发到哪个仓库。

一般来说,分发构件到远程仓库需要认证,如果你没有配置任何认证信息,你往往会得到401错误。这个时候,如下在settings.xml中配置认证信息:

Xml代码

<settings>    
  ...    
  <servers>    
    <server>    
      <id>nexus-releases</id>    
      <username>admin</username>    
      <password>admin123</password>    
    </server>    
    <server>    
      <id>nexus-snapshots</id>    
      <username>admin</username>    
      <password>admin123</password>    
    </server>      
  </servers>    
  ...    
</settings>

需 要注意的是,settings.xml中server元素下id的值必须与POM中repository或snapshotRepository下id的 值完全一致。将认证信息放到settings下而非POM中,是因为POM往往是它人可见的,而settings.xml是本地的。

转载于:https://my.oschina.net/u/1458864/blog/370466

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值