mvn多运行环境,用参数来打包不同的配置文件

每一个maven工程(比如web项目),开发人员在开发时,会使用一种配置文件,比如数据库配置,而测试环境可能使用另一种配置文件。

打包完成后,手动调整配置文件,工作重复度很高,可以实现maven根据参数区分不同的运行环境,打包不同的配置文件。

1为不同的环境分别建配置文件夹,笔者的配置文件目录如下,prod对应生产的配置,test对应测试环境的配置:

mvn多运行环境,用参数来打包不同的配置文件

2在pom.xml中增加配置文件,使用maven-resources-plugin插件,在compile阶段实现指定目录中配置文件的拷贝操作。

<build>
 <!-- 别的配置写在下面*位置 -->
    ********
       
  </plugins>
    <!-- 别的plugin写在下面*位置 -->
    *****
     <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <!-- 覆盖原有文件 -->
                            <overwrite>true</overwrite>
                            <!--没有地方有该配置,但是依旧好用,原因未知-->
                            <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                            <!-- 也可以用下面这样的方式(指定相对url的方式指定outputDirectory) <outputDirectory>target/classes</outputDirectory> -->
                            <!-- 待处理的资源定义 -->
                            <resources>
                                <resource>
                                    <!-- 指定resources插件处理哪个目录下的资源文件 -->
                                    <directory>src/main/resources/${package.environment}</directory>
                                    <filtering>false</filtering>
                                </resource>
                            </resources>
                        </configuration>
                        <inherited></inherited>
                    </execution>
                </executions>
            </plugin>
            <!--over-->
        </plugins>
       <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
        <!--over-->
    </build>

pom.xml中增加profiles配置

使用profiles可为maven命令执行时,**不同的变量,并依据此变量同上述的插件配合,完成指定目录中配置文件拷贝操作。

   <profiles>
        <profile>
            <!--这里是执行mvn指令时的参数值 如mvn clean package -Ptest-->
            <id>test</id>
            <!--设置默认配置,不加参数时,对应为test的环境-->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <!--这里是对应test环境配置文件所在包名,与实际包名一致即可-->
            <properties>
                <package.environment>test</package.environment>
            </properties>
        </profile>
        <profile>
            <id>product</id>
            <properties>
                <package.environment>prod</package.environment>
            </properties>
        </profile>
    </profiles>

下面就可以执行打包命令了

mvn clean package
mvn clean package -Ptest
mvn clean package -Pproduct

但是笔者在打包后,启动项目的时候遇到一个问题:

会报错:

Could not resolve placeholder 'jdbc.driver'

查询资料后发现,缺少了一个配置(下面红色部分):

<!--
        报错:Could not resolve placeholder 'jdbc.driver'
        ignoreUnresolvablePlaceholders为是否忽略不可解析的 Placeholder,如配置了多个PropertyPlaceholderConfigurer,则需设置为true
        但是此处不是仅仅设置了一个吗?不设置该属性为true为啥会报错呢?
-->
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="locations">
            <list>
                <value>classpath*:/application.properties</value>
            </list>
        </property>
        <property name="systemPropertiesMode" value="2"></property>
    </bean>

然后就可以启动了。但是整个过程笔者遇到了三个问题,还有待解决,希望大家能帮忙解决:

问题1 单个PropertyPlaceholderConfigurer,为什么还需要设置        <property name="ignoreUnresolvablePlaceholders" value="true"/>
可能原因:
多个环境,会有多个application.properties


问题2 src/main/resource下的prod和test文件夹下已经有配置文件,为什么还需要在src/main/resource下还要有这些配置文件?
可能原因:PropertyPlaceholderConfigurer中的配置直接指向了src/main/resource,先加载配置文件,没有会导致配置文件无法加载

问题3  pom.xml中有<outputDirectory>${project.build.outputDirectory}</outputDirectory>,但是并没有对project.build.outputDirectory赋值或设置,为啥不报错,还能正常使用
可能原因:
设置的有默认值:src/main/resource