5、Maven-构建配置文件

  1. 什么是构建配置文件?
    1. 配置文件是一组配置的集合,用来设置或者覆盖Maven构建的默认设置
    2. 使用配置文件可以为不同的环境定制构建过程,例如Producation和Development环境。
  2. Profile在pom.xml文件中使用activeProfiles/profiles元素定义,并且可以用很多的方式触发
    1. Profile在构建的时候修改POM文件,并且为变量设置不同的目标环境(例如:在开发、测试、和产品环境中的数据库服务器路径)
  3. Profile的类型
    1. profile主要有三种类型    
  4.  Profile激活
    1.   maven的profile可以通过一下几种方式进行激活
      1.   显示使用命令控制台
      2. 通过maven设置
      3. 基于环境变量(用户/系统变量)
      4. 操作系统配置(Windows family)
      5. 现存/残缺 文件
    2. profile激活示例
      1. 假定工程目录像下面这样
      2.  现在在src/main/resources目录下有三个环境配置文件
        1.  
           
    3. 显示Profile激活
      1. 接下来的例子当中,通过将attach maven-antrun-plugin:run目标添加到测试阶段中,这样可以在我们的不同的Profile中输出文本信息。
      2. 我们通过使用pom.xml来定义不同的Profile,并在命令行窗口中使用maven命令进行激活Profile
      3. 假定有一下的文件
        1. <project 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/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <groupId>com.companyname.projectgroup</groupId>
          <artifactId>project</artifactId>
          <version>1.0</version>
          <profiles>
          <profile>
          <id>test</id>
          <build>
          <plugins>
          <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.1</version>
          <executions>
          <execution>
          <phase>test</phase>
          <goals>
          <goal>run</goal>
          </goals>
          <configuration>
          <tasks>
          <echo>Using env.test.properties</echo>
          <copy file="src/main/resources/env.test.propertiestofile
          ="${project.build.outputDirectory}/env.properties"/>
          </tasks>
          </configuration>
          </execution>
          </executions>
          </plugin>
          </plugins>
          </build>
          </profile>
          </profiles>
          </project>
        2. 假定有以上文件,在控制台跳转到文件所在路径,然后执行一下命令
          1. mvn test -Ptest
          2. Maven 将会显示并且test  Profile的结果
    4. 现在我们练习一下可以按照接下来的步骤这么做
      1. 在pom.xml文件的profiles元素中添加一个profile元素(拷贝已有的profile元素并粘贴到profiles元素的结尾)
      2. 将此profile元素的id从test改为normal
      3. 将任务部分修改为echo env.properties以及copy env.properties到目标目录
      4. 再次重复以上三个步骤,修改id为prod,修改task部分为env.prod.properties
      5. 全部就是这些了,现在有了三个构建配置文件(normal/test/prod)
        1. 在命令行窗口,跳转到pom.xml所在目录,执行以下的mvn命令,使用-P选项来指定profile的名称
        2. mvn test -Pnormal
          mvn test -Pprod
        3. 检查构建的输出有什么不同!
    5. 通过Maven设置激活Profile
      1. 打开maven的setting.xml文件,该文件可以在%USER_HOME%/.m2目录下面找到,
      2. 如果setting.xml不存在,则需要创建一个
      3. 在下面的例子当中,使用activeProfiles节点添加test配置作为激活Profile
        1. <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">
          <mirrors>
          <mirror>
          <id>maven.dev.snaponglobal.com</id>
          <name>Internal Artifactory Maven repository</name>
          <url>http://repo1.maven.org/maven2/</url>
          <mirrorOf>*</mirrorOf>
          </mirror>
          </mirrors>
          <activeProfiles>
          <activeProfile>test</activeProfile>
          </activeProfiles>
          </settings>
        2. 命令行控制台,跳转到pom.xml文件所在的目录,并且执行以下的mvn命令,不要使用-P选项指定Profile的名称
          1. Maven将显示被激活的test Profile的结果
          2. mvn test
    6. 通过环境激活Profile
      1. 现在从maven的setting.xml文件中删除激活的Profile,并且更新pom.xml中的test Profile,将下面的内容添加到profile元素的activation元素中
      2. 当系统属性env被设置成“test”的时候,test配置将会被触发,创建一个环境变量“env”并设置他的值为“test”
        1. <profile>
          <id>test</id>
          <activation>
          <property>
          <name>env</name>
          <value>test</value>
          </property>
          </activation>
          </profile>
      3. 打开控制台窗口,跳转到pom.xml文件所在的目录,并且执行一下的mvn命令
      4. mvn test
    7. 通过操作系统激活Profile
      1. activation元素包含下面的操作系统的信息
      2. 当系统为WindowsXP的时候,test Profile文件会触发
      3. <profile>
        <id>test</id>
        <activation>
        <os>
        <name>Windows XP</name>
        <family>Windows</family>
        <arch>x86</arch>
        <version>5.1.2600</version>
        </os>
        </activation>
        </profile>
      4. 现在控制台跳转到pom.xml所在的目录,并且执行以下的mvn命令。不要使用-P选项指定profile 的名称,Maven将显示被激活的test Profile的结果
      5. mvn  test







原文地址:https://www.cnblogs.com/yangzsnews/p/7506510.html