大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

Maven教程
Maven常用命令應(yīng)用
Maven修改倉庫地址
使用Eclipse創(chuàng)建Maven項(xiàng)目
Maven插件
Maven打包
Maven項(xiàng)目導(dǎo)入及轉(zhuǎn)換

Maven構(gòu)建配置文件

構(gòu)建配置文件是一系列的配置項(xiàng)的值,可以用來設(shè)置或者覆蓋 Maven 構(gòu)建默認(rèn)值。

使用構(gòu)建配置文件,你可以為不同的環(huán)境,比如說生產(chǎn)環(huán)境(Production)和開發(fā)(Development)環(huán)境,定制構(gòu)建方式。

配置文件在 pom.xml 文件中使用 activeProfiles 或者 profiles 元素指定,并且可以通過各種方式觸發(fā)。配置文件在構(gòu)建時(shí)修改 POM,并且用來給參數(shù)設(shè)定不同的目標(biāo)環(huán)境(比如說,開發(fā)(Development)、測(cè)試(Testing)和生產(chǎn)環(huán)境(Production)中數(shù)據(jù)庫服務(wù)器的地址)。

構(gòu)建配置文件的類型

構(gòu)建配置文件大體上有三種類型:

類型

在哪定義

項(xiàng)目級(jí)(Per Project)

定義在項(xiàng)目的POM文件pom.xml中

用戶級(jí) (Per User)

定義在Maven的設(shè)置xml文件中 (%USER_HOME%/.m2/settings.xml)

全局(Global)

定義在 Maven 全局的設(shè)置 xml 文件中 (%M2_HOME%/conf/settings.xml)

配置文件激活

? Maven的構(gòu)建配置文件可以通過多種方式激活。

? 使用命令控制臺(tái)輸入顯式激活。

? 通過 maven 設(shè)置。

? 基于環(huán)境變量(用戶或者系統(tǒng)變量)。

? 操作系統(tǒng)設(shè)置(比如說,Windows系列)。

? 文件的存在或者缺失。

配置文件激活實(shí)例

假定項(xiàng)目結(jié)構(gòu)如下:

其中在src/main/resources文件夾下有三個(gè)用于測(cè)試文件:

文件名

描述

env.properties

如果未指定配置文件時(shí)默認(rèn)使用的配置。

env.test.properties

當(dāng)測(cè)試配置文件使用時(shí)的測(cè)試配置。

env.prod.properties

當(dāng)生產(chǎn)配置文件使用時(shí)的生產(chǎn)配置。

注意:這三個(gè)配置文件并不是代表構(gòu)建配置文件的功能,而是用于本次測(cè)試的目的;比如,我指定了構(gòu)建配置文件為 prod 時(shí),項(xiàng)目就使用 envprod.properties文件。

注意:下面的例子仍然是使用 AntRun 插件,因?yàn)榇瞬寮芙壎?Maven 生命周期階段,并通過 Ant 的標(biāo)簽不用編寫一點(diǎn)代碼即可輸出信息、復(fù)制文件等,經(jīng)此而已。其余的與本次構(gòu)建配置文件無關(guān)。

⒈ 配置文件激活

profile 可以讓我們定義一系列的配置信息,然后指定其激活條件。這樣我們就可以定義多個(gè) profile,然后每個(gè) profile 對(duì)應(yīng)不同的激活條件和配置信息,從而達(dá)到不同環(huán)境使用不同配置信息的效果。

以下實(shí)例,我們將 maven-antrun-plugin:run 目標(biāo)添加到測(cè)試階段中。這樣我們可以在不同的 profile 中輸出文本信息。我們將使用 pom.xml 來定義不同的 profile,并在命令控制臺(tái)中使用 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>

注意:構(gòu)建配置文件采用的是 節(jié)點(diǎn)。

說明:上面新建了三個(gè) ,其中 區(qū)分了不同的 執(zhí)行不同的 AntRun 任務(wù);而 AntRun 的任務(wù)可以這么理解,AntRun 監(jiān)聽 test 的 Maven 生命周期階段,當(dāng) Maven 執(zhí)行 test 時(shí),就除了發(fā) AntRun 的任務(wù),任務(wù)里面為輸出文本并復(fù)制文件到指定的位置;而至于要執(zhí)行哪個(gè) AntRun 任務(wù),此時(shí)構(gòu)建配置文件起到了傳輸指定的作用,比如,通過命令行參數(shù)輸入指定的 。

