Multi-environment configuration scheme of maven project (profile)

 

Preface:
  Write a hydrology to pass the time ^_^. In application development, there will always be development/testing/pre-release/online environments. It is a small pain point to quickly switch each environment configuration and package configuration.
  This article mainly describes the java project built on maven (based on spring), how to realize multi-environment configuration, step by step.

 

Multi-environment configuration:
  Centralize the multi-environment configuration files, for example, integrate them into the project code as follows.
  
  The configuration file app.properties that needs to be packaged and replaced is stored in src/main/resource/conf, and its content is:

key.name=${key.name}
key.value=${key.value}

 

profile tag:
  In maven, the profile tag is born for multi-environment configuration. It supports the definition of variable sets in each environment. After selecting a profile, it will automatically block the variables of other profiles. For
  example, the definition is as follows:

    <profiles>
        <profile>
            <id>test</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <env>test</env>
            </properties>
        </profile>
        <profile>
            <id>online</id>
            <properties>
                <env>online</env>
            </properties>
        </profile>
    </profiles>

  Two profiles, test and online, are defined here.
  
  At this time, it is packaged by default, and it will choose the profile test because it is activated by default, that is to say, the variable env (key=env, value=test) will act on the pom Additional configuration items for .xml files.

 

filter&resource tag:
  The filter and resource tags are used together, the former specifies the loaded properties file, and the latter specifies the target directory for the key/value pair replacement.

<build>  
    <finalName>test-app-project</finalName>
    
    <!-- Use the specified filter to filter, and bring -Ptest when executing the mvn command to represent the test environment (default),
    	The properties of the test environment will be loaded, -Ponline represents the online environment -->
    <filters>
        <filter>settings/${env}/app.properties</filter>
    </filters>

    <!-- resource file location src/main/resources/,
    	The ${} in the resource file below will all be replaced with the tag content in the filter -->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <!-- exclude can exclude specified files, support wildcards, matches will not be generated in the classes directory -->
            <!--<excludes>-->
            <!--<exclude>env/dev.properties</exclude>-->
            <!--</excludes>-->
        </resource>
    </resources>
</build>

  In fact, from the xml configuration, we can interpret it as follows, the filter directory is: settings/${env}/app.properties. Since the activated profile is test, the env is test. In this way, the filter directory is settings/test/app.properties . maven automatically extracts this key/value file and replaces all ${} in the configuration file in the src/main/resource directory.

 

Test:
  Compile and package through maven, which can specify the command line parameter P to specify which profile to activate.
  For example, to activate online (online configuration), execute the following command:

mvn -Ponline package

  The specific output results after execution are as follows:
  

 

Summary:
  Maven's multi-environment configuration is a basic point. Let's just write a hydrology here and record it ^_^.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324891870&siteId=291194637