第10章 多环境下构建项目

第10章 多环境下构建项目

本章根据实际情况,介绍多种部署环境下构建项目的配置。

在大多数公司,都会有多种项目的运行环境,例如:开发环境、测试环境、预发布环境、正式环境…
我们都会遇到这样一个问题,不同环境的一些配置是不同的,例如:数据库配置、网银接口的账号和地址配置、邮箱和短信账号配置等等。那么在maven构建项目时,需要根据不同的环境,打包不同配置。
还是用我们的crm项目进行演示,基本项目目录:

Alt text

10.1 属性配置

首先,在parent项目中配置不同环境的属性,例如,我们的数据库链接和redis链接,正式环境和开发环境不一致,此时在parent中的pom.xml中添加,如图:

Alt text


<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <dataSource.url>jdbc:mysql://192.168.100.211:3306/crm</dataSource.url>
            <dataSource.username>dev</dataSource.username>
            <dataSource.password>devpassword</dataSource.password>
            <redis.host>192.168.100.211</redis.host>
            <redis.port>6379</redis.port>
            <redis.password>redispassword</redis.password>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>online</id>
        <properties>
            <dataSource.url>jdbc:mysql://192.168.200.211:3406/crm</dataSource.url>
            <dataSource.username>online</dataSource.username>
            <dataSource.password>onlinepassword</dataSource.password>
            <redis.host>192.168.200.211</redis.host>
            <redis.port>6479</redis.port>
            <redis.password>redispassword</redis.password>
        </properties>
    </profile>
</profiles>

以上代码中,id为dev的是开发环境,id为online的正式环境,其中activeByDefault表示default为开发环境,意思说当不添加-P命令时,以开发环境的配置打包。

10.2 资源文件配置

如图,在crm-web中,有application.properties配置文件,此文件中配置了web项目中需要的所有配置,其中也包含数据库链接和redis链接。为了能让maven的maven-resources-plugin插件替换关键点,我们将需要替换的地方用${}代替。

Alt text

注意:所有需要替换的地方,命名需要与parent项目pom.xml中的标签名相同,如图。

Alt text

10.3 pom.xml配置

最后,在crm-web项目pom.xml中,添加maven-resources-plugin插件,同时在resource中,设置过滤配置filtering为true

Alt text


<build>
    <finalName>crm-web</finalName>
    <resources>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <outputDirectory>${basedir}/src/main/webapp/WEB-INF/classes</outputDirectory>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.7</version>
            <configuration>
              <encoding>UTF-8</encoding>
            </configuration>
            </plugin>
    </plugins>
</build>

10.4 不同环境打包

配置已完成,现在来做打包测试。

10.4.1 开发环境打包

  • 命令方式
    mvn clean install –Pdev

    Alt text

    Alt text

  • Eclipse操作方式
    右击crm-parent—>Run As—>Run configuations
    在Goals中键入clean install –Pdev,然后点击Run
    注意:-P后的字母代表开发环境或正式环境的ID。
    运行不起来请看第二章2.2章节
    如图

Alt text

因为parent设置了聚合关系,所以他的子项目都打包完成,如图:

Alt text

验证我们的配置文件是否正确,在crm-web项目中target/crm-web/WEB-INF/classes/中,打开application.properties,发现已经替换成开发环境的变量,如图:

Alt text

同样,打开war包验证application.properties,也是正确的。

Alt text

10.4.2 正式环境打包

与开发环境的步骤一致。
- 命令方式:
mvn clean install –Ponline

Alt text

  • Eclipse操作方式:

右击crm-parent—>Run As—>Run configuations
在Goals中键入clean install –Ponline,然后点击Run
注意:-P后的字母代表开发环境或正式环境的ID。
如图

Alt text

Alt text

Alt text


 

版权声明:本文为博主原创文章,未经博主允许不得转载。

 

posted @ 2015-11-11 20:09  _JA  阅读(179)  评论(0编辑  收藏  举报