Maven与Profile的多环境管理-非Spring

​ 开发、线上等多环境配置都是使用maven的profile配置,使用spring框架的话会很省事,因为spring框架都封装完了。但是如果使用普通的maven项目开发非spring的项目的就需要自行配置。

1、添加配置文件

​ 比如系统默认的配置文件名为:application.properties,那么再根据不同的环境配置不同的配置文件,如application-dev.propertie(开发环境)、application-local.properties(本地环境)、application-online.properties(线上环境)等。

2、配置pom.xml

(1)、profile的配置

<profiles>
    <profile>
        <id>local</id>
        <properties>
            <envProfile>local</envProfile>
        </properties>
    </profile>

    <profile>
        <id>dev</id>
        <properties>
            <envProfile>dev</envProfile>
        </properties>
    </profile>
</profiles>

(2)、maven-resources-plugin配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
        <filters>
            <filter>src/main/resources/application-${envProfile}.properties</filter>
        </filters>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>
    </configuration>
</plugin>

(3)、maven的properties配置

<properties>
    ...
    <envProfile>local</envProfile>
</properties>

3、编译与测试

命令 mvn clean package assembly:single -Plocal