对于maven的具体配置文件还不是很了解,一直都是似懂非懂的状态,趁着最近项目不紧张,决定学习一下maven的配置文件.

maven的setting.xml文件最开始介绍了maven的两个作用域.


在maven配置文件的开始部分,介绍了maven的两个作用域,原文大致意思为:

该文件为maven配置文件,他指定了两种级别的作用范围:

其中一种为用户级别,这个级别的配置文件一般放在用户名下的(${user.home}/.m2/settings.xml).m2目录下,他的作用范围是单个的指定的用户.

例如:C:\Users\Administrator\.m2该文件的配置文件,为我电脑管理员用户的用户级配置文件.

maven 配置文件下载 maven的配置文件_xml

另外一种是全局范围:这个级别的配置文件一般放在maven工程的conf(${maven.home}/conf/settings.xml)目录下.它的作用范围是所有的用户(假设所有的用户使用的是同一个maven).

例如:我使用为maven3.3.9版本我的全局配置文件为

maven 配置文件下载 maven的配置文件_配置文件_02

注意:如果用户配置文件不存在,将默认使用全局配置文件.


原文如下:


<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |  1. User Level. This settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |                 NOTE: This location can be overridden with the CLI option:
 |                 -s /path/to/user/settings.xml
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in
 |                 ${maven.home}/conf/settings.xml.
 |                 NOTE: This location can be overridden with the CLI option:
 |                 -gs /path/to/global/settings.xml
 |
 |-->

一:修改本地仓库位置

修改配置文件的localRepository的参数为自定义本地仓库的位置,

<localRepository>G:\Maven-repository\Maven-3.9</localRepository>
<!-- localRepository
该路径将决定存放项目的maven本地仓库的地址.如果不修改该参数,本地仓库默认为用户目录下.m2/repository.
 | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->



二:导包提示    interactiveMode

该参数用于确定在你需要导包的时候是否进行提示,如果这个值为false,maven将会自动判断使用默认的值,或者是基于其他的设置.

默认值为:true

原文如下:

<!-- interactiveMode
   | This will determine whether maven prompts you when it needs input. If set to false,
   | maven will use a sensible default value, perhaps based on some other setting, for
   | the parameter in question.
   |
   | Default: true
  <interactiveMode>true</interactiveMode>
  -->

三:offline参数

该参数决定是否在maven构建时尝试连接网络,他影响羡慕的下载,部署等,默认值为false

<!-- offline
   | Determines whether maven should attempt to connect to the network when executing a build.
   | This will have an effect on artifact downloads, artifact deployment, and others.
   |
   | Default: false
  <offline>false</offline>

四:pluginGroups插件组配置

这是一个组标识符列表,在分解插件时会根据他们的前缀取搜索.也就是说,当执行类似于"mvn prefixLgoal"这样的命令时,maven将会自动的添加组标识符"org.apache,maven.plugins"和"org.codehaus.mojo"(前提是其没有被包含在组标识符列表中).

<!-- pluginGroups
   | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
   | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
   | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
   |-->

其子元素:plugingroup进一步的描述了一个组标识符,应用于插件的搜索.

<pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>



五:proxies元素

该元素将列出所有的能够使本机连接到网络的代理(除非有其他的说明,比如系统参数,或者命令行),在这个列表中的第一个代理规范将会被标记为活动.

<!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->



其子元素proxy用于描述一个用于网络连接的代理规范.

<!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    -->



六:servers

用于说明链接特定服务的验证信息规范,在系统内根据一个独一无二的name取验证.

将username/password 或者 privateKey/passphrase一块使用.

(用户名密码格式)和(私有键密码):

<server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
<server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->



七:mirrors镜像

该参数将列出所有能从远程仓库下载工程的镜像空间,

他的工作流程类似于:

      一个POM文件必须声明一个用于解析某些项目的仓库,但是,这个仓库有时可能会出现阻塞的问题,所以用户可以备份几个镜像空间.

      每个仓库定义都会有一个独一无二的ID,所以我们可以创建一个镜像去引用这个仓库,这个镜像作为备用的下载空间,这个镜像空间将会是这个服务的首选服务器.