執(zhí)行命令:

mvn test -Ptest

提示:第一個(gè) test 為 Maven 生命周期階段,第 2 個(gè) test 為構(gòu)建配置文件指定的 參數(shù),這個(gè)參數(shù)通過 -P 來傳輸,當(dāng)然,它可以是 prod 或者 normal 這些由你定義的<id>。

運(yùn)行的結(jié)果如下:

可以看出成功的觸發(fā)了AntRun的任務(wù)。并且是對(duì)應(yīng)構(gòu)建配置文件下的 為 test 的任務(wù)。

再測(cè)試其余兩個(gè)命令,結(jié)果如下:

⒉ 通過Maven設(shè)置激活配置文件

打開 %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>

執(zhí)行命令:

mvn test

提示 1:此時(shí)不需要使用 -Ptest 來輸入?yún)?shù)了,上面的 setting.xml 文件的 已經(jīng)指定了 test 參數(shù)代替了。

提示 2:同樣可以使用在 %M2_HOME%/conf/settings.xml 的文件進(jìn)行配置,效果一致。

執(zhí)行結(jié)果:

⒊ 通過環(huán)境變量激活配置文件

先把上一步測(cè)試的 setting.xml 值全部去掉。

然后在 pom.xml 里面的 <id>  為 test 的 <profile> 節(jié)點(diǎn),加入 <activation> 節(jié)點(diǎn):

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

執(zhí)行命令:

mvn test -Denv=test

提示 1:上面使用 -D 傳遞環(huán)境變量,其中 evn 對(duì)應(yīng)剛才設(shè)置的 <name> 值,test 對(duì)應(yīng)<value>。

提示 2:在 Windows 10 上測(cè)試了系統(tǒng)的環(huán)境變量,但是不生效,所以,只能通過 -D 傳遞。

執(zhí)行結(jié)果:

⒋ 通過操作系統(tǒng)激活配置文件

activation 元素包含下面的操作系統(tǒng)信息。當(dāng)系統(tǒng)為 windows XP 時(shí),test Profile 將會(huì)被觸發(fā)。

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

現(xiàn)在打開命令控制臺(tái),跳轉(zhuǎn)到 pom.xml 所在目錄,并執(zhí)行下面的 mvn 命令。不要使用 -P 選項(xiàng)指定 Profile 的名稱。Maven 將顯示被激活的 test Profile 的結(jié)果。

mvn test

⒌ 通過文件的存在或者缺失激活配置文件

現(xiàn)在使用 activation 元素包含下面的操作系統(tǒng)信息。當(dāng) target/generated-sources/axistools/wsdl2java/com/companyname/group 缺失時(shí),test Profile 將會(huì)被觸發(fā)。

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

現(xiàn)在打開命令控制臺(tái),跳轉(zhuǎn)到 pom.xml 所在目錄,并執(zhí)行下面的 mvn 命令。不要使用 -P 選項(xiàng)指定 Profile 的名稱。Maven 將顯示被激活的 test Profile 的結(jié)果。

mvn test

 

全部教程
主站蜘蛛池模板: 操操操日日 | 欧美曰批人成在线观看 | 国产精品_国产精品_国产精品 | 四虎最新网站 | 欧美一级毛片免费看 | 国内精品伊人久久久久7777人 | 一级女性全黄生活片免费看 | 国产高清ujzzujzz | 天海翼一区 在线播放 | 亚洲另类图 | 精品一区二区三区免费观看 | 久草久草在线 | 爱爱网站在线观看免费 | 久久精品国产99久久久 | 亚洲国产大片 | 亚洲精品久久久久综合91 | 久久久国产精品免费看 | 欧美亚洲国产精品久久久久 | 国产精品怡红院永久免费 | 天天撸天天操 | 国产色综合久久无码有码 | 国产欧美另类久久久精品免费 | 99国产大尺度福利视频 | 国产精品乱码高清在线观看 | 亚洲欧美国产一区二区三区 | 四虎精品成人a在线观看 | 国产欧美一区二区三区在线看 | 日韩一区二区三区视频 | 日本老年人精品久久中文字幕 | 国产精品久久久久久久 | 午夜久久久精品 | 99久久精品国内 | 色花堂国产精品第二页 | 国产在线观看99 | 成人观看视频 | 日本色色网 | 特黄特黄aaaa级毛片免费看 | 日日夜夜人人 | 欧美一区二区三区香蕉视 | 奇米第四色视频 | 天天做夜夜做 |