关于spring-cloud:第五篇Maven-构建配置文件之Spring-Cloud直播商城-b2b2c电子商务技术总结

4次阅读

共计 7036 个字符,预计需要花费 18 分钟才能阅读完成。

Maven 构建配置文件
构建配置文件是一系列的配置项的值,能够用来设置或者笼罩 Maven 构建默认值。

应用构建配置文件,你能够为不同的环境,比如说生产环境(Production)和开发(Development)环境,定制构建形式。

配置文件在 pom.xml 文件中应用 activeProfiles 或者 profiles 元素指定,并且能够通过各种形式触发。配置文件在构建时批改 POM,并且用来给参数设定不同的指标环境(比如说,开发(Development)、测试(Testing)和生产环境(Production)中数据库服务器的地址)。

构建配置文件的类型
构建配置文件大体上有三种类型:
配置文件激活
Maven 的构建配置文件能够通过多种形式激活。

应用命令控制台输出显式激活。
通过 maven 设置。
基于环境变量(用户或者零碎变量)。
操作系统设置(比如说,Windows 系列)。
文件的存在或者缺失。
配置文件激活实例
假设我的项目构造如下:

其中在 src/main/resources 文件夹下有三个用于测试文件:留神:这三个配置文件并不是代表构建配置文件的性能,而是用于本次测试的目标;比方,我指定了构建配置文件为 prod 时,我的项目就应用 env.prod.properties 文件。

留神:上面的例子依然是应用 AntRun 插件,因为此插件能绑定 Maven 生命周期阶段,并通过 Ant 的标签不必编写一点代码即可输入信息、复制文件等,经此而已。其余的与本次构建配置文件无关。

1、配置文件激活
profile 能够让咱们定义一系列的配置信息,而后指定其激活条件。这样咱们就能够定义多个 profile,而后每个 profile 对应不同的激活条件和配置信息,从而达到不同环境应用不同配置信息的成果。

以下实例,咱们将 maven-antrun-plugin:run 指标增加到测试阶段中。这样咱们能够在不同的 profile 中输入文本信息。咱们将应用 pom.xml 来定义不同的 profile,并在命令控制台中应用 maven 命令激活 profile。

pom.xml 文件如下:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jsoft.test</groupId>
  <artifactId>testproject</artifactId>
  <packaging>jar</packaging>
  <version>0.1-SNAPSHOT</version>
  <name>testproject</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <profiles>
      <profile>
          <id>test</id>
          <build>
              <plugins>
                 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</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.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
                          </tasks>
                          </configuration>
                       </execution>
                    </executions>
                 </plugin>
              </plugins>
          </build>
      </profile>
      <profile>
          <id>normal</id>
          <build>
              <plugins>
                 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                       <execution>
                          <phase>test</phase>
                          <goals>
                             <goal>run</goal>
                          </goals>
                          <configuration>
                          <tasks>
                             <echo>Using env.properties</echo>
                             <copy file="src/main/resources/env.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
                          </tasks>
                          </configuration>
                       </execution>
                    </executions>
                 </plugin>
              </plugins>
          </build>
      </profile>
      <profile>
          <id>prod</id>
          <build>
              <plugins>
                 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                       <execution>
                          <phase>test</phase>
                          <goals>
                             <goal>run</goal>
                          </goals>
                          <configuration>
                          <tasks>
                             <echo>Using env.prod.properties</echo>
                             <copy file="src/main/resources/env.prod.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
                          </tasks>
                          </configuration>
                       </execution>
                    </executions>
                 </plugin>
              </plugins>
          </build>
      </profile>
   </profiles>
</project>

留神:构建配置文件采纳的是 <profiles> 节点。

阐明:下面新建了三个 <profiles>,其中 <id> 辨别了不同的 <profiles> 执行不同的 AntRun 工作;而 AntRun 的工作能够这么了解,AntRun 监听 test 的 Maven 生命周期阶段,当 Maven 执行 test 时,就触发了 AntRun 的工作,工作外面为输入文本并复制文件到指定的地位;而至于要执行哪个 AntRun 工作,此时构建配置文件起到了传输指定的作用,比方,通过命令行参数输出指定的 <id>。

执行命令:mvn test -Ptest
提醒:第一个 test 为 Maven 生命周期阶段,第 2 个 test 为构建配置文件指定的 <id> 参数,这个参数通过 -P 来传输,当然,它能够是 prod 或者 normal 这些由你定义的 <id>。

