02-03 使用 Maven 进行分环境配置和发布版本

使用 Maven 进行分环境配置和发布版本

首先,在讲解 Maven 的用法的时候,我们需要普及下面的一些概念:

在公司,我们常见的常用的环境有:dev、sit、pro, 具体的介绍如下:

dev就是开发环境(Development Environment),每个开发人员自己搭建的环境,当然一般也会在公司内部服务器搭建一些诸如数据库、分布式服务等公用的开发环境服务。

sit就是系统集成测试环境(System Integration Testing Environment),主要目的是把系统的各个模块作为一个组进行测试。

pro就是生产环境(Production Environment),这个环境是我们最终交付的产品所运行的环境。

为什么要有这么多环境呢?答案是形势所迫。随着软件开发的分工日益精细化和软件系统的日益复杂化,不同环境所承担的职责不同,但最终目的是一样的:提高效率、保证质量、节约成本、保证收益。

关于分环境的思想这里就不多讲了,下面要讲的一个问题是分环境是如何实现的?
分环境的实现方式有很多Spring Profile、Spring Boot等等都有不同的实现。

下面讲一个使用 maven profiles 实现分环境配置的方式。

分环境实现

比如我在不同的环境需要提供不同的配置文件,怎么实现呢?

首先在pom.xml增加如下几个环境的配置,并指定配置路径:

<profiles>

    <!-- 分环境profile> -->
    <profile>
        <id>dev</id>
        <!-- 如果dev带上activeByDefault,会默认将dev下的配置复制到config目录下-->
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <env>dev</env>
            <package.target>dev</package.target>
            <spring.profiles.active.value>dev</spring.profiles.active.value>
            <yui.skip>true</yui.skip>
            <config.path>src/main/resources/config/dev</config.path>
        </properties>
    </profile>

    <!--sit-->
    <profile>
        <id>sit</id>
        <properties>
            <env>sit</env>
            <package.target>sit</package.target>
            <spring.profiles.active.value>sit</spring.profiles.active.value>
            <yui.skip>false</yui.skip>
            <config.path>src/main/resources/config/sit</config.path>
        </properties>
    </profile>

    <!-- uat -->
    <profile>
        <id>uat</id>
        <properties>
            <env>uat</env>
            <package.target>uat</package.target>
            <spring.profiles.active.value>uat</spring.profiles.active.value>
            <yui.skip>false</yui.skip>
            <config.path>src/main/resources/config/uat</config.path>
        </properties>
    </profile>

    <!--sandbox-->
    <profile>
        <id>sandbox</id>
        <properties>
            <env>sandbox</env>
            <package.target>sandbox</package.target>
            <spring.profiles.active.value>sandbox</spring.profiles.active.value>
            <yui.skip>false</yui.skip>
            <config.path>src/main/resources/config/sandbox</config.path>
        </properties>
    </profile>

    <!--prod-->
    <profile>
        <id>prod</id>
        <properties>
            <env>prod</env>
            <package.target>prod</package.target>
            <spring.profiles.active.value>prod</spring.profiles.active.value>
            <yui.skip>false</yui.skip>
            <config.path>src/main/resources/config/prod</config.path>
        </properties>
    </profile>

</profiles>

然后在打包项目的时候通过-P参数指定环境就行了。例如打包 prod 环境:

mvn install -Puat

指定环境打包的缺点

首先就是慢,也可说浪费咖啡。很多大型项目每次从编译到拉文件都要半个多小时。
那怎么节省发布的时间,让我们早点下班呢?
答案就是所有环境一个包。
5个环境就能节省2个小时,太值了!因为最终只打一个包

将所有环境打成一个包

首先在pom.xml配置maven-resources-plugin插件,并指定copy-resources的路径,把所有环境的配置都打到包里

<!-- maven-resources-plugin -->
 <plugin>
     <artifactId>maven-resources-plugin</artifactId>
     <version>2.6</version>
     <executions>
         <execution>
             <id>copy-resources</id>
             <phase>validate</phase>
             <goals>
                 <goal>copy-resources</goal>
             </goals>
             <configuration>
                 <outputDirectory>${basedir}/target/classes/config</outputDirectory>
                 <resources>
                     <resource>
                         <directory>${config.path}/</directory>
                         <filtering>true</filtering>
                     </resource>
                 </resources>
             </configuration>
         </execution>
     </executions>
 </plugin>

然后正常使用mvn install打包。

最后把要发布的包复制到指定环境机器的磁盘上以后,通过mv命令把需要发布的环境的配置移动出来。例如发布sandbox环境:

mv config/sandbox/* config/

截止到此,所有的东西我们都打成了一个环境,方便了我们接下来的操作

posted @ 2019-10-24 20:39  小猿取经-林海峰老师  阅读(540)  评论(0编辑  收藏  举报