Maven build combined with profile to realize multi environment configuration

problem

The development of a project mainly includes several stages: development, testing and final deployment. Each stage has different settings for configuration (database and log). The deployment engineer is responsible for modifying the configuration and launching each time. This way of working makes everyone tremble every time they go online and decide to study how maven can solve this problem.

thinking

The configuration items of several environments are classified into database configuration, log path configuration and external dependent interface configuration.
If we can realize the different replacement of resources when generating different distribution packages, we can achieve the goal. maven's build combined with profile can be realized by dynamically specifying the configuration file.

step

1. First, you need to determine the filter and the resources to be filtered in the pom file, which is realized by adding filter and resource in the build node.
2. Then, the pom file configures multiple IDs of the profile and dynamically specifies the configuration through mvn package -P.

Examples

build tag:

<build>
    <filters>
        <filter>src/main/filters/filter-${env}.properties</filter>
    </filters>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

The build/resource/directory configuration indicates that the resources under src/main/resources should be filtered; The build/resource/filtering configuration indicates that the filtering method adopts file filtering.
build/filter means that the filter file is Src / main / filters / filter - ${env} Properties, where ${env} is a variable, which is defined by profile in pom file.

profiles tab:

<!--Default activation can be configured here-->
<properties>
    <env>dev</env>
</properties>

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <env>dev</env>
        </properties>
        <activation>
            <!--This field indicates default activation-->
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile> 
    <profile> 
        <id>test</id>
        <properties>
            <env>test</env>
        </properties>
    </profile>
    <profile>
        <id>product</id>
        <properties>
            <env>product</env>
        </properties>
    </profile>
</profiles>

properties/env configures the default value of ${env}.
profiles configure the configuration of different environments. profile/id configuration id, profile/properties/env configuration ${env}.
Use mvn package -P(id) to dynamically specify the configuration ${env}. For example: maven package -Pproduct, filter product will be used Properties to replace the configuration file in the resources directory. For example, there is dB. In our project resources properties.

db. The contents of the properties file are as follows:

...
jdbc.connection.url=${ymqx.jdbc.url} 
jdbc.connection.username=${ymqx.jdbc.username} 
jdbc.connection.password=${ymqx.jdbc.password}
...

filter-product. The contents of the properties file are as follows:

...
ymqx.jdbc.url=jdbc:mysql://localhost:3306/ymqx?characterEncoding=UTF-8
ymqx.jdbc.username=root
ymqx.jdbc.password=admin
...

After maven executes the command maven package -Pproduct, DB Contents of properties:

...
jdbc.connection.url=jdbc:mysql://localhost:3306/ymqx?characterEncoding=UTF-8
jdbc.connection.username=root
jdbc.connection.password=admin
...

Summary

build combines with profile to realize multi environment dynamic configuration. build specifies the resource file directory; Profile configures the configuration id of multiple environments; Through maven package -P, specify the id to dynamically load the resource file.

Keywords: Maven

Added by backyard on Thu, 27 Jan 2022 21:42:16 +0200