(十八)maven实践-使用profile实现多环境可移植构建

来源:互联网 发布:映射网络驱动器 密码 编辑:程序博客网 时间:2024/06/15 06:12

开发中经常会有多套环境,比如开发环境、测试环境、生产环境,每套环境的参数是不同的,比较常见的如数据库连接信息、日志信息、配置文件信息等。在开发过程中通常项目负责人会在上线时候,打包之前逐个将开发环境各种配置信息,换成线上环境信息,然后打包。另外一种方式是直接打包,然后用事先准备好的生产环境配置文件替换打好的war包里的配置文件。这样的方式存在以下问题:

(1)、项目负责人在修改配置文件的时候可能会遗漏掉一些项,导致打好的包放到生产环境启动报错。

(2)、把开发环境中的配置修改成生产环境后,如果不小心就会把修改提交到代码库中,其他开发人员更新代码后,在不知道的情况下对生产数据库等造成垃圾数据,甚至删掉生产数据,十分危险;再说如果是直接替换配置文件的方式,有可能,开发环境的配置文件中已经添加了新的配置,而生产环境的配置文件中没有,也会导致升级时报错。

以上问题引发的上线搞到凌晨或者更晚,一堆问题,这不就是你一直担心的“上线惊魂”吗?那我们能不能不这样刀耕火种呢,maven提供的profile功能,能实现你想要的做的事,那我们来看看吧。还是使用之前我们创建的web项目,我们在pom.xml文件中添加profile配置

<profiles>    <!-- 开发环境 -->    <profile>        <id>dev</id>        <activation>            <activeByDefault>true</activeByDefault>        </activation>        <properties>              <web.db.driverClassName>com.mysql.jdbc.Driver</web.db.driverClassName>            <web.db.url>jdbc:mysql://127.0.0.1:3306/dev</web.db.url>            <web.db.username>dev</web.db.username>            <web.db.password>dev</web.db.password>        </properties>    </profile>    <!-- 测试环境 -->    <profile>        <id>test</id>        <activation>            <activeByDefault>false</activeByDefault>        </activation>        <properties>            <web.db.driverClassName>com.mysql.jdbc.Driver</web.db.driverClassName>            <web.db.url>jdbc:mysql://10.11.143.62:3306/test</web.db.url>            <web.db.username>test</web.db.username>            <web.db.password>test</web.db.password>        </properties>    </profile>    <!-- 生产环境 -->    <profile>        <id>pro</id>        <activation>            <activeByDefault>false</activeByDefault>        </activation>        <properties>            <web.db.driverClassName>com.mysql.jdbc.Driver</web.db.driverClassName>            <web.db.url>jdbc:mysql://10.11.143.66:3306/pro</web.db.url>            <web.db.username>pro</web.db.username>            <web.db.password>pro</web.db.password>        </properties>    </profile></profiles>


 

 

在resources下新建config.properties文件,添加如下配置

jdbc.driver=${web.db.driverClassName}jdbc.url=${web.db.url}jdbc.username=${web.db.username}jdbc.password=${web.db.password}


 

在pom.xml文件中添加resource配置,如下

完整的pom.xml文件配置:

<resources>    <resource>        <directory>src/main/resources</directory>        <filtering>true</filtering>        <includes>            <include>**/*.properties</include>            <include>**/*.xml</include>            <include>**/*.txt</include>        </includes>        <excludes>            <exclude>deploy/*</exclude>        </excludes>    </resource></resources>


 

在resources下添加application-context.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"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <property name="locations">            <list>                <value>classpath:/config.properties</value>            </list>        </property>    </bean>    <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">        <property name="driverClassName" value="${jdbc.driver}"></property>        <property name="url" value="${jdbc.url}"></property>        <property name="username" value="${jdbc.username}"></property>        <property name="password" value="${jdbc.password}"></property>        <property name="maxActive" value="30"></property>        <property name="maxIdle" value="10"></property>        <property name="minIdle" value="5"></property>        <property name="initialSize" value="10"></property>        <property name="logAbandoned" value="true"></property>        <property name="removeAbandoned" value="true"></property>        <property name="removeAbandonedTimeout" value="180"></property>        <property name="maxWait" value="5000"></property>        <property name="timeBetweenEvictionRunsMillis" value="3600000"></property>        <property name="minEvictableIdleTimeMillis" value="18000000"></property>    </bean></beans>

 

在xml配置文件中使用占位符引用config.properties中定义的属性。现在我们运行mvn命令:

mvn clean install -DskipTests -Pdev


在target目录下我们查看一下生成的文件,你会发现config.properties文件里的占位符都被替换成你在pom.xml文件profile dev(开发环境)中定义的值


下面我们把命令改成:

mvn clean install -DskipTests -Ptest


 

你会发现这次的值是test profile中定义的值


 生产环境的你自己可以试试



原文地址:http://ittechcourse.com/archives/107

0 0