1. Introduction
Before this, we must first clarify the concept of filtering.
definition
filtering is a feature provided in Maven's resource plugin.
Decide whether to replace the tokens (that is, variables identified by @val@) in the files in the resources directory.
There are two main sources of parameters here, one is from the properties attribute in the pom file, and the other is from the external .properties file (I personally believe that this file cannot be confused with the configuration file in springboot).
Problems solved
I want to solve the problem of variable injection of configuration files during encoding, such as: different configuration properties need to be enabled in different development environments, how to flexibly switch? filtering provides us with a method.
usage
First, start with the above example. After that, you can define the properties in the pom file and use the configuration file in the resources directory. If used, please see the details below.
2. Instructions for use
1. Introduce variable precompilation configuration of pom and resource resource files
The sample code is as follows, which is to enable filtering filtering in the resources directory, without adding filtering, and the default value is false.
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build>
After a deeper understanding of this filtering, the detailed explanation is as follows.
1.1 pom file Read variables inside pom file
The application of variables within the pom file is through valuename.
Common usage methods are as follows
- {value_name}
- valuename
- {}
1.2 File reading Variable methods inside the file
References to internal variables are usually used in the way of using ${value_name}, such as the common one: ${}
This leads to a conflict, and both configuration files use the same set of identifiers. How can the yaml file introduce variables in the pom, so this property is available
1.3 How to read the variables of the yaml file in pom file
If you look at the property bar in spring-boot-starter-parent, you can see that it says:
<>@</>
If you want to reference the attributes in pom in yaml, just use the @ identifier directly, and the usage is @value_of_pom@.
That's how the third variable reference appears:
- When referring to variables in pom in the yaml file of resources, reference is made through @value_of_pom@ (provided that filtering of the resource is enabled).
1.4 Pom file read variables in .properties file
In the Maven project, you can use elements to define variables and use them to specify the location of the resource file. If you want to read variables in .properties files in, you can use Maven's filtering function.
Here is a simple example:
Create a .properties file in the src/main/resources directory, for example. src/main/resources/
=jdbc:mysql://localhost/mydb =myuser =mypassword
Configure resource filtering and elements in.
<project> ... <properties> <>${}</> <>${}</> <>${}</> </properties> <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> ... </project>
2. Turn on resource packaging resource file filtering
Filtering of packaged resource files through include and excludes tags
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include></include> </includes> </resource> <resource> <directory>src/main/resources</directory> <!--filteringThe default value of the tag isfalse,Can write or not here--> <filtering>false</filtering> <excludes> <exclude></exclude> </excludes> </resource> </resources> </build>
3. Maven command parameters
Mainly used to switch environments:
//This means that the file variable in the pom is assigned as my-setting-file. If this is the file name of the set filter, the setting parameters in my-setting-file will be introduced.mvn resources:resources -Dfile=my-setting-file //This means that test and jdk8 two profiles are enabled, and os-windows are not activated.mvn clean package -Ptest,jdk8,!os-windows
This is the end of this article about how to use filtering under Maven recovery. For more related content on the function of Maven recovery filtering, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!