利用maven-assembly-plugin加载不同环境所需的配置文件及使用场景
背景:
如何加载不同环境的配置文件已经成了势在必行的,我们通常利用profile进行,详情参见我上篇博客 http://www.cnblogs.com/lianshan/p/7347890.html,但是单单的profile实在无法满足我们的需求,因为这实在是太简单太单一了,我们将它与maven-assembly-plugin,结合起来,来实现配置分离的问题。
profile不同环境参数设置
此参数的配置与上篇几乎相同,不过是在properties的属性声明将其与assembly结合起来,具体示例如下。
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env.devMode>dev</env.devMode>
<skipAssemblyDEV>false</skipAssemblyDEV>
<skipAssemblyUAT>true</skipAssemblyUAT>
<skipAssemblyPROD>true</skipAssemblyPROD>
</properties>
</profile>
<profile>
<id>uat</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<env.devMode>uat</env.devMode>
<skipAssemblyDEV>true</skipAssemblyDEV>
<skipAssemblyUAT>false</skipAssemblyUAT>
<skipAssemblyPROD>true</skipAssemblyPROD>
</properties>
</profile>
</profiles>
我定义了三个环境的相同的四个变量,我在mvn clean package -P dev 时指定了环境信息,也就相对于制定了变量的值。
可以观察到 skipAssemblyDEV 、skipAssemblyUAT、 skipAssemblyPROD 这三个值都是排他的,聪明的小伙伴已经可能意识到了,一定有三个地方使用了这三个变量,并将他们指向了指定的配置文件。
好我们来观察assembly的相关配置。
<!--assembly test start-->
<!--this include the xml assembly-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>make-assembly-dev</id> <!--The identifier of this execution for labelling the goals during the build 标志-->
<phase>package</phase> <!--The build lifecycle phase to bind the goals in this execution to 指定其编译阶段-->
<goals>
<goal>single</goal> <!--The goals to execute with the given configuration-->
</goals>
<configuration>
<skipAssembly>${skipAssemblyDEV}</skipAssembly> <!--profile声明参数调用-->
<descriptors>
<descriptor>src/main/assembly/dev/assembly.xml</descriptor> <!--加载指定的assembly配置文件-->
</descriptors>
</configuration>
</execution>
<execution>
<id>make-assembly-uat</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<skipAssembly>${skipAssemblyUAT}</skipAssembly>
<descriptors>
<descriptor>src/main/assembly/uat/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
<execution>
<id>make-assembly-prod</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<skipAssembly>${skipAssemblyPROD}</skipAssembly>
<descriptors>
<descriptor>src/main/assembly/prod/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
<!--assembly test end-->
首先我们通过 profile声明了相应的参数。
skipAssembly 的解释如下,其声明值true和false 表明我们是否要执行下列声明的配置文件
Flag allowing one or more executions of the assembly plugin to be configured as skipped for a particular build.
This makes the assembly plugin more controllable from profiles. 此时我们再来观察下assembly.xml的相关配置文件。
<assembly>
<id>assembly-${env.devMode}</id> <!--输出文件名-->
<formats>
<format>tar.gz</format> <!--打包文件结构-->
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/assembly/bin</directory> <!-- 项目文件目录-->
<outputDirectory>bin</outputDirectory> <!--生成bin目录-->
<directoryMode>0755</directoryMode> <!--目录执行权限-->
<fileMode>0755</fileMode><!--文件执行权限-->
</fileSet>
<fileSet>
<directory>src/main/assembly/uat/conf</directory>
<outputDirectory>conf</outputDirectory>
<directoryMode>0744</directoryMode>
<fileMode>0644</fileMode>
</fileSet>
<fileSet>
<directory>lib</directory>
<outputDirectory>lib</outputDirectory>
<directoryMode>0744</directoryMode>
<fileMode>0644</fileMode>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory><!-- 依赖jar包放置目录-->
</dependencySet>
</dependencySets>
</assembly>
那么这就是一个完整的利用assembly加载不同环境所需配置文件。
具体的构建完成包结构信息如下,关于target 此块只是tar.gz 与assembly有关,其余的为其他插件使用生成,生成的包结构亦如下:

