SoFunction
Updated on 2025-05-23

Detailed explanation of resource filtering and attribute management in Maven Profile

Introduction: Engineering Challenges to Building Environmental Differentiation

Under the continuous delivery system, the software needs to be differentiated for multiple environments such as development, testing, pre-release, and production. The traditional manual configuration file replacement method is not only inefficient, but also has serious hidden dangers such as configuration omissions and version pollution.MavenAs the core building tool of the Java ecosystem, its Profile mechanism cooperates with resource filtering (Resource Filtering) function provides an elegant solution for multi-environment construction.

This article explains in-depth how to pass<resources>and<filters>The linkage of the environment is achieved, analyzes the priority rules for attribute coverage, explains the internal mechanism of dynamic placeholder replacement, and discusses sensitive information encryption andProfiledeep integration solution. ByApache Maven 3.8.6The interpretation of the core source code reveals the implementation principle of "one-one construction, multiple deployments" behind it, helping developers build a robust continuous delivery pipeline.

Chapter 1: Engineering Practice of Multi-Environment Resource Allocation

1.1 Standardized layout of resource catalogs

The resource management of a typical Maven project follows the principle of conventions over configuration:

src/
  main/
    resources/
      env/
        dev/
          
        test/
          
        prod/
          
    filters/
      
       
      

Implement environment switching by activating different profiles:

<profiles>
  <profile>
    <id>dev</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <build>
      <resources>
        <resource>
          <directory>src/main/resources/env/dev</directory>
          <filtering>true</filtering>
        </resource>
      </resources>
      <filters>
        <filter>src/main/filters/</filter>
      </filters>
    </build>
  </profile>
</profiles>

1.2 Two-way binding of filtering mechanism

The complete life cycle of resource filtering consists of three stages:

  1. Resource collection phase: Determine the pending resource directory based on the activated Profile
  2. Attribute injection stage: Load key-value pairs in filter file into memory context
  3. Placeholder replacement phase: Scan the ${…} expression in the resource file for value replacement

Key configuration parameters analysis:

<resource>
  <directory>${basedir}/target/config</directory>
  <includes>
    <include>*.xml</include>
  </includes>
  <excludes>
    <exclude>*.jks</exclude>
  </excludes>
  <filtering>true</filtering>
</resource>

1.3 Advanced mode for multi-environment configuration

For large distributed systems, a hierarchical filtering strategy is recommended:

Basic layer: Common configuration shared by all environments

# 
=1.0.0
=INFO

Environmental layer: Environment-specific configuration

# 
=jdbc:mysql://dev-db:3306/app

Confidential layer: Sensitive information injected through external

# 
=${ENV_DB_PASSWORD}

Configuring merge through filter chain:

<filters>
  <filter>src/main/filters/</filter>
  <filter>src/main/filters/${env}.properties</filter>
  <filter>${}/</filter>
</filters>

Chapter 2: Priority System for Attribute Coverage

2.1 Loading order of Maven attribute sources

Attribute parsing follows the following priority (from high to low):

  1. Command line -D parameters
  2. Activate properties in Profile
  3. In-house
  4. Properties in
  5. System environment variables
  6. Project default properties

The final effective attribute value can be verified through mvn help:effective-pom.

2.2 Dynamic injection of Profile attributes

Profile attribute definition supports multiple data sources:

&lt;profile&gt;
  &lt;id&gt;ci&lt;/id&gt;
  &lt;properties&gt;
    &lt;!-- Static value --&gt;
    &lt;&gt;/repo&lt;/&gt;
    
    &lt;!-- Environment variable injection --&gt;
    &lt;&gt;${env.BUILD_NUMBER}&lt;/&gt;
    
    &lt;!-- Conditional expression --&gt;
    &lt;&gt;${("1.8") ? "-XX:PermSize=256m" : ""}&lt;/&gt;
  &lt;/properties&gt;
&lt;/profile&gt;

2.3 Arbitration mechanism for attribute conflicts

