Maven中的settings.xml文件默认是在MAVEN_HOME/conf目录下的,用于配置全局maven行为;但是,一般用户都需要复制一份到用户目录/.m2下,以用其配置本用户的maven行为,这是一条最佳实践。
settings.xm的配置其实在settings.xml文件内部已经有详细的说明,本文主要针对个别细节特别强调下,因为如果不注意这些细节,那么狠容易碰到一些莫名其妙的问题。
1、关于profiles的配置
文件中关于profiles的配置有如下描述:
<!-- profiles
| This is a list of profiles which can be activated in a variety of ways, and which can modify
| the build process. Profiles provided in the settings.xml are intended to provide local machine-
| specific paths and repository locations which allow the build to work in the local environment.
|
| For example, if you have an integration testing plugin - like cactus - that needs to know where
| your Tomcat instance is installed, you can provide a variable here such that the variable is
| dereferenced during the build process to configure the cactus plugin.
|
| As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
| section of this document (settings.xml) - will be discussed later. Another way essentially
| relies on the detection of a system property, either matching a particular value for the property,
| or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
| value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
| Finally, the list of active profiles can be specified directly from the command line.
|
| NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
| repositories, plugin repositories, and free-form properties to be used as configuration
| variables for plugins in the POM.
|
|-->
注意NOTE部分,大体意思是:
注意:对于在settings.xml中定义的profiles,将被限制为只能定义构件仓库(artifact repositories),插件仓库(plugin repositories),和自由形式(free-form)的属性,用作对POM中(使用的)插件的配置变量。
言下之意,只能在settings.xml的profiles里配置仓库和属性,不能配置其他的东西,如配置plugins,配置build,配置project等。
如,试图在settings.xml的profiles中增加如下profile,将会在maven执行mvn clean命令时报错:
<profile>
<id>cleanC</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<configuration>
<verbose>true</verbose>
<filesets>
<fileset>
<directory>c:/a</directory>
</fileset>
</filesets>
</configuration>
</plugin>
</plugins>
</build>
</profile>
出错原因就是不能在settings.xml中使用profiles配置插件。经试验,如果不在profiles中配置plugin,还是会报同样的错,看来还是不要在settings.xml中配置plugin了~