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

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

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

1为不同的环境分别建配置文件夹,笔者的配置文件目录如下(其实prod和test包中仅仅需要application.properties

即可,因为只有这个文件在开发和测试环境是不同的,对于prod和test中没有的文件,会直接默认使用resource文件夹下的配置文件,这样以后对别的配置文件修改之类的,仅仅维护resource下的配置就行了,否则需要维护三处),prod对应生产的配置,test对应测试环境的配置:

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>

applicationContext.xml中的配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"
        default-lazy-init="true">

	<description>Spring公共配置 </description>
	
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:/application.properties</value>
            </list>
        </property>
        <property name="systemPropertiesMode" value="2"></property>
    </bean>
    
    <!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->
	<context:component-scan base-package="com.ruisitech.bi.service">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
		<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
	</context:component-scan>
</beans>

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

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

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

会报错:

Could not resolve placeholder 'jdbc.driver'

查询资料后发现,applicationContext.xml中缺少了一个配置(<property name="ignoreUnresolvablePlaceholders" value="true"/>),修改后如下:

<!--
        报错: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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值