Maven Resources Plugin – Maven资源插件

最后修改: 2018年 4月 19日

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

1. Overview

1.概述

This tutorial describes the resources plugin, one of the core plugins of the Maven build tool.

本教程介绍了resources插件,这是Maven构建工具的核心插件之一。

For an overview of the other core plugins, refer to this article.

关于其他核心插件的概述,请参考这篇文章

2. Plugin Goals

2.插件目标

The resources plugin copies files from input resource directories to an output directory. This plugin has three goals, which are different only in how the resources and output directories are specified.

resources插件将文件从输入资源目录复制到输出目录。该插件有三个目标,它们的区别仅在于如何指定资源和输出目录。

The three goals of this plugin are:

这个插件的三个目标是。

  • resources – copy resources that are part of the main source code to the main output directory
  • testResources copy resources that are part of the test source code to the test output directory
  • copy-resources copy arbitrary resource files to an output directory, requiring us to specify the input files and the output directory

Let’s take a look at the resources plugin in the pom.xml:

让我们看看resources插件中的pom.xml

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.2</version>
    <configuration>
        ...
    </configuration>
</plugin>

We can find the latest version of this plugin here.

我们可以找到这个插件的最新版本这里

3. Example

3.实例

Assume we want to copy resource files from the directory input-resources to the directory output-resources and we want to exclude all files ending with the extension .png.

假设我们想把资源文件从input-resources目录复制到output-resources目录,并且我们想排除所有以.png结尾的文件。

These requirements are satisfied with this configuration:

这种配置满足了这些要求。

<configuration>
    <outputDirectory>output-resources</outputDirectory>
    <resources>
        <resource>
            <directory>input-resources</directory>
            <excludes>
                <exclude>*.png</exclude>
            </excludes>
            <filtering>true</filtering>
        </resource>
    </resources>
</configuration>

The configuration applies to all executions of the resources plugin.

该配置适用于resources插件的所有执行。

For example, when the resources goal of this plugin is executed with the command mvn resources:resources, all resources from the input-resources directory, except for PNG files, will be copied to output-resources.

例如,当这个插件的resources目标用mvn resources:resources命令执行时,input-resources目录下的所有资源,除了PNG文件,将被复制到output-resources

Since, by default, the resources goal is bound to the process-resources phase in the Maven default lifecycle, we can execute this goal and all the preceding phases by running the command mvn process-resources.

由于默认情况下,resources目标与Maven default生命周期中的process-resources阶段绑定,我们可以通过运行mvn process-resources命令来执行该目标和之前的所有阶段。

In the given configuration, there’s a parameter named filtering with the value of true. The filtering parameter is used to replace placeholder variables in the resource files.

在给定的配置中,有一个名为filtering的参数,值为truefiltering参数被用来替换资源文件中的占位变量

For instance, if we have a property in the POM:

例如,如果我们在POM中有一个属性。

<properties>
    <resources.name>Baeldung</resources.name>
</properties>

and one of the resource files contains:

和其中一个资源文件包含。

Welcome to ${resources.name}!

then the variable will be evaluated in the output resource, and the resulting file will contain:

则该变量将在输出资源中被评估,结果文件将包含。

Welcome to Baeldung!

4. Conclusion

4.结论

In this quick article, we went over the resources plugin and gave instructions on using and customizing it.

在这篇快速文章中,我们介绍了resources插件,并给出了使用和定制它的说明。

The complete source code for this tutorial can be found over on GitHub.

本教程的完整源代码可以在GitHub上找到over

Next »

Maven Compiler Plugin