maven multi-environment configuration packaging

Project development often encounters that the jdbc.url tested in the configuration file is different from the online one. For this reason, it is very annoying and error-prone to modify the configuration file every time it is packaged.

It was only recently discovered that the original maven profile can be used to configure a variety of environments.
The configuration process is as follows: (For specific examples, you can download the attachment )

1. In src/main/resources/filters, create two files: test.properties and prod.properties.
Place the configuration for the test environment and the production environment separately. (Assume the jdbc.url property is set in both files)

2. Create a new src/main/resources/conf.properties file. inside settings
jdbc.url=${jdbc.url}

3. Configure pom.xml. The configuration is as follows
<profiles>
        <profile>
            <id>test</id>
            <properties>
                <env>test</env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <env>prod</env>
            </properties>
        </profile>
    </profiles>

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

4. Packing. Use mvn packge -Ptest to package the test environment. If you want prod environment configuration, just -Pprod can. Of course, if the -P parameter is not passed in the pom configuration by default, the test environment configuration is used.

Guess you like

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