Maven默认中央仓库(settings.xml 配置详解)
Maven的安装 安装Maven之前要确保已经安装好了jdk,并且配置好了环境变量JAVA_HOME。具体安装步骤如下: 1. 从apache网上下载maven项目的压缩包。下载地址为:http://maven.apache.org/download.html。比如现在最新的Maven版本是3.0.4,那么我下载好的安装文件就是apache-maven-3.0.4.zip。 2. 将下载后的压缩包解压到Maven的安装目录,比如说是D:develop,那么解压后就是D:developapache-maven-3.0.4。 3. 添加环境变量M2_HOME,其值为Maven的家目录,如D:developapache-maven-3.0.4。 4.添加环境变量M2,其值将为maven安装目录的bin目录,即D:developapache-maven-3.0.4bin,对于windows系统也可以使用%M2_HOME%bin,对于linux系统也可以使用$M2_HOME/bin。 5.将环境变量M2加入到PATH变量中,对于windows系统,可以在PATH变量的值后面加上“;%M2%”,对于linux系统,可以使用“export path=$path:$M2” 6.还有一个可选的环境变量MAVEN_OPTS,该环境变量主要是配置Maven在使用jdk的时候指定JVM属性的。如指定其值为“-Xms256m -Xmx512m”。 经过以上几步之后Maven就安装成功了。接下来我们可以在命令窗口使用mvn --version来验证一下Maven是否安装成功。如能正确输出Maven的安装版本,则表示它安装成功了。 当我们创建一个简单的Maven项目后(只需要在pom.xml配置好依赖),运行mvn clean install就可以把项目构建好,不需要我们手工下载任何jar,这全靠中央仓库的存在,它会自动从仓库下载。这个仓库的定义是在${M2_HOME}/lib/maven-2.0.10-uber.jar 里面。你可以在里面找到orgapachemavenprojectpom-4.0.0.xml 这个文件,在这个文件里面定义了默认中央仓库的地址: <repositories> <repository> <id> central</id> <name> Maven Repository Switchboard</name> <layout> default</layout> <url> http://repo1.maven.org/maven2</url> <snapshots> <enabled> false</enabled> </snapshots> </repository> </repositories> 如果你想覆盖中央仓库的默认地址,那么这里我们就会使用的镜像了,还在setting.xml里面配置: <settings> … <mirrors> <mirror> <id> maven-net-cn</id> <name> Maven China Mirror</name> <url> http://maven.net.cn/content/groups/public/</url> <mirrorOf> central</mirrorOf> </mirror> </mirrors> … </settings> ,表示只为central仓库做镜像,如果想为所有的仓库做镜像那么可以改为: * Maven配置详解 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基本结构如下: <settings xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <localRepository/> <interactiveMode/> <usePluginRegistry/> <offline/> <pluginGroups/> <servers/> <mirrors/> <proxies/> <profiles/> <activeProfiles/> </settings> 简单介绍一下几个主要的配置因素: localRepository 表示本地库的保存位置,也就是maven2主要的jar保存位置,默认在${user.dir}/.m2/repository,如果需要另外设置,就换成其他的路径。 offline 如果不想每次编译,都去查找远程中心库,那就设置为true。当然前提是你已经下载了必须的依赖包。 Servers 在POM中的 distributionManagement元素定义了开发库。然而,特定的username和pwd不能使用于pom.xml,所以通过此配置来保存server信息 <servers> <server> <id>server001</id> <username>my_login</username> <password>my_password</password> <privateKey>${usr.home}/.ssh/id_dsa</privateKey> <passphrase>some_passphrase</passphrase> <filePermissions>664</filePermissions> <directoryPermissions>775</directoryPermissions> <configuration></configuration> </server> </servers>
Mirrors 表示镜像库,指定库的镜像,用于增加其他库 <mirrors> <mirror> <id>planetmirror.com</id> <name>PlanetMirror Australia</name> <url>http://downloads.planetmirror.com/pub/maven2</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors>
Proxies 此设置,主要用于无法直接访问中心的库用户配置。 <proxies> <proxy> <id>myproxy</id> <active>true</active> <protocol>http</protocol> <host>proxy.somewhere.com</host> <port>8080</port> <username>proxyuser</username> <password>somepassword</password> <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts> </proxy> </proxies>
Profiles 类似于pom.xml中的profile元素,主要包括activation,repositories,pluginRepositories 和properties元素 刚开始接触的时候,可能会比较迷惑,其实这是maven2中比较强大的功能。从字面上来说,就是个性配置。 单独定义profile后,并不会生效,需要通过满足条件来激活。 repositories 和pluginRepositories 定义其他开发库和插件开发库。对于团队来说,肯定有自己的开发库。可以通过此配置来定义。 如下的配置,定义了本地开发库,用于release 发布。 <repositories> <repository> <id>repo-local</id> <name>Internal 开发库</name> <url>http://192.168.0.2:8082/repo-local</url> <releases> <enabled>true</enabled> <updatePolicy>never</updatePolicy> <checksumPolicy>warn</checksumPolicy> </releases> <snapshots> <enabled>false</enabled> </snapshots> <layout>default</layout> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>repo-local</id> <name>Internal 开发库</name> <url>http://192.168.0.2:8082/repo-local</url> <releases> <enabled>true</enabled> <updatePolicy>never</updatePolicy> <checksumPolicy>warn</checksumPolicy> </releases> <snapshots> <enabled>false</enabled> </snapshots> <layout>default</layout> </pluginRepository> </pluginRepositories> releases,snapshots:每个产品的版本的Release或者snapshot(注:release和snapshot的区别,release一般是比较稳定的版本,而snapshot基本上不稳定,只是作为快照) properties maven 的properties作为placeholder值,如ant的properties。 包括以下的5种类型值: 1. env.X,返回当前的环境变量 Activation 用于激活此profile <activation> <activeByDefault>false</activeByDefault> <jdk>1.5</jdk> <os> <name>Windows XP</name> <family>Windows</family> <arch>x86</arch> <version>5.1.2600</version> </os> <property> <name>mavenVersion</name> <value>2.0.3</value> </property> <file> <exists>${basedir}/file2.properties</exists> <missing>${basedir}/file1.properties</missing> </file> </activation>
除了使用activation来激活profile,同样可以通过activeProfiles来激活 Active Profiles 表示激活的profile,通过profile id来指定。 <activeProfiles> <activeProfile>env-test</activeProfile> 指定的profile id </activeProfiles> 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |