Copying Files With Maven – 用Maven复制文件

最后修改: 2021年 5月 14日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

Maven is a build automation tool that allows Java developers to manage a project’s build, reporting, and documentation from a centralized location – the POM (Project Object Model).

Maven是一种构建自动化工具,允许Java开发人员从一个集中的位置–POM(项目对象模型)–管理项目的构建、报告和文档。

When we build a Java project, we often require arbitrary project resources to be copied to a specific location in the output build – we can achieve this with Maven through the use of several different plugins.

当我们构建一个Java项目时,我们经常要求将任意项目资源复制到输出构建的特定位置–我们可以通过使用几个不同的插件在Maven中实现这一目标。

In this tutorial, we’ll build a Java project and copy a specific file to a destination in the build output, using:

在本教程中,我们将构建一个Java项目,并将一个特定的文件复制到构建输出中的一个目的地,使用。

2. Using the Maven Resources Plugin

2.使用Maven资源插件

The maven-resources-plugin handles the copying of project resources to an output directory.

maven-resources-plugin处理将项目资源复制到输出目录中。

Let’s start by adding the plugin to our pom.xml:

让我们先把这个插件添加到我们的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>

Then, let’s create a folder in the project root called source-files. It’ll contain a text file that we want to copy: foo.txt. We’ll then add a configuration element to the maven-resources-plugin to copy this file to target/destination-folder:

然后,让我们在项目根部创建一个文件夹,名为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>

After building the project, we can find the foo.txt in the target/destination-folder.

建立项目后,我们可以在target/destination-folder中找到foo.txt

3. Using the Maven Antrun Plugin

3.使用Maven Antrun插件

The maven-antrun-plugin provides the ability to run Ant tasks from within Maven. We’ll use it here to specify an Ant task that copies a source file to a destination.

maven-antrun-plugin提供了从Maven内部运行Ant任务的能力。在这里我们将用它来指定一个将源文件复制到目的地的蚂蚁任务。

The plugin is defined in the pom.xml as follows:

该插件在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>

We’ll perform the same example as above: copying source-files/foo.txt to target/destination-folder/foo.txt – we’ll achieve this by defining an Ant task to perform the copy:

我们将执行与上面相同的例子:将source-files/foo.txt复制到target/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>

After building the project we’ll find foo.txt in target/destination-folder.

建立项目后,我们会在target/destination-folder中找到foo.txt

4. Using the Copy Rename Maven Plugin

4.使用复制重命名Maven插件

The copy-rename-maven-plugin helps copying files or renaming files/directories during the Maven build lifecycle.

copy-rename-maven-plugin有助于在Maven构建生命周期内复制文件或重命名文件/目录。

The plugin can be installed by adding the following entry to our pom.xml:

该插件可以通过在我们的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>

We’ll now add some config to perform the copy: source-files/foo.txt to target/destination-folder/foo.txt:

我们现在要添加一些配置来执行复制。source-files/foo.txttarget/destination-folder/foo.txt

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

Upon building the project, we’ll see foo.txt in the target/destination-folder.

在构建项目时,我们会看到foo.txttarget/destination-folder中。

5. Conclusion

5.总结

We’ve successfully copied a source file to a destination using three different Maven plugins. Each one operates slightly differently, and while we’ve covered copying a single file here, the plugins are capable of copying multiple files and in some instances, entire directories.

我们使用三个不同的Maven插件成功地将一个源文件复制到了一个目的地。每个插件的操作都略有不同,虽然我们在这里介绍的是复制单个文件,但这些插件能够复制多个文件,在某些情况下还能复制整个目录。

Our other articles plus the official documentation for each of the plugins go into further detail about how to perform more complicated operations.

我们的其他文章和每个插件的官方文档会进一步详细介绍如何执行更复杂的操作。

The source code for these examples can be found over on GitHub.

这些例子的源代码可以在GitHub上找到over