profile を指定して、ビルド対象リソースを切り替える
通常のビルド対象リソース
src/main/resources/application.properties
profile 名=develop として develop のビルド対象リソース
src/main/resources-develop/application.properties
と用意されている。
pom.xml に profile を記述する。
<profiles> <profile> <id>develop</id> <build> <resources> <resource> <directory>src/main/resources-develop</directory> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> </build> </profile> </profiles>
これで、src/main/resources にしか存在しないものリソースはそのままビルド対象になり、
src/main/resources-develop で、同じファイル名のリソースが存在すれば、
profile 名=develop を指定したビルドで resources-develop にあるリソースでビルドされる。
以下、-jar で実行するビルドで簡単に試せるであろう。
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.5.2</version> <configuration> <appendAssemblyId>true</appendAssemblyId> <attach>false</attach> <finalName>${artifactId}-${project.version}</finalName> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <appendAssemblyId>false</appendAssemblyId> <archive> <manifest> <mainClass>org.sample.HelloMain</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
作成される JARファイル名は、
<finalName>${artifactId}-${project.version}</finalName>
で、一般的なアーティファクトID - バージョン.jar
を指定しているが、違う名前にしたければ、ここを書きかえれば良い。
profile = develop を指定しないビルド
mvn clean package -DskipTests=true
profile = develop を指定するビルド
-P オプションで、プロファイル名を指定する。
mvn clean package -DskipTests=true -P develop