maven中filter的使用方法

7,509 阅读3分钟

1 Filtering 的使用

1.1 使用项目中属性

在资源文件中可以使用${...}来表示变量。变量的定义可以为系统属性,项目中属性,筛选的资源以及命令。

例如: 在 src/main/resources/hello.txt 中包含以下内容:

Hello ${name}

并且pom.xml文件中代码如下:

<project>
  ...
  <name>My Resources Plugin Practice Project</name>
  ...
  <build>
    ...
    <resources>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
      ...
    </resources>
    ...
  </build>
  ...
</project>

执行 mvn resources:resources 产生的 target 文件夹中 生成的 target/classes/hello.txt 文件和src/main/resources/hello.txt 文件有着一样的内容:

Hello ${name}

然而,在pom 文件<resource>标签下加入<filtering>标签 并且 设置为 true

...
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
...

执行 mvn resources:resources 命令后,target/classes/hello.txt 文件内容发生了变化:

hello My Resources Plugin Practice Project

这是因为定义在 pom 中的 <name> 标签中的值替换了 hello.txt文件中 name 变量。

1.2 使用命令行

此外,也可以使用“-D”后面加上声明的命令行的方式来指定内容,例如: mvn resources:resources -Dname="world" 命令执行后,target/classes/hello.txt 文件内容为:

hello world

1.2 使用自定义属性

更近一步,不仅可以使用预先定义的项目变量,也可以使用在<properties>标签下自定义变量。例如:

src/main/resources/hello.txt 文件中将文件内容更改为:

Hello ${your.name}

在pom中<properties>标签下定义自定义变量 your.name

<project>
  ...
  <properties>
    <your.name>world</your.name>
  </properties>
  ...
</project>

1.3 spring boot框架 与 Filtering 的使用

如果在 pom 文件中继承了 spring-boot-starter-parent pom文件,那么maven-resources-plugins的 Filtering 默认的过滤符号就从 ${*} 改为 @...@ (i.e. @maven.token@ instead of ${maven.token})来防止与 spring 中的占位符冲突。点击 这里 查看文档

2 filter 的使用

为了管理项目,可以将所有变量和 其对应的值 写入一个独立的文件,这样就不需要重写pom文件或者每次构建都设置值。为此可以增加一个 filter

<project>
  ...
  <name>My Resources Plugin Practice Project</name>
  ...
  <build>
    ...
    <filters>
      <filter>[a filter property]</filter>
    </filters>
    ...
  </build>
  ...
</project>

例如: 新建文件 my-filter-values.properties 内容为:

your.name = world

在pom中增加filter:

...
    <filters>
      <filter>my-filter-values.properties</filter>
    </filters>
    ...

注:不要过滤二进制内容的文件(如:图像)!会导致输出损坏。

有文本和二进制资源文件情况下,推荐使用两个独立的文件夹。文件夹 src/main/resources (默认)中存放不需要过滤的资源文件,src/main/resources-filtered文件夹存放需要过滤的资源文件。

<project>
  ...
  <build>
    ...
    <resources>
      <resource>
        <directory>src/main/resources-filtered</directory>
        <filtering>true</filtering>
      </resource>
      ...
    </resources>
    ...
  </build>
  ...
</project>

注:正如前面所提到的,过滤图像、pdf等二进制文件可能导致损坏的输出。为了防止这样的问题,可以配置文件扩展名不会被过滤。

3 Binary filtering

该插件将阻止二进制文件过滤,而无需为以下文件扩展名添加一些排除配置:

jpg, jpeg, gif, bmp 以及 png

如果想要添加补充文件扩展名,可以使用以下配置简单地实现

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          ...
          <nonFilteredFileExtensions>
            <nonFilteredFileExtension>pdf</nonFilteredFileExtension>
            <nonFilteredFileExtension>swf</nonFilteredFileExtension>
          </nonFilteredFileExtensions>
          ...
        </configuration>
      </plugin>
    </plugins>
    ...
  </build>
  ...
</project>

4 练习

在单独文件中定义变量以及值,并且将值对 resiurce文件夹下hello.txt文件中的变量定义符号进行替换。 文件结构:

├─ src
│    └─ main
│           ├─ resources
│           │    └─ hello.txt
│           └─ resources-filtered
│                  └─ my-filter-values.properties
└─ pom.xml

hello.txt

hello ${your.name}

my-filter-values.properties

your.name = nomiracle

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project ...>
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>test</artifactId>
    <version>1.0</version>

    <name>My Resources Plugin Practice Project</name>

    <build>
        <filters>
            <filter>src/main/resources-filtered/my-filter-values.properties</filter>
        </filters>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

</project>

执行 mvn clean resources:resources 命令,生成的 target 文件夹目录结构为:

target
└─ classes
       └─ hello.txt

其中 hello.txt 文件中变量已经被替换:

hello nomiracle