maven配置setting.xml文件详解

本文深入探讨Maven2中settings.xml的配置要素,包括localRepository、offline、servers、mirrors、proxies和profiles。详细介绍了如何通过settings.xml实现全局配置和团队共享,以及如何利用profiles实现个性化的项目配置。
maven2 比起maven1 来说,需要配置的文件少多了,主要集中在pom.xml和settings.xml中。
    先来说说settings.xml,settings.xml对于maven来说相当于全局性的配置,用于所有的项目。在maven2中存在两个 settings.xml,一个位于maven2的安装目录conf下面,作为全局性配置。对于团队设置,保持一致的定义是关键,所以 maven2/conf下面的settings.xml就作为团队共同的配置文件。保证所有的团队成员都拥有相同的配置。当然对于每个成员,都需要特殊的 自定义设置,如用户信息,所以另外一个settings.xml就作为本地配置。默认的位置为:${user.dir} /.m2/settings.xml目录中(${user.dir} 指windows 中的用户目录)。
    settings.xml基本结构如下:
   
xml 代码
 
  1. <settings xmlns="http://maven.apache.org/POM/4.0.0"  
  2.           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  
  4.                                http://maven.apache.org/xsd/settings-1.0.0.xsd">  
  5.   <localRepository/>  
  6.   <interactiveMode/>  
  7.   <usePluginRegistry/>  
  8.   <offline/>  
  9.   <pluginGroups/>  
  10.   <servers/>  
  11.   <mirrors/>  
  12.   <proxies/>  
  13.   <profiles/>  
  14.   <activeProfiles/>  
  15. </settings>  

简单介绍一下几个主要的配置因素:
localRepository:表示本地库的保存位置,也就是maven2主要的jar保存位置,默认在${user.dir}/.m2/repository,如果需要另外设置,就换成其他的路径。
offline:如果不想每次编译,都去查找远程中心库,那就设置为true。当然前提是你已经下载了必须的依赖包。
Servers
   在POM中的 distributionManagement元素定义了开发库。然而,特定的username和pwd不能使用于pom.xml,所以通过此配置来保存server信息
 
xml 代码
 
  1. <servers>  
  2.    <server>  
  3.      <id>server001</id>  
  4.      <username>my_login</username>  
  5.      <password>my_password</password>  
  6.      <privateKey>${usr.home}/.ssh/id_dsa</privateKey>  
  7.      <passphrase>some_passphrase</passphrase>  
  8.      <filePermissions>664</filePermissions>  
  9.      <directoryPermissions>775</directoryPermissions>  
  10.      <configuration></configuration>  
  11.    </server>  
  12.  </servers>   

  • id:server 的id,用于匹配distributionManagement库id,比较重要。
  • username, password:用于登陆此服务器的用户名和密码
  • privateKey, passphrase:设置private key,以及passphrase
  • filePermissions, directoryPermissions:当库文件或者目录创建后,需要使用权限进行访问。参照unix文件许可,如664和775
Mirrors
表示镜像库,指定库的镜像,用于增加其他库
 
xml 代码
 
  1. <mirrors>  
  2.    <mirror>  
  3.      <id>planetmirror.com</id>  
  4.      <name>PlanetMirror Australia</name>  
  5.      <url>http://downloads.planetmirror.com/pub/maven2</url>  
  6.      <mirrorOf>central</mirrorOf>  
  7.    </mirror>  
  8.  </mirrors>  

  • id,name:唯一的标志,用于区别镜像
  • url:镜像的url
  • mirrorOf:此镜像指向的服务id
Proxies
此设置,主要用于无法直接访问中心的库用户配置。
 
xml 代码
 
  1. <proxies>  
  2.    <proxy>  
  3.      <id>myproxy</id>  
  4.      <active>true</active>  
  5.      <protocol>http</protocol>  
  6.      <host>proxy.somewhere.com</host>  
  7.      <port>8080</port>  
  8.      <username>proxyuser</username>  
  9.      <password>somepassword</password>  
  10.      <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>  
  11.    </proxy>  
  12.  </proxies>  

  • id:代理的标志
  • active:是否激活代理
  • protocol, host, port:protocol://host:port 代理
  • username, password:用户名和密码
  • nonProxyHosts: 不需要代理的host
Profiles
  类似于pom.xml中的profile元素,主要包括activation,repositories,pluginRepositories 和properties元素
  刚开始接触的时候,可能会比较迷惑,其实这是maven2中比较强大的功能。从字面上来说,就是个性配置。
  单独定义profile后,并不会生效,需要通过满足条件来激活。
 repositories 和pluginRepositories
 定义其他开发库和插件开发库。对于团队来说,肯定有自己的开发库。可以通过此配置来定义。
 如下的配置,定义了本地开发库,用于release 发布。
   
xml 代码
 
  1. <repositories>  
  2.         <repository>  
  3.           <id>repo-local</id>  
  4.        <name>Internal 开发库</name>  
  5.        <url>http://192.168.0.2:8082/repo-local</url>  
  6.           <releases>  
  7.             <enabled>true</enabled>  
  8.             <updatePolicy>never</updatePolicy>  
  9.             <checksumPolicy>warn</checksumPolicy>  
  10.           </releases>  
  11.           <snapshots>  
  12.             <enabled>false</enabled>  
  13.           </snapshots>  
  14.           <layout>default</layout>  
  15.         </repository>  
  16.       </repositories>  
  17.       <pluginRepositories>  
  18.     <pluginRepository>  
  19.     <id>repo-local</id>  
  20.     <name>Internal 开发库</name>  
  21.     <url>http://192.168.0.2:8082/repo-local</url>  
  22.     <releases>  
  23.             <enabled>true</enabled>  
  24.             <updatePolicy>never</updatePolicy>  
  25.             <checksumPolicy>warn</checksumPolicy>  
  26.     </releases>  
  27.     <snapshots>  
  28.     <enabled>false</enabled>  
  29.     </snapshots>  
  30.     <layout>default</layout>  
  31.     </pluginRepository>  
  32.     </pluginRepositories>  

