Maven解析1-配置文件setting.xml
前言
maven官网:
Maven – Welcome to Apache Maven
菜鸟教程:
Maven 教程 | 菜鸟教程 (runoob.com)
Maven仓库:
Maven Repository: Search/Browse/Explore (mvnrepository.com)
本机使用的Maven 版本:apache-maven-3.6.0 JDK1.8 镜像仓库:如果仓库 X 可以提供仓库 Y 存储的所有的内容,那么仓库 X 就可以说是 仓库 Y 的镜像(也就是说能从仓库 Y 获取的内容,从仓库 X 也能获取) maven的setting.xml文件一般会存在两个地方: 1. 在maven安装目录(全局):apache-maven-3.6.0/conf/setting.xml 2. 在用户安装目录下(当前用户下):${user.home}\.m2\settings.xml 。 配置文件这里对于本地IDEA开发会遇到一个坑,留给大家吧 属性解析 setting<setting>setting 文件的根元素setting>localRepository
<localRepository>本地仓库localRepository> <localRepository>usr/.m2/repositorylocalRepository>interactiveMode
<interactiveMode>Maven 是否与用户交互interactiveMode> <interactiveMode>trueinteractiveMode>offline
<offline>离线模式offline> <offline>falseoffline>pluginGroups
<pluginGroups>插件组pluginGroups>proxies
<proxies> <proxy> <id>optionalid> <active>trueactive> <protocol>httpprotocol> <username>proxyuserusername> <password>proxypasspassword> <host>proxy.host.nethost> <port>80port> <nonProxyHosts>local.net|some.host.comnonProxyHosts> proxy> proxies>servers
<servers> <server> <id>deploymentRepoid> <username>repouserusername> <password>repopwdpassword> server> servers>mirrors
<mirrors> <mirror> <id>mirrorIdid> <mirrorOf>repositoryIdmirrorOf> <name>Human Readable Name for this Mirror.name> <url>http://my.repository.com/repo/pathurl> mirror> mirrors>profiles
<profiles> <profile> <id>nexusid> <repositories> <repository> <id>nexusid> <name>Nexusname> <url>http://100.4.252.5:18080/nexus/content/groups/publicurl> <releases> <enabled>trueenabled> <updatePolicy>dailyupdatePolicy> <checksumPolicy>warnchecksumPolicy> releases> <snapshots> <enabled>trueenabled> <updatePolicy>dailyupdatePolicy> <checksumPolicy>warnchecksumPolicy> snapshots> repository> repositories> <pluginRepositories> <pluginRepository> <id>nexusid> <name>Nexusname> <url>http://100.4.252.5:18080/nexus/content/groups/publicurl> <releases><enabled>trueenabled>releases> <snapshots><enabled>trueenabled>snapshots> pluginRepository> pluginRepositories> profile> profiles>profile 种类 1、pom.xml: pom 文件的 profile 只对当前项目生效 2、用户setting.xml: 用户目录下/.m2/setting.xml 文件中的 profile 只对本机该用户所有的 Maven 项目生效 3、全局setting.xml: Maven 安装目录下的 setting.xml 文件中的 profile 对本机所有的 Maven 项目生效 4、profiles.xml (Maven 2) 还可以在项目根目录下使用一个额外的 profiles.xml 声明,该特性在 Maven 3 中移除,建议将 profiles 添加到 setting.xml 中 profile激活 1、通过命令行激活:用户使用 mvn 命令行参数 -P 加上 profile 的id 来激活 profile,多个通过逗号分隔
mvn clean -Pdev-x,dev-y2、setting.xml 配置文件显式激活:使用 activeProfiles 属性,表示 setting.xml 中的 profile 在所有项目中激活
<activeProfiles> <activeProfile>nexusactiveProfile> activeProfiles>3、系统属性激活:用户可以配置系统属性 test 存在时,自动激活 profile ,如下面第一个示例;用户可以配置系统属性存在,且系统属性值时,自动激活profile,如下面第二个示例
<profiles> <profile> <activeprofile> <property> <name>testname> property> activeprofile> profile> profiles> <profiles> <profile> <activeprofile> <property> <name>testname> <value>xvalue> property> activeprofile> profile> profiles> mvn clean -Ptest=x4、操作系统环境激活:profile 可以根据不同操作系统环境自动激活,如果构建在不同操作系统环境且有差异,可以把这些差异写进 profile
<profiles> <profile> <activeprofile> <os> <name>Window XPname> <family>Windowvalue> <arch>X86arch> os> activeprofile> profile> profiles>5、文件是否存在激活:Maven 能够根据项目中某个文件是否存在来决定是否激活 profile
<profiles> <profile> <activeprofile> <file> <missing>y.propertiesname> <exists>x.propertiesvalue> file> activeprofile> profile> profiles>activeprofile
<activeProfiles> <activeProfile>central(id 需要与 profile 中的 id 保持一致)activeProfile> activeProfiles>
补充国内常用的几个镜像地址-供本地开发学习使用:
<mirrors> <mirror> <id>alimavenid> <name>aliyun mavenname> <url>http://maven.aliyun.com/nexus/content/groups/public/url> <mirrorOf>centralmirrorOf> mirror> <mirror> <id>alisnapshotsid> <name>Ali OSS snapshotsname> <mirrorOf>centralmirrorOf> <url>https://oss.sonatype.org/content/repositories/snapshotsurl> mirror> <mirror> <id>alirelaseid> <name>Ali OSS releasename> <mirrorOf>centralmirrorOf> <url>https://oss.sonatype.org/content/repositories/releasesurl> mirror> <mirror> <id>centralid> <name>Maven Repository Switchboardname> <url>http://repo1.maven.org/maven2/url> <mirrorOf>centralmirrorOf> mirror> <mirror> <id>repo2id> <mirrorOf>centralmirrorOf> <name>Human Readable Name for this Mirror.name> <url>http://repo2.maven.org/maven2/url> mirror> mirrors>