13

I have two property files [one.properties and two.properties]. I want to dynamically load the property files into my Ant project from the command line.

My build file name is build.xml.

Command line:

> ant build [How do I pass the property file names here?]
Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
Muthukumar
  • 8,679
  • 17
  • 61
  • 86
  • You want to create the files or you want to load the files? – ewan.chalmers Jul 05 '12 at 16:28
  • I want to run the property file using ant build. I have created two property file but dunno how to run from cmd. I am runing now -> ant build. This will run the build.xml file and the property file i mentioned inside the build file, but I have multiple property file i need to pass property file name dynamically in the ant command build. – Muthukumar Jul 06 '12 at 07:10

2 Answers2

26

Loading property files from the command line

ant -propertyfile one.properties -propertyfile two.properties 

Individual properties may be defined on the command line with the -D flag:

ant -Dmy.property=42


Loading property files from within an Ant project

LoadProperties Ant task

<loadproperties srcfile="one.properties" />
<loadproperties srcfile="two.properties" />

Property Ant task

<property file="one.properties" />
<property file="two.properties" />

Match property files using a pattern

JB Nizet's solution combines concat with fileset:

<target name="init" description="Initialize the project.">
  <mkdir dir="temp" />
  <concat destfile="temp/combined.properties" fixlastline="true">
    <fileset dir="." includes="*.properties" />
  </concat>
  <property file="temp/combined.properties" />
</target>
Community
  • 1
  • 1
Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
  • Thaks for your respond.I feel you misunderstood my question. I need to run via 'cmd' and need to pass property file name dynamically. pls advice! – Muthukumar Jul 06 '12 at 08:55
  • @Muthukumar, I expanded my answer to show how to pass the names of properties files on the command line [Specify properties files on command line]. – Christopher Peisert Jul 06 '12 at 14:32
  • In windows you might need to add quotes, "-Dmy.property=42" – cmcginty Apr 12 '14 at 00:07
0

Making a build condition such that if Required System parameters are provided for build then only allowing for the next target else build gets fail.

 Pass CMD: ant -DclientName=Name1 -Dtarget.profile.evn=dev
 Fail CMD: ant
<project name="MyProject" default="myTarget" basedir=".">
    <target name="checkParams">
        <condition property="isReqParamsProvided">
            <and>
                <isset property="clientName" /> <!-- if provide read latest else read form property tag -->
                <length string="${clientName}" when="greater" length="0" />
                <isset property="target.profile.evn" /> <!-- mvn clean install -Pdev -->
                <length string="${target.profile.evn}" when="greater" length="0" />
            </and>
        </condition>
        <echo>Runtime Sytem Properties:</echo>
        <echo>client              = ${clientName}</echo>
        <echo>target.profile.evn  = ${target.profile.evn}</echo>
        <echo>isReqParamsProvided = ${isReqParamsProvided}</echo>
        <echo>Java/JVM version: ${ant.java.version}</echo> 
    </target>

    <target name="failOn_InSufficentParams" depends="checkParams" unless="isReqParamsProvided">
        <fail>Invalid params for provided for Build.</fail>
    </target>

    <target name="myTarget" depends="failOn_InSufficentParams">
        <echo>Build Success.</echo>
    </target>
</project>

@see also: Replace all tokens form file

Yash
  • 9,250
  • 2
  • 69
  • 74