2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Maven antrunプラグインを利用して、テスト時にリソースファイルをコピーする。

Posted at

概略

テスト実行前にmyproject.propertiesファイルをmyproject.ja_JP.propertiesへコピーします。

なんでこんなことしているのか?

ユニットテスト時に、どうしても各国語リソースが欲しい。

とりあえず元ファイルをコピーしてしのぐ。

元ファイルを変更すると、それを各国語へ反映しなくちゃいけない。

面倒すぎる。 :disappointed:

テスト時に元ファイルをコピーすればいいんじゃね? < イマココ!

antrunプラグインを使って、ファイルをコピーする

resourcesプラグインには特定ファイルを複製する機能がないのです。 :disappointed_relieved:

antrunプラグインをresourcesプラグインのライフサイクルフェイズであるprocess-test-resourcesフェイズで実行しています。

テスト時のリソースコピー先は${project.build.testOutputDirectory}に入っているので、それをantのプロパティとして使えるようにしています。

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">
    <!-- (略)-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <phase>process-test-resources</phase>
                        <configuration>
                            <target>
                                <property name="output_dir" value="${project.build.testOutputDirectory}"/>
                                <copy file="${output_dir}/myproject.properties"
                                      tofile="${output_dir}/myproject.ja_JP.properties" />
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

これで、test-compileフェイズ実行時にリソースファイルがコピーされるようになりました。 :smile:

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?