When multiple Profiles are activated simultaneously, the order of attribute loading is determined by the order of the Profile declaration. It is recommended to use conditions to automatically activate to avoid uncontrollable overwriting behavior.

Conflict resolution example:

<!-- profile A -->
<profile>
  <id>A</id>
  <properties>
    <key>valueA</key>
  </properties>
</profile>

<!-- profile B -->
<profile>
  <id>B</id>
  <properties>
    <key>valueB</key>
  </properties>
</profile>

Command line executionmvn -PA,BWhen the properties of Profile B declared last override Profile A.

Chapter 3: The engine principle of dynamic replacement

3.1 Workflow for placeholder parser

The core of Maven resource filtering ismaven-resources-pluginPlugin, its replacement process includes:

  1. Mark scan: Use regular expressions\$\{([^}]+)\}Match placeholders
  2. Key value search: Search for attribute values ​​in the following contexts:
    • Maven Project Properties
    • System Properties
    • Filter file properties
    • Environment variables
  3. Recursive parsing: Support nested expressions${outer.${}}
  4. Type conversion: Automatically handle special characters escape rules

3.2 Analysis example of multi-level placeholder

Consider the following configuration file:

# 
=${db.${env}.url}

Together with filter files:

# 
env=dev
=jdbc:mysql://dev-db:3306/app

The analysis process is carried out in two steps:

  • First analysis obtained=${}
  • Secondary parsing is replaced with actual JDBC URL

3.3 Custom delimiter configuration

For expressions containing special characters, the delimiter can be modified through plug-in configuration:

<plugin>
  <groupId></groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <configuration>
    <useDefaultDelimiters>false</useDefaultDelimiters>
    <delimiters>
      <delimiter>@</delimiter>
    </delimiters>
    <escapeString>\</escapeString>
  </configuration>
</plugin>

At this time the placeholder format becomes@key@, avoid conflicts with Spring Boot's configuration syntax.

Chapter 4: Secure processing of sensitive information

4.1 Common solutions for configuring encryption

plan advantage shortcoming
Jasypt encryption Seamless integration with Spring Encryption keys need to be managed
Vault dynamic injection The key does not fall Rely on Vault service availability
Environment variable injection Simple and fast Unable to version control configuration
Key Separation Storage Comply with security audit requirements Increase deployment complexity

4.2 Jasypt-based integration examples

  1. Define encryption values ​​in filter file:
# 
=ENC(GTWEbqXd6PDsRrQZYgZfVQ==)
  • Configure maven-jasypt-plugin:
<plugin>
  <groupId></groupId>
  <artifactId>jasypt-maven-plugin</artifactId>
  <version>3.0.4</version>
  <configuration>
    <password>${env.ENCRYPTION_PASSWORD}</password>
  </configuration>
  <executions>
    <execution>
      <phase>initialize</phase>
      <goals>
        <goal>encrypt</goal>
      </goals>
    </execution>
  </executions>
</plugin>
  • Pass the key through environment variables at runtime:
export ENCRYPTION_PASSWORD=s3cr3t
mvn clean package -Pprod

4.3 Linkage between Profile and Key Management

It is recommended to assign independent keys to each environment:

<profiles>
  <profile>
    <id>prod</id>
    <properties>
      <>${env.PROD_JASYPT_PWD}</>
    </properties>
  </profile>
  
  <profile>
    <id>dev</id>
    <properties>
      <>dev_secret</>
    </properties>
  </profile>
</profiles>

Summarize

Through the in-depth deconstruction of the Maven resource filtering mechanism, we have achieved security reinforcement from basic environmental isolation to enterprise-level. It is worth stressing that no automation tool can replace rigorous process design. It is recommended to adopt security practices such as key rotation, configuration audit, and construction of traceability in production environments, and use Maven Profile as a key link in the continuous delivery system rather than the only solution.

The above is the detailed explanation of resource filtering and attribute management in Maven Profile. For more information about resource filtering and attribute management in Maven Profile, please pay attention to my other related articles!