ANT —— Jenkins配置Profile

简介

通过Jenkins编译ant打包的项目,并配置不同环境的jdbc.properties

ANT 脚本

<?xml version="1.0"?>
<project name="com.esoft.jdp2p" default="init" basedir=".">
	... 
	...
	<target name="ENVIRONMENTAL_CONFIGURATION">
		<!-- 当自定义参数TARGET传递的是DEV,会调用devConfig判断为True,改变配置-->
		<condition property="DEV_FLAG">
			<equals arg1="${TARGET}" arg2="DEV"/>
		</condition>
		<antcall target="devConfig"/>

		<!-- 当自定义参数TARGET传递的是PRO,会调用proConfig判断为True,改变配置-->
		<condition property="PRO_FLAG">
			<equals arg1="${TARGET}" arg2="PRO"/>
		</condition>
		<antcall target="proConfig"/>

		<!-- 当自定义参数TARGET传递的是SDE,会调用sdeConfig判断为True,改变配置-->
		<condition property="SDE_FLAG">
			<equals arg1="${TARGET}" arg2="SDE"/>
		</condition>
		<antcall target="sdeConfig"/>
	</target>

	<!-- 配置不同数据源 -->
	<target name="devConfig" if="DEV_FLAG">
		<echo message="--------------------------------------- 配置测试环境 begin --------------------------------------- "/>
		<!-- 将dev中的jdbc.properties拷贝到class下,如果存在相同的重写 -->
		<copy file="profile/dev/jdbc.properties" todir="WebRoot/WEB-INF/classes" overwrite="true"/>
		<echo message="--------------------------------------- 配置测试环境 end --------------------------------------- "/>
	</target>

	<target name="proConfig" if="PRO_FLAG">
		<echo message="--------------------------------------- 配置生产环境 begin --------------------------------------- "/>
		<!-- 将pro中的jdbc.properties拷贝到class下,如果存在相同的重写 -->
		<copy file="profile/pro/jdbc.properties" todir="WebRoot/WEB-INF/classes" overwrite="true"/>
		<echo message="--------------------------------------- 配置生产环境 end --------------------------------------- "/>
	</target>

	<target name="sdeConfig" if="SDE_FLAG">
		<echo message="--------------------------------------- 配置开发环境 begin --------------------------------------- "/>
		<!-- 将sde中的jdbc.properties拷贝到class下,如果存在相同的重写 -->
		<copy file="profile/sde/jdbc.properties" todir="WebRoot/WEB-INF/classes" overwrite="true"/>
		<echo message="--------------------------------------- 配置开发环境 end --------------------------------------- "/>
	</target>

</project>

Jenkins 配置

加入插件 String Parameter

ANT —— Jenkins配置Profile
配置TARGET(与build.xml中判断的参数一致),并赋值DEV或PRO或SDE,分别代表不同环境

总结

ANT 相关配置及属性

<condition property="PRO_FLAG">
	<equals arg1="${TARGET}" arg2="PRO"/>
</condition>
<antcall target="proConfig"/>

<target name="proConfig" if="PRO_FLAG">
	<echo message="--------------------------------------- 配置生产环境 begin --------------------------------------- "/>
	<!-- 将pro中的jdbc.properties拷贝到class下,如果存在相同的重写 -->
	<copy file="profile/pro/jdbc.properties" todir="WebRoot/WEB-INF/classes" overwrite="true"/>
	<echo message="--------------------------------------- 配置生产环境 end --------------------------------------- "/>
</target>
  • condition
    代表条件判断,这个判断只返回true或false,并将结果赋给属性property
  • equals
    判断arg1的值与arg2的值是否一致,一致是property=true,反之property=false
  • antcall
    程序依次从上向下执行,调用target对应脚本,这里指proConfig
  • target
    • name
      别名,供antcall使用
    • if
      代表if对应的property为true时会进入target
    • [扩展知识]if代表true,unless代表false

上述代码的意思为:
- 判断Jenkins自定义参数TARGET,参数是否为PRO,如果是的话PRO_FLAG为true,否则为false。
- 为true时,会将profile/pro/jdbc.properties替换掉WebRoot/WEB-INF/classes下的jdbc.properties文件,依此完成动态配置请求数据源。

环境说明

  • DEV
    测试环境
  • PRO
    生产环境
  • SDE
    开发环境

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-13
  • 2022-12-23
  • 2021-11-18
  • 2022-01-28
  • 2021-12-04
猜你喜欢
  • 2022-01-09
  • 2021-07-17
  • 2021-12-09
  • 2022-12-23
  • 2021-12-31
  • 2021-04-11
  • 2021-07-10
相关资源
相似解决方案