最详细的信息请参考apache官网:https://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html
利用maven-assembly-plugin加载不同环境所需的配置文件及使用场景的更多相关文章
- maven (profiles)装载不同环境所需的配置文件
引子: maven与java的联系在今天的项目已经是不可分割的 ,但是不同的项目有各具特色的项目结构,不同的项目结构使用了不同的maven插件,想要了解一个项目的项目结构,或者自己构建一个具有成熟结构 ...
- 重温.NET下Assembly的加载过程
最近在工作中牵涉到了.NET下的一个古老的问题:Assembly的加载过程.虽然网上有很多文章介绍这部分内容,很多文章也是很久以前就已经出现了,但阅读之后发现,并没能解决我的问题,有些点写的不是特别详 ...
- 重温.NET下Assembly的加载过程 ASP.NET Core Web API下事件驱动型架构的实现(三):基于RabbitMQ的事件总线
重温.NET下Assembly的加载过程 最近在工作中牵涉到了.NET下的一个古老的问题:Assembly的加载过程.虽然网上有很多文章介绍这部分内容,很多文章也是很久以前就已经出现了,但阅读之后 ...
- NET下Assembly的加载过程
NET下Assembly的加载过程 最近在工作中牵涉到了.NET下的一个古老的问题:Assembly的加载过程.虽然网上有很多文章介绍这部分内容,很多文章也是很久以前就已经出现了,但阅读之后发现,并没 ...
- Django_在单独文件中加载Django环境临时调试
创建Django环境后,每次在打印调试都需要基于项目有些麻烦. 如何在项目外的文件中加载项目环境进行便携的调试? 创建一个新的 orm.py import os if __name__ == '__m ...
- 记录一次maven打包时将test目录下的类打包到jar中,Maven Assembly Plugin的使用
今天有人问我打包后找不到主类,运行的类写在test中.按照常规,test目录下的文件不会打包到jar包中.(但是我测试一个springboot工程就可以,这里之后再研究) 具体解决如下 第一步:在po ...
- Java日志组件logback使用:加载非类路径下的配置文件并设置定时更新
Java日志组件logback使用:加载非类路径下的配置文件并设置定时更新 摘自: https://blog.csdn.net/johnson_moon/article/details/7887449 ...
- maven pom.xml加载不同properties配置[转]
可以参考http://www.openwebx.org/docs/autoconfig.html 1.pom.xml =========================== <!-- 不同的打包 ...
- idea开发maven项目热加载
JavaWeb项目,尤其是一些大型项目,在开发过程中,启动项目耗费的时间就不短.大大的拖慢了开发速度!在这分享一种不需要插件就能实现热加载的方法! 默认已经创建好一个Maven项目 点击此按钮 点击 ...
随机推荐
- perl 函数的参数列表
在perl中,定义一个函数的时候,不需要在圆括号内指定具体的参数,所有的参数都从@_ 这个列表中得到 代码示例: sub test { my ($a, $b) = @_; print qq{$a\t$ ...
- Linux 域名服务器配置
cat /etc/redhat-release CentOS Linux release 7.0.1406 (Core) 使用BIND构建DNS服务器 1.BIND服务器安装 yum install ...
- ssh在本地调用远程主机上的命令,不登录远程主机shell
需求描述: 在实际shell脚本的编写过程中,需要通过ssh远程执行一个命令,并返回执行的结果 简单来说,就是将命令发送到远程的主机上进行执行,但是并没有实际的登录到远程主机上.即通过 ssh的方式本 ...
- 将Spring源代码导入eclipse步骤
深入学习spring.研读源代码是必须的~ 1.到https://github.com/spring-projects/spring-framework/releases去找自己须要的spring版本 ...
- 超全面的JavaWeb笔记day02<CSS&JavaScript>
1.CSS的简介 2.CSS概述和与HTML的结合方式(四种)(*******) 3.CSS的基本选择器(******) 4.CSS的扩展选择器(了解) 5.CSS的盒子模型(了解) 6.CSS的布局 ...
- Java类的设计----方法的重写、覆盖
方法的重写.覆盖 在子类中可以根据需要对从父类中继承来的方法进行改造—覆盖方法(方法的重置.重写),在程序执行时,子类的方法将覆盖父类的方法. 覆盖方法必须和被覆盖方法具有相同的方法名称.参数列表和返 ...
- iOS 开发 - iOS 8 以后使用UIAlertController的使用
最近在写项目的时候,发现使用alertview和actonsheet会报警告,所以就查了一下,发现ios 9 以后会使用UIAlertController来进行操作, 具体代码如下: 1.声明 #im ...
- linux大全链接
http://man.linuxde.net/
- LVS+keeplived+nginx+tomcat高可用、高性能jsp集群
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://kerry.blog.51cto.com/172631/557749 #!/bin ...
- 华为P10闪存门
随着余承东的倡议书以及五一假期3天的时间冲刷,华为的闪存门事件,似乎被冲淡了.但相信还有很多人对华为“闪存门”的起始及发展过程不是特别了解.而华为作为2017年Q1季度手机出货量的冠军,居然在4月份出 ...