1 Use of Filtering
1.1 Use properties in the project
You can use ${… } to represent variables. Variables can be defined as system properties, in-project properties, filtered resources, and commands.
For example: in the SRC/main/resources/hello. TXT contains the following contents:
Hello ${name}
Copy the code
And the code in the POM.xml file is as follows:
<project>
...
<name>My Resources Plugin Practice Project</name>
...
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
...
</resources>
...
</build>
...
</project>
Copy the code
Execute MVN resources: resources produce the target folder to generate target/classes/hello. TXT file and SRC/main/resources/hello. TXT file has the same content:
Hello ${name}
Copy the code
However, add the
tag to the POM file
tag and set it to true:
. <resource> <directory>src/main/resources</directory> <filtering>true</filtering>
</resource>
...
Copy the code
The target/classes/hello. TXT file changes after MVN resources:resources
hello My Resources Plugin Practice Project
Copy the code
This is because the value defined in the
tag in the POM replaces the name variable in the hello.txt file.
1.2 Using the CLI
MVN resources: resources-dname =”world” MVN resources: resources-dname =”world” target/classes/hello. TXT
hello world
Copy the code
1.2 Using custom Attributes
Taking a step further, you can use not only pre-defined project variables, but also custom variables under the
In the SRC/main/resources/hello. TXT file will be changed to:
Hello ${your.name}
Copy the code
Define a custom variable your.name under the
<project>
...
<properties>
<your.name>world</your.name>
</properties>
...
</project>
Copy the code
1.3 Use of Spring Boot Framework and Filtering
If you inherit the spring-boot-starter-parent POM file, maven-resources-plugins’ Filtering defaults from ${*} to @… @ (i.e. @maven.token@ instead of ${maven.token}) to prevent placeholder collisions with spring. Click here to view the documentation
2 Filter usage
To manage the project, you can write all variables and their corresponding values to a separate file, so you don’t have to rewrite the POM file or set the values for each build. To do this, we can add a filter:
<project>
...
<name>My Resources Plugin Practice Project</name>
...
<build>
...
<filters>
<filter>[a filter property]</filter>
</filters>
...
</build>
...
</project>
Copy the code
For example, create a file named my-filter-values.properties with the following contents:
your.name = world
Copy the code
Add filter to POM:
. <filters> <filter>my-filter-values.properties</filter> </filters> ...Copy the code
Note: Do not filter binary content of files (e.g., images)! The output is corrupted.
It is recommended to use two separate folders for text and binary resource files. The SRC /main/resources folder (default) stores resource files that do not need to be filtered. The SRC /main/resources-filtered folder stores resource files that need to be filtered.
<project>
...
<build>
...
<resources>
<resource>
<directory>src/main/resources-filtered</directory>
<filtering>true</filtering>
</resource>
...
</resources>
...
</build>
...
</project>
Copy the code
Note: As mentioned earlier, filtering binaries such as images, PDF files can cause corrupted output. To prevent such problems, file extensions can be configured not to be filtered.
3 Binary filtering
This plug-in will prevent binary file filtering without adding some exclusion configuration for the following file extensions:
JPG, JPEG, GIF, BMP and PNGCopy the code
If you want to add supplementary file extensions, you can simply do so using the following configuration
<project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> The < version > 3.1.0 < / version > < configuration >... <nonFilteredFileExtensions> <nonFilteredFileExtension>pdf</nonFilteredFileExtension> <nonFilteredFileExtension>swf</nonFilteredFileExtension> </nonFilteredFileExtensions> ... </configuration> </plugin> </plugins> ... </build> ... </project>Copy the code
4 practice
Define variables and values in separate files and replace the values with variable definition symbols in the hello.txt file under the Resiurce folder. File structure:
├─ SRC │ ├─ ├─ download.txt │ ├─ download.txt │ ├─ download.txt │ ├─ download.txtCopy the code
hello.txt
hello ${your.name}
Copy the code
my-filter-values.properties
your.name = nomiracle
Copy the code
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>
Copy the code
Run the MVN clean resources:resources command to generate the target folder directory structure:
Target └─ classes └─ hello.txtCopy the code
The variable in hello. TXT has been replaced:
hello nomiracle
Copy the code