乐闻世界logo
搜索文章和话题

What is Maven resource filtering? How to use resource filtering to manage multi-environment configurations?

2月18日 21:29

Maven Resource Filtering is a mechanism provided by Maven that allows dynamic replacement of variables in resource files during the build process. This feature is very useful for managing configurations in different environments.

Basic Principle of Resource Filtering: During the build process, Maven scans resource files (such as properties, xml, yml files) and replaces variables in the form of ${variable} with property values defined in the POM file.

Configuring Resource Filtering:

  1. Enable resource filtering in POM:
xml
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </resource> </resources> </build>
  1. Define property variables:
xml
<properties> <database.url>jdbc:mysql://localhost:3306/mydb</database.url> <database.username>root</database.username> <database.password>password</database.password> <app.version>1.0.0</app.version> </properties>
  1. Use variables in resource files:
properties
# application.properties database.url=${database.url} database.username=${database.username} database.password=${database.password} app.version=${app.version}

Using Resource Filtering with Profile:

xml
<profiles> <profile> <id>dev</id> <properties> <env>dev</env> <database.url>jdbc:mysql://dev-db:3306/mydb</database.url> </properties> </profile> <profile> <id>prod</id> <properties> <env>prod</env> <database.url>jdbc:mysql://prod-db:3306/mydb</database.url> </properties> </profile> </profiles>

Advanced Usage of Resource Filtering:

  1. Multiple resource directory configuration:
xml
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> <resource> <directory>src/main/static</directory> <filtering>false</filtering> </resource> </resources> </build>
  1. Using external property files:
xml
<build> <filters> <filter>src/main/filters/${env}.properties</filter> </filters> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build>
  1. Test resource filtering:
xml
<build> <testResources> <testResource> <directory>src/test/resources</directory> <filtering>true</filtering> </testResource> </testResources> </build>

Notes:

  • Resource filtering increases build time, only enable filtering for necessary files
  • Avoid enabling filtering on binary files (such as images, jar packages)
  • Use Profile to distinguish configurations for different environments
  • Sensitive information (such as passwords) should be stored using environment variables or encryption
  • Use -D parameters to pass dynamic properties in CI/CD processes

Best Practices:

  • Use @variable@ or ${variable} format to avoid conflicts with placeholders in frameworks like Spring
  • Uniformly manage resource filtering configuration in parent POM
  • Use Maven's default delimiter ${}, or customize delimiters
  • Regularly check filtered files to ensure variable replacement is correct

Resource filtering is an important means for Maven to manage multi-environment configurations and can significantly simplify configuration management work.

标签:Maven