运行的后果如下:
能够看出胜利的触发了 AntRun 的工作。并且是对应构建配置文件下的 <id> 为 test 的工作。

再测试其余两个命令,后果如下:2、通过 Maven 设置激活配置文件
关上 %USER_HOME%/.m2 目录下的 settings.xml 文件,其中 %USER_HOME% 代表用户主目录。如果 setting.xml 文件不存在就间接拷贝 %M2_HOME%/conf/settings.xml 到 .m2 目录,其中 %M2_HOME% 代表 Maven 的装置目录。

配置 setting.xml 文件,减少 <activeProfiles> 属性:

<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">
   ...
   <activeProfiles>
      <activeProfile>test</activeProfile>
   </activeProfiles>
</settings>

执行命令:
mvn test
提醒 1:此时不须要应用 -Ptest 来输出参数了,下面的 setting.xml 文件的 <activeprofile> 曾经指定了 test 参数代替了。

提醒 2:同样能够应用在 %M2_HOME%/conf/settings.xml 的文件进行配置,成果统一。

执行后果:3、通过环境变量激活配置文件
先把上一步测试的 setting.xml 值全副去掉。

而后在 pom.xml 外面的 <id> 为 test 的 <profile> 节点,退出 <activation> 节点:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jsoft.test</groupId>
  <artifactId>testproject</artifactId>
  <packaging>jar</packaging>
  <version>0.1-SNAPSHOT</version>
  <name>testproject</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <profiles>
      <profile>
          <id>test</id>
          <activation>
            <property>
               <name>env</name>
               <value>test</value>
            </property>
          </activation>
          <build>
              <plugins>
                 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</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.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
                          </tasks>
                          </configuration>
                       </execution>
                    </executions>
                 </plugin>
              </plugins>
          </build>
      </profile>
      <profile>
          <id>normal</id>
          <build>
              <plugins>
                 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                       <execution>
                          <phase>test</phase>
                          <goals>
                             <goal>run</goal>
                          </goals>
                          <configuration>
                          <tasks>
                             <echo>Using env.properties</echo>
                             <copy file="src/main/resources/env.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
                          </tasks>
                          </configuration>
                       </execution>
                    </executions>
                 </plugin>
              </plugins>
          </build>
      </profile>
      <profile>
          <id>prod</id>
          <build>
              <plugins>
                 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                       <execution>
                          <phase>test</phase>
                          <goals>
                             <goal>run</goal>
                          </goals>
                          <configuration>
                          <tasks>
                             <echo>Using env.prod.properties</echo>
                             <copy file="src/main/resources/env.prod.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
                          </tasks>
                          </configuration>
                       </execution>
                    </executions>
                 </plugin>
              </plugins>
          </build>
      </profile>
   </profiles>
</project>

执行命令:
mvn test -Denv=test
提醒 1:下面应用 -D 传递环境变量,其中 env 对应方才设置的 <name> 值,test 对应 <value>。

提醒 2:在 Windows 10 上测试了零碎的环境变量,然而不失效,所以,只能通过 -D 传递。

执行后果:4、通过操作系统激活配置文件
activation 元素蕴含上面的操作系统信息。当零碎为 windows XP 时,test Profile 将会被触发。

<profile>
   <id>test</id>
   <activation>
      <os>
         <name>Windows XP</name>
         <family>Windows</family>
         <arch>x86</arch>
         <version>5.1.2600</version>
      </os>
   </activation>
</profile>

当初关上命令控制台,跳转到 pom.xml 所在目录,并执行上面的 mvn 命令。不要应用 -P 选项指定 Profile 的名称。Maven 将显示被激活的 test Profile 的后果。
mvn test
5、通过文件的存在或者缺失激活配置文件
当初应用 activation 元素蕴含上面的操作系统信息。当 target/generated-sources/axistools/wsdl2java/com/companyname/group 缺失时,test Profile 将会被触发。

<profile>
   <id>test</id>
   <activation>
      <file>
         <missing>target/generated-sources/axistools/wsdl2java/
         com/companyname/group</missing>
      </file>
   </activation>
</profile>

当初关上命令控制台,跳转到 pom.xml 所在目录,并执行上面的 mvn 命令。不要应用 -P 选项指定 Profile 的名称。Maven 将显示被激活的 test Profile 的后果。
mvn test

正文完
 0