この記事では、さまざまな環境(dev、test、prod)に対して異なるパラメータ(サーバまたはデータベースパラメータ)を渡すMavenのプロファイルの例をいくつか紹介します。
P.S Maven 3.5.3
でテスト済み
1.基本Mavenプロファイル
1.1ユニットテストをスキップする簡単なプロファイル。
pom.xml
<!-- skip unit test --> <profile> <id>xtest</id> <properties> <maven.test.skip>true</maven.test.skip> </properties> </profile>
1.2プロファイルを有効にするには、 `-P`オプションを追加します。
ターミナル
# Activate xtest profile to skip unit test and package the project $ mvn package -Pxtest
1.3複数のプロファイルを有効にするには:
ターミナル
$ mvn package -P xtest, another-profile-id # multi modules, same syntax $ mvn -pl module-name package -P xtest, another-profile-id
1.4 `maven-help-plugin`を追加してコンパイルやパッケージ段階でアクティブなプロファイルを表示すると、多くのデバッグ時間が節約されます。
pom.xml
<build> <plugins> <!-- display active profile in compile phase --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-help-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <id>show-profiles</id> <phase>compile</phase> <goals> <goal>active-profiles</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
次回は、現在アクティブなプロファイルがコンパイルフェーズで表示されます。
ターミナル
$ mvn compile -P xtest [INFO]--- maven-help-plugin:3.1.0:active-profiles (show-profiles) @ example1 ---[INFO]Active Profiles for Project 'com.mkyong:example1:jar:1.0': The following profiles are active: - xtest (source: com.mkyong:example1:1.0)
2. Mavenプロファイル – 例1
さまざまなプロパティ値を開発環境や運用環境に渡すためのMavenプロファイルの例
2.1プロパティファイル。
resources/db.properties
db.driverClassName=${db.driverClassName} db.url=${db.url} db.username=${db.username} db.password=${db.password}
2.2フィルタリングを有効にする。 Mavenは
resources/db.properties`の
$ {} `をアクティブなMavenプロファイルのプロパティでマップします。
pom.xml
<!-- map ${} variable --> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources>
2.3異なるプロパティ値を持つ2つのプロファイルID(devとprod)を作成します。
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>maven-profiles</artifactId> <groupId>com.mkyong</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>example1</artifactId> <profiles> <profile> <id>dev</id> <activation> <!-- this profile is active by default --> <activeByDefault>true</activeByDefault> <!-- activate if system properties 'env=dev' --> <property> <name>env</name> <value>dev</value> </property> </activation> <properties> <db.driverClassName>com.mysql.jdbc.Driver</db.driverClassName> <db.url>jdbc:mysql://localhost:3306/dev</db.url> <db.username>mkyong</db.username> <db.password>8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92</db.password> </properties> </profile> <profile> <id>prod</id> <activation> <!-- activate if system properties 'env=prod' --> <property> <name>env</name> <value>prod</value> </property> </activation> <properties> <db.driverClassName>com.mysql.jdbc.Driver</db.driverClassName> <db.url>jdbc:mysql://live01:3306/prod</db.url> <db.username>mkyong</db.username> <db.password>8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92</db.password> </properties> </profile> </profiles> <build> <!-- map ${} variable --> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.mkyong.example1.App1</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
2.4プロパティファイルをロードして印刷します。
App1.java
package com.mkyong.example1; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class App1 { public static void main(String[]args) { App1 app = new App1(); Properties prop = app.loadPropertiesFile("db.properties"); prop.forEach((k, v) -> System.out.println(k + ":" + v)); } public Properties loadPropertiesFile(String filePath) { Properties prop = new Properties(); try (InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(filePath)) { prop.load(resourceAsStream); } catch (IOException e) { System.err.println("Unable to load properties file : " + filePath); } return prop; } }
2.5テストする。
ターミナル
# default profile id is 'dev' $ mvn package $ java -jar target/example1-1.0.jar db.password:8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 db.driverClassName:com.mysql.jdbc.Driver db.username:mkyong db.url:jdbc:mysql://localhost:3306/dev # enable profile id 'prod' with -P prod or -D env=prod $ mvn package -P prod $ mvn package -D env=prod $ java -jar target/example1-1.0.jar db.password:8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 db.driverClassName:com.mysql.jdbc.Driver db.username:mkyong db.url:jdbc:mysql://live01:3306/prod
3. Mavenプロファイル – 例2
このMavenプロファイルの例は、すべてをプロパティファイルに入れます。
3.1プロパティファイル。後でMavenは値をプロファイルIDにマップします。
resources/config.properties
# Database Config db.driverClassName=${db.driverClassName} db.url=${db.url} db.username=${db.username} db.password=${db.password} # Email Server email.server=${email.server} # Log Files log.file.location=${log.file.location}
3.2開発環境、テスト環境、本番環境向けに異なるプロパティファイルを作成する。
resources/env/config.dev.properties
# Database Config db.driverClassName=com.mysql.jdbc.Driver db.url=jdbc:mysql://localhost:3306/dev db.username=mkyong db.password=8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 # Email Server email.server=email-dev:8888 # Log Files log.file.location=dev/file.log
resources/env/config.test.properties
# Database Config db.driverClassName=com.mysql.jdbc.Driver db.url=jdbc:mysql://test01:3306/test db.username=mkyong db.password=8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 # Email Server email.server=email-test:8888 # Log Files log.file.location=test/file.log
resources/env/config.prod.properties
# Database Config db.driverClassName=com.mysql.jdbc.Driver db.url=jdbc:mysql://live01:3306/prod db.username=mkyong db.password=8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 # Email Server email.server=email-prod:25 # Log Files log.file.location=prod/file.log
3.3フィルタリングを有効にする。これが鍵です!
-
注** このhttps://maven.apache.org/plugins/maven-resources-plugin/examples/filter.htmlを参照してください[除外フィルタ]
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>maven-profiles</artifactId> <groupId>com.mkyong</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>example2</artifactId> <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <env>dev</env> </properties> </profile> <profile> <id>prod</id> <properties> <env>prod</env> </properties> </profile> <profile> <id>test</id> <properties> <env>test</env> </properties> </profile> </profiles> <build> <!-- Loading all ${} --> <filters> <filter>src/main/resources/env/config.${env}.properties</filter> </filters> <!-- Map ${} into resources --> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>** .properties</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.mkyong.example2.App2</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
3.4プロパティファイルをロードして印刷します。
App1.java
package com.mkyong.example2; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class App2 { public static void main(String[]args) { App2 app = new App2(); Properties prop = app.loadPropertiesFile("config.properties"); prop.forEach((k, v) -> System.out.println(k + ":" + v)); } public Properties loadPropertiesFile(String filePath) { Properties prop = new Properties(); try (InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(filePath)) { prop.load(resourceAsStream); } catch (IOException e) { System.err.println("Unable to load properties file : " + filePath); } return prop; } }
3.5テストします。
ターミナル
# profile id dev (default) $ mvn package $ java -jar target/example2-1.0.jar log.file.location:dev/file.log db.password:8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 db.driverClassName:com.mysql.jdbc.Driver db.username:mkyong email.server:email-dev:8888 db.url:jdbc:mysql://localhost:3306/dev # profile id prod $ mvn package -P prod $ java -jar target/example2-1.0.jar log.file.location:prod/file.log db.password:8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 db.driverClassName:com.mysql.jdbc.Driver db.username:mkyong email.server:email-prod:25 db.url:jdbc:mysql://live01:3306/prod # profile id test $ mvn package -P test $ java -jar target/example2-1.0.jar log.file.location:test/file.log db.password:8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 db.driverClassName:com.mysql.jdbc.Driver db.username:mkyong email.server:email-test:8888 db.url:jdbc:mysql://test01:3306/test
最後に、あなたのユースケースを教えてください:)
ソースコードをダウンロードする
$ git clone
https://github.com/mkyong/maven-examples.git
$ cd maven-profiles
#プロファイル ‘prod’を使用してサンプル1をテストします。$ mvn -pl example1 package -Pprod $ java -jar example1/target/example1-1.0.jar
#例2をプロファイル ‘test’でテストします。$ mvn -pl example2 package -Ptest $ java -jar example2/target/example2-1.0.jar
参考文献
プロファイルを作成する]。
https://blog.tallan.com/2010/09/16/maven-best-practices/
[Maven Best
Maven 2を使用するさまざまな環境]。
http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
[Maven
フィルタリング]