maven程序员在成长SpringBoot之路

Maven系列(八)assembly打包-程序和依赖jar包分开

2017-06-24  本文已影响272人  冬天只爱早晨
maven.png

前言

上一篇介绍的是“assembly打包-程序和依赖jar包分开化”的配置方法, 这一篇就来介绍下如何多环境的配置,这里请看清楚,是“程序和依赖jar包分开化+多环境”跟之前的不太一样哦。

需要修改的配置

项目的目录结构


工程结构.png

pom

添加profile配置,我这里同样配置了三种环境

<profiles>
        <profile>
            <id>local</id>
            <properties>
                <env>local</env>
            </properties>
            <!-- 如果不指定ID,默认是本地环境-->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <env>test</env>
            </properties>
        </profile>
        <profile>
            <id>product</id>
            <properties>
                <env>product</env>
            </properties>
        </profile>
    </profiles>

package.xml

新增了两处

<fileSets>
        <!--需要包含的文件与输出的路径-->
        <fileSet>
            <directory>src/main/bin</directory>
            <outputDirectory>bin/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>/</outputDirectory>
            <!-- 去除需要多环境配置的文件-->
            <excludes>
                <exclude>application.properties</exclude>
            </excludes>
        </fileSet>
        <!--多环境配置-->
        <fileSet>
            <!--${env} 可以获取打包命令里的参数-->
            <directory>src/main/resources/env/${env}/</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>

多环境打包测试

环境 命令
本地 mvn clean package -P local
测试 mvn clean package -P test
生产 mvn clean package -P product

后记

欢迎浏览我的博客

代码在Github

上一篇 下一篇

猜你喜欢

热点阅读