1. 序章

Maven は、Java開発者がプロジェクトのビルド、レポート、およびドキュメントを一元化された場所であるPOM(プロジェクトオブジェクトモデル)から管理できるようにするビルド自動化ツールです。

Javaプロジェクトをビルドするとき、出力ビルドの特定の場所に任意のプロジェクトリソースをコピーする必要があることがよくあります。これは、いくつかの異なるプラグインを使用してMavenで実現できます。

このチュートリアルでは、Javaプロジェクトをビルドし、以下を使用して、ビルド出力の宛先に特定のファイルをコピーします。

2. Mavenリソースプラグインの使用

maven-resources-plugin は、プロジェクトリソースの出力ディレクトリへのコピーを処理します。

プラグインをpom.xmlに追加することから始めましょう。

<project>
    ...
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.2.0</version>
                </plugin>
                ...
            </plugins>
        </pluginManagement>
        <!-- To use the plugin goals in your POM or parent POM -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.2.0</version>
            </plugin>
            ...
        </plugins>
    </build>
    ...
</project>

次に、プロジェクトルートに source-files。というフォルダーを作成します。このフォルダーには、コピーするテキストファイルfoo.txtが含まれています。 次に、構成要素を maven-resources-plugin に追加して、このファイルを target /destination-folderにコピーします。

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.2</version>
    <executions>
        <execution>
            <id>copy-resource-one</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}/target/destination-folder</outputDirectory>
                <resources>
                    <resource>
                        <directory>source-files</directory>
                        <includes>
                            <include>foo.txt</include>
                        </includes>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

プロジェクトをビルドした後、[X48X] target /destination-folderにfoo.txtがあります。

3. MavenAntrunプラグインの使用

maven-antrun-plugin は、Maven内からAntタスクを実行する機能を提供します。 ここでは、これを使用して、ソースファイルを宛先にコピーするAntタスクを指定します。

プラグインは、pom.xmlで次のように定義されています。

<project>
    [...]
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <phase>
                            <phase>generate-sources</phase>
                        </phase>
                        <configuration>
                            <target>
                                <!-- Place any Ant task here. -->
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

上記と同じ例を実行します。source-files/foo.txttarget/ destination-folder / foo.txt にコピーします–これはAntを定義することで実現しますコピーを実行するタスク:

<configuration>
    <target>
        <mkdir dir="${basedir}/target/destination-folder" />
        <copy todir="${basedir}/target/destination-folder">
            <fileset dir="${basedir}/source-files" includes="foo.txt" />
        </copy>
    </target>
</configuration>

プロジェクトをビルドすると、 target /destination-folderfoo.txtが見つかります。

4. CopyRenameMavenプラグインの使用

copy-rename-maven-plugin は、Mavenビルドライフサイクル中にファイルをコピーしたり、ファイル/ディレクトリの名前を変更したりするのに役立ちます。

プラグインは、pom.xmlに次のエントリを追加することでインストールできます。

<project>
    ...
    <build>
        <plugins>
            <plugin>
                <groupId>com.coderplus.maven.plugins</groupId>
                <artifactId>copy-rename-maven-plugin</artifactId>
                <version>1.0</version>
                <executions>
                    <execution>
                        <id>copy-file</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <!-- Place config here -->
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

次に、コピーを実行するための構成を追加します。 source-files /foo.txtからtarget/ destination-folder / foo.txt

<configuration>
    <sourceFile>source-files/foo.txt</sourceFile>
    <destinationFile>target/destination-folder/foo.txt</destinationFile>
</configuration>

プロジェクトをビルドすると、 target /destination-folderfoo.txtが表示されます。

5. 結論

3つの異なるMavenプラグインを使用して、ソースファイルを宛先に正常にコピーしました。 それぞれの動作は少し異なります。ここでは単一のファイルのコピーについて説明しましたが、プラグインは複数のファイル、場合によってはディレクトリ全体をコピーできます。

他の記事と各プラグインの公式ドキュメントでは、より複雑な操作を実行する方法についてさらに詳しく説明しています。

これらの例のソースコードは、GitHubにあります。