在资源文件中能够使用${...}
来表示变量。变量的定义能够为系统属性,项目中属性,筛选的资源以及命令。html
例如: 在 src/main/resources/hello.txt
中包含如下内容:spring
Hello ${name}
复制代码
而且pom.xml文件中代码以下:apache
<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
文件有着同样的内容:bash
Hello ${name}
复制代码
然而,在pom 文件<resource>
标签下加入<filtering>
标签 而且 设置为 true
:框架
...
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
...
复制代码
执行 mvn resources:resources
命令后,target/classes/hello.txt
文件内容发生了变化:maven
hello My Resources Plugin Practice Project
复制代码
这是由于定义在 pom 中的 <name>
标签中的值替换了 hello.txt
文件中 name
变量。spring-boot
此外,也能够使用“-D”
后面加上声明的命令行的方式来指定内容,例如: mvn resources:resources -Dname="world"
命令执行后,target/classes/hello.txt
文件内容为:ui
hello world
复制代码
更近一步,不只能够使用预先定义的项目变量,也能够使用在<properties>
标签下自定义变量。例如:spa
在 src/main/resources/hello.txt
文件中将文件内容更改成:插件
Hello ${your.name}
复制代码
在pom中<properties>
标签下定义自定义变量 your.name
:
<project>
...
<properties>
<your.name>world</your.name>
</properties>
...
</project>
复制代码
若是在 pom 文件中继承了 spring-boot-starter-parent
pom文件,那么maven-resources-plugins的 Filtering 默认的过滤符号就从 ${*}
改成 @...@
(i.e. @maven.token@ instead of ${maven.token})来防止与 spring 中的占位符冲突。点击 这里 查看文档
为了管理项目,能够将全部变量和 其对应的值 写入一个独立的文件,这样就不须要重写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等二进制文件可能致使损坏的输出。为了防止这样的问题,能够配置文件扩展名不会被过滤。
该插件将阻止二进制文件过滤,而无需为如下文件扩展名添加一些排除配置:
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>
复制代码
在单独文件中定义变量以及值,而且将值对 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
复制代码