ant record task save to different directory - ant

I am looking for a way to change the directory to which the ant record task saves. Is there a way to do this?
Example:
<record name="${logFileName}" action="start" append="true" loglevel="verbose" />
Currently using the example above, but i would like to save to a different directory than the one that is selected by default. Currently it just does the current working directory.
Thanks!

Just pass an absolute file name rather than a relative one:
<record name="/home/prolink007/record.txt" action="start" append="true" loglevel="verbose" />

Related

Replace a file in zip using Ant despite of timestamp

I want to replace 1-2 files in a huge archive using zip task (or any other ANT task that can do this). I know passing update=true parameter can do it but it is only if new file has grater timestamp than old file. I want to make sure that the file gets replaced even though it has old timestamp as compared to the file in existing archive.
I have gone through several posts where they suggest unzip-replace-rezip thing. However I want to avoid this if possible because my archive is huge (in GBs). Is there any other way which has minimum performance impact?
Other way I found was using zipfileset task as follows:
<zip destfile="tmp.jar">
<zipfileset src="src.jar">
<exclude name="abc.class" />
</zipfileset>
<zipfileset dir="${basedir}/myclasses" includes="abc.class" />
</zip>
<move file="tmp.jar" tofile="src.jar" />
Does this task do the same as unzip-delete-add-rezip for entire archive? Or is it more efficient in terms of time?

Concat files in Apache ANT to variable?

I know that I can use ANT's concat to merge several files into one file. But what about merging files into a variable?
Can this be done?
concat can be used as a resource collection, so you can use it in a loadresource:
<loadresource property="the.property">
<concat>
<file file="foo.txt" />
<file file="another_file.txt />
<string>You can put any resource in here, not just files!</string>
</concat>
</loadresource>
Now the the.property property will contain the concatenation of all the resources inside the concat. (I'm assuming you mean an ant property when you say variable.)

Renaming files with dir values using Ant?

I have the following simple thing to do with Ant but did not find how to do that:
move build/xxx/file.ext to dest/xxxfile.ext
I'm no Ant Guru .
file.ext is constant in this particular case
Nota : xxx can take many values so I want to apply to all these values
You need to use a mapper element to generate the destination file names. This is derived from the Ant mapper docs:
<move todir="dest">
<fileset dir="build" includes="*/*.ext" />
<mapper type="regexp" from="^([^/]*)/([^/]*)" to="\1\2"/>
</move>
When in doubt, an exec will do the job for you, but it's not always the best way.
Try the move task.
<move file="build/xxx/file.ext" tofile="dest/xxxfile.ext"/>

How to replace template in one file with content of another file?

I have a file username.txt with one word in it.
I have another file with template in it:
<root>
<user name="${username}">
</root>
I need during ant build change template ${username} with content of file username.txt.
How to do it?
What ant task should I use for this purpose?
If you can make your first file be a properties file instead of containing just the one word e.g.
username=superuser
Then you can load it using the property task. e.g.
<property file="username.properties" />
Update
If the file format needs to remain as in the question then take a look at the LoadFile task. It can be used to load the contents of a file into a property. e.g.
<loadfile property="username" srcFile="username.txt" />

Ant: Create directory containing file if it doesn't already exist?

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.

Resources