Maven profiles multi-environment configuration

Step 1: Create a new config directory in the resources directory, and create two configuration files, profile-dev.properties (development) and profile-prod.properties (production). E.g:

Step 2: Modify the pom.xml file, you need to modify two places

1. Add the following configuration in the project tag

<profiles>
    <profile>
        <id>dev</id>
        <build>
            <filters>
                <filter>src/main/resources/config/profile-dev.properties</filter>
            </filters>
        </build>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>prod</id>
        <build>
            <filters>
                <filter>src/main/resources/config/profile-prod.properties</filter>
            </filters>
        </build>
    </profile>
</profiles>

2. Add the following under the project > build > resources tab:

<resource>
    <directory>${basedir}/src/main/resources</directory>
    <includes>
        <include>**/*.properties</include>
        <include>**/*.xml</include>
    </includes>
    <filtering>true</filtering>
</resource>

Analyze the attributes below <resource>:
<directory>: The files in the configuration directory will be replaced with attribute values ​​through ${key}. In the resource directory, we generally put jdbc connections or configuration files.
<includes>: Specifies the file in that directory.
<filtering>: This configuration means to filter the placeholders in the properties file specified above. The placeholders are in the form of ${variable name}. Maven will automatically read the configuration file, and then parse the placeholders. Use Replace the properties defined in the pom file above.
<exclueds>: In the resource directory, there are many files, but some files do not want to be replaced, you can specify them through <excluede>.
<filters>: The filters here have the same meaning as the filter of <profile>, which refers to the address of the attribute file. If this is specified when <profile> is defined above, it is not needed here, but some development habits are used in <profile> Not defined, then specified in <build>.

Case:

The content of profile-dev.properties is as follows:

#1000*60*2
thread.sleepTime=120000

#say again
redis.keyPrefix=hbase
redis.host=10.0.11.245
redis.port=6379
redis.timeout=3600000
redis.password=wetp

#hbase
hbase.zookeeper.quorum=10.0.11.104
hbase.zookeeper.property.clientPort=2181
hbase.master=10.0.11.104:16010

The contents of the replaced file wetp.properties are as follows:

thread.sleepTime=${thread.sleepTime}

#say again
redis.keyPrefix=${redis.keyPrefix}
redis.host=${redis.host}
redis.port=${redis.port}
redis.timeout=${redis.timeout}
redis.password=${redis.password}

#hbase
hbase.zookeeper.quorum=${hbase.zookeeper.quorum}
hbase.zookeeper.property.clientPort=${hbase.zookeeper.property.clientPort}
hbase.master=${hbase.master}

Guess you like

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