Generate environment specific properties file using ant replace

Sep 19 2010 Published by under Java

If you want create your config file specific to each environment, then you could take the following approach. Assuming your environment specific properties are kept in folder structure as in the image. 

The template config properties file which will contain all the properties. Each of the property that needs to be replaced by environment specific values are marked as “@adduri”.

add.uri=@adduri
update.uri=@updateuri
search.uri=@searchuri

In the environment specific property file map the values as follows.

@adduri=http://hostname/adduri
@updateuri=http://hostname/updateuri
@searchuri=http://hostname/searchuri

Now you can run the ant with “env” as the target.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?xml version="1.0" ?>
<project name="eDeveloper Build" default="dev">
 
	<property name="edev.env.dir" value="environment"/>
 
	<target name="env1">
		<antcall target="build">
			<param name="build.env" value="env1" />
		</antcall>
	</target>
 
	<target name="env2">
		<antcall target="build">
			<param name="build.env" value="env2" />
		</antcall>
	</target>	
 
	<target name="dev">
		<antcall target="build">
			<param name="build.env" value="dev" />
		</antcall>
	</target>		
 
	<target name="prod">
		<antcall target="build">
			<param name="build.env" value="prod" />
		</antcall>
	</target>			
 
	<target name="clean">
		<delete file="${edev.env.dir}/myapp-config.properties"/>
	</target>
 
	<target name="build" depends="clean">
		<copy file="${edev.env.dir}/myapp-templage-config.properties" tofile="${edev.env.dir}/myapp-config.properties"></copy>
		<replace 
		    file="${edev.env.dir}/myapp-config.properties"
			summary="true"
			replacefilterfile="${edev.env.dir}/${build.env}/myapp-${build.env}-config.properties">
		</replace>
	</target>
 
</project>

No responses yet

Leave a Reply