releases, snapshots:每个产品的版本的Release或者snapshot(注:release和snapshot的区别,release一般是比较稳定的版本,而snapshot基本上不稳定,只是作为快照)

properties
  maven 的properties作为placeholder值,如ant的properties。
包括以下的5种类型值:
  1. env.X,返回当前的环境变量
  2. project.x:返回pom中定义的元素值,如project.version
  3. settings.x:返回settings.xml中定义的元素
  4. java 系统属性:所有经过java.lang.System.getProperties()返回的值
  5. x:用户自己设定的值
Activation
  用于激活此profile
 
xml 代码
 
  1. <activation>  
  2.         <activeByDefault>false</activeByDefault>  
  3.         <jdk>1.5</jdk>  
  4.         <os>  
  5.           <name>Windows XP</name>  
  6.           <family>Windows</family>  
  7.           <arch>x86</arch>  
  8.           <version>5.1.2600</version>  
  9.         </os>  
  10.         <property>  
  11.           <name>mavenVersion</name>  
  12.           <value>2.0.3</value>  
  13.         </property>  
  14.         <file>  
  15.           <exists>${basedir}/file2.properties</exists>  
  16.           <missing>${basedir}/file1.properties</missing>  
  17.         </file>  
  18.       </activation>  

  • jdk:如果匹配指定的jdk版本,将会激活
  • os:操作系统
  • property:如果maven能检测到相应的属性
  • file: 用于判断文件是否存在或者不存在

除了使用activation来激活profile,同样可以通过activeProfiles来激活
Active Profiles
表示激活的profile,通过profile id来指定。
 
xml 代码
 
  1. <activeProfiles>  
  2.     <activeProfile>env-test</activeProfile> 指定的profile id  
  3.   </activeProfiles> 
### Maven 3.9.2 `settings.xml` 配置指南 Maven 是一个广泛使用的项目管理工具,其核心配置文件之一是 `settings.xml`。该文件用于定义 Maven 的全局或用户级别的行为,例如本地仓库路径、远程仓库配置、镜像设置、服务器认证信息等。 #### 1. `settings.xml` 文件位置 Maven 支持两种层级的配置: - **全局配置**:位于 Maven 安装目录下的 `conf/settings.xml`,适用于所有用户。 - **用户配置**:位于 `${user.home}/.m2/settings.xml`,仅对当前用户生效,优先级高于全局配置 [^1]。 #### 2. 基本结构与常用配置项 以下是一个典型的 `settings.xml` 配置模板,包含常见的配置项说明: ```xml <settings xmlns="http://maven.apache.org/SETTINGS/1.2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd"> <!-- 设置本地仓库路径 --> <localRepository>/path/to/local/repo</localRepository> <!-- 是否启用交互式模式 --> <interactiveMode>true</interactiveMode> <!-- 离线模式开关 --> <offline>false</offline> <!-- 镜像配置 --> <mirrors> <mirror> <id>aliyun-maven</id> <url>https://maven.aliyun.com/repository/public</url> <mirrorOf>central,!third-party-repos</mirrorOf> </mirror> </mirrors> <!-- 服务器认证信息 --> <servers> <server> <id>my-releases</id> <username>admin</username> <password>admin123</password> </server> </servers> <!-- 配置文件激活条件 --> <profiles> <profile> <id>default-jdk-17</id> <activation> <activeByDefault>true</activeByDefault> <jdk>17</jdk> </activation> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties> </profile> </profiles> <!-- 激活的 profile 列表 --> <activeProfiles> <activeProfile>default-jdk-17</activeProfile> </activeProfiles> </settings> ``` #### 3. 关键配置详解 - **本地仓库(`<localRepository>`)** 用于指定 Maven 存储依赖包的本地路径,默认路径为 `${user.home}/.m2/repository`。可以通过此标签修改默认路径 [^3]。 - **镜像(`<mirrors>`)** 可以配置远程仓库的镜像地址,例如使用阿里云镜像加速依赖下载。其中 `<mirrorOf>` 指定要镜像的仓库 ID,支持通配符如 `*` 或排除特定仓库如 `!central` [^2]。 - **服务器认证(`<servers>`)** 如果使用了私有仓库(如 Nexus),需要在此配置用户名和密码,确保 Maven 能够正确访问受保护的资源。 - **构建 JDK 版本控制(`<profiles>`)** 在 `<profiles>` 中可以配置不同的构建环境,例如针对不同 JDK 版本的编译器选项。配合 `<activation>` 标签可实现自动激活。 #### 4. 推荐实践 - 修改本地仓库路径时,确保目标路径存在且具有写权限。 - 使用镜像时建议避免完全覆盖中央仓库(`central`),以免影响某些插件的行为。 - 敏感信息(如密码)应使用加密方式存储,可通过 `mvn --encrypt-password` 加密后配置到 `<server>` 中。 - 不同开发环境(如测试、生产)可分别配置多个 `<profile>` 并通过 `<activeProfiles>` 控制激活状态。 ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值