qqjianyue代码工

砌java代码
posts - 62, comments - 9, trackbacks - 0, articles - 10
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

maven2 setting文件详解

Posted on 2008-09-08 11:01 Qzi 阅读(13201) 评论(0)  编辑  收藏 所属分类: appfuse2 and maven2

以下完全照搬官网:http://maven.apache.org/settings.html  只是加上一点自己的理解而已
可以具有两个setting文件:
1.$M2_HOME/conf/settring.xml安装Maven就有的系统setting(各个用户有效的全局的)
2.${user.home}/.m2/settring.xml用户自己加在Repository里的setting文件(用户级的)

元素总览:

<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:配置本地的Repository路径,可以是绝对路径,通常写的是${user.home}/.m2/repository
  • interactiveMode:是否希望maven与用户输入进行交互,true的话,maven就有可能与用户交互,默认是true
  • usePluginRegistry:如果需要使用${user.home}/.m2/plugin-registry.xml来控制plugin的版本的话,就是true,现在默认为false,因为maven2.0,不建议依赖这个文件
  • offline: 是否为离线运行状态,默认为false,对于不能够使用远程Repository的,使用true
  • pluginGroups: 包含一组pluginGroup元素,如果在命令行上没有声明使用某个插件,又要用到这个插件,就在这里的pluginGroup里声明。这个列表默认包括org.maven.plugins
  • Servers:Repositories的详细定义在pom.xml的distributionManagement中,但是
  •     <server>
          <id>server001</id>
          <username>my_login</username>
          <password>my_password</password>
          <privateKey>${user.home}/.ssh/id_dsa</privateKey>
          <passphrase>some_passphrase</passphrase>
          <filePermissions>664</filePermissions>
          <directoryPermissions>775</directoryPermissions>
          <configuration></configuration>
        </server>

    这些是在这里定义的。
    id:与distributionManagement中的repository中的元素id对应,用于表示一个server
    username,password:如果server需要验证的话,这一对元素就是用于验证的
    privateKey, passphrase:也是用于服务器验证的,前者指定私钥privateKey(默认${user.home}/.ssh/id_dsa),后者指定口号passphrase
    filePermissions, directoryPermissions:使用linux中的三位数字形式标示文件与路径的权限,例如664,775等。
    注意:如果使用私钥登录服务器的话,那么password就省略,否则,私钥会被忽略。
    configuration:官网也没有解释。

  • Mirrors
  •     <mirror>
          <id>planetmirror.com</id>
          <name>PlanetMirror Australia</name>
          <url>http://downloads.planetmirror.com/pub/maven2</url>
          <mirrorOf>central</mirrorOf>
        </mirror>
    id,name:server镜像的唯一标识与可读性强的名称(id才是唯一的)

    url:mirror的地址
    mirrorOf:指定这个镜像代表的server的id,那么以后工程引用这个server的时候将不会使用server而是变成使用mirror的地址

    In 2.0.9+, an enhanced syntax is supported:

    • * matches all repo ids.
    • external:* matches all repos except those using localhost or file based repositories. This is used in conjunction with a repository manager when you want to exclude redirecting repositories that are defined for Integration Testing.
    • multiple repos may be specified using , as the delimiter
    • ! may be used in conjunction with one of the above wildcards to exclude a repo id.

    The order is not important from left to right as the wildcards defer to further processing and explicit includes or excludes stop the processing. Additionally, the mirror list will now be ordered using a LinkedHashMap instead of HashMap such that the user may influence match order by changing the order of the definitions in the settings.xml

    Examples:

    • * = everything
    • external:* = everything not on the localhost and not file based.
    • repo,repo1 = repo or repo1
    • *,!repo1 = everything except repo1

  • 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>
    id:表示不同的proxy
    active:当有多个proxy的时候,只有active为true的时候才是有效的
    protocol,host,port:协议,主机,端口
    username,password:如果需要的话就加上
    nonProxyHosts:排除不用代理的站点,使用“|”或者“,”符号分开不同站点,可以使用通配符“*”

  • profiles
  • activation, repositories, pluginRepositories and properties elements
        activation指定激活这个profile的条件。其他激活的方法:(1)setting.xml可以通过activeProfile元素指定profile的id,来明确地激活这个profile。(2)在命令行上加上-P profile的id,也可以激活这个profile,其中可以通过“,”分开几个profile的id来指定多个。
        repositories包含的repository指定依赖,它之前的activation如果符合激活条件的话就开启这个依赖
        pluginRepositories和properties也一样,如果activation符合激活条件,则它们将被激活
    例子1:如果使用jdk-1.4的话,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>

    例子2:如果target-env的属性值为dev的时候,就激活tomcatPath的属性,其他地方肯定会有元素引用这个属性,例如后面引用

        <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>
        -->
      </profiles>

    引用tomcatPath
         | <plugin>
         |   <groupId>org.myco.myplugins</groupId>
         |   <artifactId>myplugin</artifactId>
         |  
         |   <configuration>
         |     <tomcatLocation>${tomcatPath}</tomcatLocation>
         |   </configuration>
         | </plugin>


  • Properties
  • 属性的引用可以通过${},其中:
    1)env.***是引用命令行comandline参数既环境变量等参数的值,例如%{env.PATH}引用环境变量path的值
    2)project.***引用pom文件中project的元素值,例如pom中有个<project><version>1.0</version></project>,那么可以%{project.version}
    3)setting.***引用setting文件中的元素值,例如:<settings><offline>false</offline></settings> is accessible via ${settings.offline}
    4)java.***,java System Properties:在java语言中可以通过java.lang.System.getProperties() 获得的值都可以通过此来获得,例如:${java.home}
    5)***是在<properties/>中定义了的可以直接引用的属性,方法%{someVar}。

  • Repositories
  •      <repositories>
            <repository>
              <id>codehausSnapshots</id>
              <name>Codehaus Snapshots</name>
              <releases>
                <enabled>false</enabled>
                <updatePolicy>always</updatePolicy>
                <checksumPolicy>warn</checksumPolicy>
              </releases>
              <snapshots>
                <enabled>true</enabled>
                <updatePolicy>never</updatePolicy>
                <checksumPolicy>fail</checksumPolicy>
              </snapshots>
              <url>http://snapshots.maven.codehaus.org/maven2</url>
              <layout>default</layout>
            </repository>
          </repositories>
    1)releases, snapshots:(理解不了,原文照搬)These are the policies for each type of artifact, Release or snapshot. With these two sets, a POM has the power to alter the policies for each type independent of the other within a single repository. For example, one may decide to enable only snapshot downloads, possibly for development purposes.
    2)enabled:true或者false来指明这个repository哪个类型被开启
    3)updatePolicy:升级策略。The choices are: always, daily (default), interval:X (where X is an integer in minutes) or never
    4)checksumPolicy:When Maven deploys files to the repository, it also deploys corresponding checksum files. options are to ignore, fail, or warn on missing or incorrect checksums
    5)layout:In the above description of repositories, it was mentioned that they all follow a common layout. This is mostly correct. Maven 2 has a default layout for its repositories; however, Maven 1.x had a different layout. Use this element to specify which if it is default or legacy.

  • Plugin Repositories
  • 结构如同Repositories,但是可以这样理解,repositories用于工程运行之前的,plugin Repositories用于运行。例如tomcat jetty等都是plugins,而jdk,jar等运行之前的工程编译时候就已经用到了

  • activeProfiles如上所说,可以指定一定激活的profile
  • activeProfiles

    只有注册用户登录后才能发表评论。


    网站导航: