Maven多环境打包

Maven多环境打包

本文是基于SpringBoot框架的多环境打包,其中打包插件使用的是maven-assembly-plugin。

一、pom.xml配置

...
<profiles>
	<profile>
    	<id>dev</id>
        <properties>
        	<profiles.active>dev</profiles.active>
        </properties>
        <activation>
        	<activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
    	<id>prod</id>
        <properties>
        	<profiles.active>prod</profiles.active>
        </properties>
    </profile>
</profiles>
<build>
    <!-- 指定加载的资源 -->
	<resources>
    	<directory>src/main/resources</directory>
        <filtering>true</filtering>
    </resources>
    <resource>
    	<directory>src/env/${profiles.active}</directory>
        <includes>**</includes>
        <filtering>true</filtering>
    </resource>
    <plugins>
    	<plugin>
        	<groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <!-- 不适用自带的模板 -->
            	<appendAssemblyId>false</appendAssemblyId>
                <!-- 加载指定目录下的配置 -->
                <descriptors>
                	<descriptor>src/env/assembly.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
            	<id>make-assembly</id>
                <phase>package</phase>
                <goals>
                	<goal>single</goal>
                </goals>
            </executions>
        </plugin>
    </plugins>
</build>
...

二、资源文件存放路径

  1. 在src/main/resources目录下存放application.properties、bootstrap.properties;
  2. 在src/env目录下放置assembly.xml;
  3. 在src/env/dev下放置application-dev.properties、bootstrap-dev.properties;
  4. 在src/env/prod下放置application-prod.properties、bootstrap-prod.properties;

三、assembly.xml配置

...
<id>${profiles.active}</id>
<formats>
	<format>tar.gz</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
	<fileSet>
    	<directory>src/env/${profiles.active}</directory>
        <!-- 输出到conf目录下 -->
        <outputDirectory>conf</outputDirectory>
        <fileMode>0644</fileMode>
    </fileSet>
    <fileSet>
    	<directory>src/main/resources</directory>
        <outputDirectory>conf</outputDirectory>
        <!-- 激活属性过滤 -->
        <filtered>true</filtered>
        <fileMode>0644</fileMode>
        <excludes>
        	<exclude>embedded/**</exclude>
        </excludes>
    </fileSet>
</fileSets>
...

四、application.properties配置

# ...
# 使用@var@引用pom.xml中的对应的标签值
spring.profiles.active=@profiles.active@
# ...

五、打包

# 使用-P选项激活指定的profile,并且会替换掉配置文件中的@profiles.active@
mvn clean package -P dev 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐江小鱼

知识创造财富,余额还是小数

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值