<!-- mirrors
   | This is a list of mirrors to be used in downloading artifacts from remote repositories.
   |
   | It works like this: a POM may declare a repository to use in resolving certain artifacts.
   | However, this repository may have problems with heavy traffic at times, so people have mirrored
   | it to several places.
   |
   | That repository definition will have a unique id, so we can create a mirror reference for that
   | repository, to be used as an alternate download site. The mirror site will be the preferred
   | server for that repository.
   |-->

参数定义一个仓库的镜像去代替给定的仓库,这个仓库的镜像服务有一个ID用于匹配其所映射镜像.这个IDS用于继承和查找,

而且这个ID在镜像集合中必须是独一无二的(QAQ,英语水平太差,翻译不出来了= =

)

<mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
  </mirrors>

八:profiles参数

这是配置文件的列表,可以使用多种方式激活,并且可以修改构建过程,配置文件中提供的setting.xml文件的目的是:为本地工作环境提供项目路径和本地仓库,

举个例子来说,如果你有一个集成测试的插件,比如cactus(Cactus是一个基于JUnit框架的简单测试框架,用来单元测试服务端Java代码),他需要知道你的tomcat是否已经安装了,此时,你可以去提供一个变量,在构建过程中对变量引用时,配置cactus插件.

终上所述:资源文件可以用多种方式激活,其中一种激活资源文件(setting.xml),将在稍后讨论,另一种方式本质上依赖于检测系统属性,属性匹配一个特定的值,或者是测试这个值是否存在,资源文件也可以根据JDK的前缀取激活,当在jdk1.4.2_07执行构建的时候,激活资源文件的这个值可能是1.4,也可直接从命令行列出详细的已激活资源文件列表.

需要注意的是:在settting.xml文件中进行配置资源文件时,你在POM中配置的变量局限于项目仓库,插件仓库,和定义的属性.

<!-- 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.
   |

在上文,指定了一组激活构建过程的方式集合,无论是通过

< activatedProfiles / >或者命令行激活的方式,资源文件都必须有一个独一无二的ID(这一句翻译的不知道对不对

),

建议资源文件的命名使用一贯的方式,如‘env-dev’、‘env-test’,‘env-production’,‘user-jdcasey’,‘user-brett’,等。

这样做可以直观的看出这个资源文件的作用,特别是在debug下,你只有一个列表的配置ID.

<!-- profile
     | Specifies a set of introductions to the build process, to be activated using one or more of the
     | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
     | or the command line, profiles have to have an ID that is unique.
     |
     | An encouraged best practice for profile identification is to use a consistent naming convention
     | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
     | This will make it more intuitive to understand what the set of introduced profiles is attempting
     | to accomplish, particularly when you only have a list of profile id's for debug.
     |
     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.

下面的

示例使用的jdk版本激活

<profile>
      <id>jdk-1.4</id>

      <activation>
        <jdk>1.4</jdk>
      </activation>

      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>

下面是另一种激活方式,使用系统参数,'target-env'和"

dev", 给tomcat示例提供一个特殊的路径,你的插件项目配置可能像下面那样:

<!--
     | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
     | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
     | might hypothetically look like:
     |
     | ...
<plugin>
     |   <groupId>org.myco.myplugins</groupId>
     |   <artifactId>myplugin</artifactId>
     |
     |   <configuration>
     |     <tomcatLocation>${tomcatPath}</tomcatLocation>
     |   </configuration>
     | </plugin>
     | ...


如果你只是希望,每当target-env任意值时注入该配置,你需要立即停止activation-property的<value/>.

| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
     |       anything, you could just leave off the <value/> inside the activation-property.
     |
    <profile>
      <id>env-dev</id>

      <activation>
        <property>
          <name>target-env</name>
          <value>dev</value>
        </property>
      </activation>

      <properties>
        <tomcatPath>/path/to/tomcat/instance</tomcatPath>
      </properties>
    </profile>
    -->

九:activeProfiles 被

激活的资源文件

列出构建中所有激活的资源文件

<!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->