I have file abc.txt and i need to check that file exists in suppose
c:\test\abc.txt location
If true then I need to print some message else I need to perform some operation.
Best is to use ant <available ../> to fill a property and then either using ant-contrib <if> task or create separate targets that run depending on the property value.
Check this answer for details: Ant task to run an Ant target only if a file exists?
Related
We need to output some logging entries into a file, but only when the filename is specified. The current task reads:
<echo fileName="${debugLog}" message="${message}"/>
Currently, when the debugLog-property is not set, it creates a file named, literally "${debugLog}". How can I make it simply skip the task, when the property is not defined?
Our ant is old (1.6.5 - and cannot be upgraded), and the if:set="debugLog" attribute is simply ignored, but we use ant-contrib already, and <if> is available.
I have script that call the below target but before calling this target i want that i should be at the following location which is stored in the below variable ..
todir="${release.deployment.tool}
the target that is called is shownb below..
<target name="deploy-ion" description="Used to deploy to a given aaa.">
so finally when my target deploy-ion is being called i should make sure that I should be in
directory stored in todir , please advise how to achieve this.
I meant you can use absolute path of your property reference while performing set of tasks inside your target, as by default it uses basedir default value as cur-dir or one which is specified at project level at beginning of your build.xml file.
Also alternatively you can use ant's ant task : https://ant.apache.org/manual/Tasks/ant.html with usage like
<ant dir="${release.deployment.tool}" target="deploy-ion" />
We have a property file containing list of key and their values. The value of a property can be directory paths or numeric values or hyperlink URLs or database URLs or database server I.P address or port numbers etc.
I would like to know if there is a way in ANT build file to detrmine if the value of a property is directory path or not. Based on the answer, for the values with directory path, we would like to execute a special logic than others.
Please advise.
You want to use the Available Ant task. This is untested but it should get you started.
<target name="check.directory">
<available file="${my.property}" type="dir" property="my.property.present"/>
</target>
<target name="do-if-directory" depends="check.directory" if="my.property.present">
<!-- custom task here -->
</target>
Alternatively you can use the Condition Ant Task.
Basically, I get a path like "C:\test\subfolder1\subfolder2\subfolder3\myfile.txt", but it's possible that subfolders 1-3 don't exist already, which means I'd get an exception if I try to write to the file.
Is there a way to create the directory structure the target file is in, either by using some task that creates the structure when it outputs to the file and then deleting the file, or by parsing the directory part of the path and using the mkdir task first?
Ant will create the full tree of directories for you when you use the <mkdir> task. So you just need to use the <dirname> task to get the directory name from the file name.
<dirname property="directoryProperty" file="${filePathProperty}"/>
<mkdir dir="${directoryProperty}" />
The first line extracts the directory portion of your file path and stores it in the directoryProperty property. The second line creates the directory (and any parent directories that don't exist).
This task works well
<mkdir dir="${file}/../"/>
Sometimes we could have an alternate choice, using touch task
<touch file="${file}" mkdirs="true" verbose="true"/>
This task should do the job but would have a side effect to create the file with zero size
Just make failonerror=false to avoid the error to stop the whole logic.
<delete includeemptydirs="true" failonerror="false">
<fileset dir="${builder-base.dir}" includes="**/*"/>
</delete>
Using the
<mkdir dir="${dir}"/ >
inside your <target> tag should work, but I am not sure what else you want to do along with mkdir?
I'm not 100% sure it'll work but you might be able to do something like the following to make the parent directory you're after:
<mkdir dir="${file}/../"/>
If that doesn't work straight off then it might be worth defining a property using the location syntax before creating a directory with the new property:
<property name="dir" location="${file}/../" />
<mkdir dir="${dir}" />
Well-behaved Ant tasks are generally expected to create any necessary directory structures unless there is a good reason not to.
Are you writing a task? If so you should add the directory creation logic to your task. If you are getting the task from a third party you should point this fact out to them and have them fix their task. Failing that Dan's solution should work.
I need to modify a (xml-)file from Apache Ant. "loadfile" task allows to load the file's content in a property. But how to store the property's value back to a file after its (property) modification?
Of course I could write custom task to perform this operation but I would like to know if there's some existing implementation.
You can use the echo task.
<echo file="${fileName}" message="${xmlProperty}"/>
The echoxml task might be of interest to you as well.
Use propertyfile task. An example taken from ant manual:
<propertyfile file="my.properties">
<entry key="abc" value="${abc}"/>
</propertyfile>
This may be better than echo as it updates the properties file with a given value, while echo appends to or overwrites the whole file.