maven package different profiles depending on the environment

origin

We often encounter in doing the project have multiple environment configuration for each environment is not the same problems, such as database and test environments of our production environment database certainly not the same, every time we tested the machine when URL is possible to configure a test environment, fight war production package when they need to change the configuration of the production, back and forth is very easy to make mistakes, then there is no easy way to do it? Yes, you first need to have a project using maven.

Use maven plugin pack different profiles

First, we need to perform the following configuration in the pom.xml
different definitions of environmental parameters

<profiles>
        <profile>
            <!-- 本地开发环境 -->
            <id>dev</id>
            <properties>
                <package.environment>dev</package.environment>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 测试环境 -->
            <id>sit</id>
            <properties>
                <package.environment>sit</package.environment>
            </properties>
        </profile>
        <profile>
            <!-- 生产环境 -->
            <id>prd</id>
            <properties>
                <package.environment>prd</package.environment>
            </properties>
        </profile>
    </profiles>

Adding a plug-in

<sourceDirectory>src/main/java</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>src/main/resources/config/${package.environment}</directory>
                            <targetPath>WEB-INF/classes/config</targetPath>
                            <filtering>true</filtering>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
        </plugins>

have to be aware of is

 <directory>src/main/resources/config/${package.environment}</directory>
 <targetPath>WEB-INF/classes/config</targetPath>

This section needs to be changed according to your own directory structure, all in all this section of the purpose is to <directory>route inside the file copy to go targetPath

Then we need to build our profile directory with the corresponding configuration
Here Insert Picture Description
and then you can start packing up
using the following command packaging

clean install -P sit

-P behind the configuration parameters on behalf of the environment which package you need, you can specify sit, can also be specified as a dev or prd.

Reference: https: //blog.csdn.net/li295214001/article/details/52044800#commentBox

Published 114 original articles · won praise 146 · Views 350,000 +

Guess you like

Origin blog.csdn.net/qq32933432/article/details/101063917