English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Maven 构築設定ファイル

ビルド設定ファイルは、Mavenのビルドデフォルト値を設定またはオーバーライドするための一系列の設定項の値です。

ビルド設定ファイルを使用すると、異なる環境(例えば、生産環境(Production)および開発環境(Development))に対応するビルド方法をカスタマイズできます。

設定ファイルはpom.xmlファイルでactiveProfilesまたはprofiles要素を使用して指定され、さまざまな方法でトリガーされます。設定ファイルはビルド時にPOMを変更し、異なる目標環境(例えば、開発(Development)、テスト(Testing)および生産環境(Production)のデータベースサーバーのアドレスなど)にパラメータを設定するために使用されます。

ビルド設定ファイルのタイプ

ビルド設定ファイルは大体に3つのタイプがあります:

タイプどこで定義されるか
プロジェクトレベル(Per Project)プロジェクトのPOMファイルpom.xmlで定義されています。
ユーザーレベル(Per User)Mavenの設定XMLファイル(%USER_HOME%)で定義されています。/.m2/settings.xml)
グローバル(Global)Mavenのグローバル設定XMLファイル(%M)で定義されています。2_HOME%/conf/settings.xml)

設定ファイルアクティベーション

Mavenのビルド設定ファイルは、さまざまな方法でアクティブ化できます。

  • コマンドプロンプトから明示的にアクティブ化します。

  • Mavenで設定します。

  • 環境変数(ユーザーやシステム変数)に基づいています。

  • オペレーティングシステムの設定(例えば、Windowsシリーズ)。

  • ファイルの存在または欠如。

設定ファイルの有効化例

プロジェクトの構造は以下の通りです:

src/main/resourcesフォルダには、テスト用の3つのファイルがあります:

ファイル名説明
env.properties設定ファイルが指定されていない場合にデフォルトで使用される設定。
env.test.propertiesテスト設定ファイルを使用する際のテスト設定。
env.prod.properties生産設定ファイルを使用する際の生産設定。

注意:これらの3つの設定ファイルは、構築設定ファイルの機能を代表するものではなく、今回のテストのために使用されています;例えば、構築設定ファイルを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> ノードを作成しました。

説明:上で新たに3つの <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:上記で使用されています -環境変数を渡します。evnは先ほど設定した<name>の値に対応し、testは<value>に対応します。

指示 2:Windows 10 システムの環境変数をテストしましたが、効果がありませんでしたので、以下の方法でのみ -D 伝送。

実行結果:

4、オペレーティングシステムアクティベーション設定ファイルを通じて

activation要素が以下のオペレーティングシステム情報を含んでいます。システムがWindows XPの場合、testプロファイルがトリガーされます。

<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 オプションでプロファイルの名前を指定します。Maven はアクティブな test プロファイルの結果を表示します。

mvn test

5、ファイルの存在または欠如によってアクティブ化される設定ファイルを通じて

今、activation要素を使用して以下のオペレーティングシステム情報を含めます。targetが/generated-sources/axistools/wsdl2java/com/companyname/groupが欠けている場合、testプロファイルがトリガーされます。

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

今、コマンドコントロールパネルを開き、pom.xmlがあるディレクトリに移動して、以下のmvnコマンドを実行します。以下の -P オプションでプロファイルの名前を指定します。Maven はアクティブな test プロファイルの結果を表示します。

mvn test

参照:https://www.cnblogs.com/EasonJim/p/6828743.html