Apache Ant: copy files using regex - ant

I have the following code which doesnt work.
it gives me an error "filename doesn't support the regex attribute"
<target name="release">
<copy todir="${build.path}">
<fileset dir="${src.path}" includes="**/*.sql">
<filename regex="${regex}"/>
</fileset>
</copy>
</target>
I am trying to copy certain files using a predefined regular expression for the file names.
Is there any way to solve this problem.
I have files like
BR2-01.00.01-sns.sql
BR2-01.00.01-entrata.sql
BR2-02.00.02-mcd.sql
BR2-03.03.01-bg.sql
BR2-03.00.03-rbs.sql
BR2-04.02.01-custom.sql
I just want to provide a regex to copy files as per my regex
Example
.*\-[0][1-2]\.[0][0]\.[0-9][0-9]\-.*
should copy only
BR2-01.00.01-sns.sql
BR2-01.00.01-entrata.sql
BR2-02.00.02-mcd.sql

I guess you're using Ant 1.7.x, you should update your ant version.The regex feature for org.apache.ant.tools.ant.types.selectors.FilenameSelector is available for >= Ant 1.8.

Related

How to exclude empty directories from zipfileset

I have an ant target for creating zip like this -
<zip destfile="${dist}/myzip.zip">
<zipfileset prefix="product/install" includes="docs/resources/**,docs/*.*" excludes="docs/build.bat,docs/*.xml,docs/resources/*.html"/>
</zip>
Now, how do I ensure that empty directories don't get included in this zipfileset.
Eg: docs/resources directory only has html files, all of which I have excluded above. How do I make sure docs/resources folder doesn't get included.
Should I be checking for this manually everytime? or is there an option like includeEmptyDirs="false"?
I think there isn't an option for this in zip task, see documentation.
But what you can do is to make a copy with excludes/includes, and define to exclude the empty directories and then call the zip task on the copied folder:
<copy todir="tmp2" includeEmptyDirs="false">
<fileset dir="tmp1" excludes="**/*.txt"/>
</copy>
<zip>...
Documentation of copy

How to make a nested src tag directory optional in Ant´s javac task?

It is possible to make a fileset optional by setting the attribute erroronmissingdir to false.
How to do the same with an embedded src node in the javac task?
I have a source directory which always exists and I have an source directory with generated source files which exists only sometimes. If I try this:
<javac>
<src path="${src-dir}"/>
<src path="${build-dir}/${src-dir}" erroronmissingdir="false"/>
</javac>
I get the error that the attribute erroronmissingdir is not supported by the src element. The there an alternative?
You should be able to encapsulate filset within a src.
<src>
<fileset dir="${build-dir}/${src-dir}" erroronmissingdir="false"/>
</src>
Let me know if that works for you.

Ant: How to call a directory with php files and save the result as overwritten files

How can I use Ant to execute each php file in a directory? The output for the php execution should overwrite the original file.
The directory is on a localhost webserver with php installed. I assume the solution would involve GET and have src point to the http equivalent of each file.
Some dynamic version of
c:/webroot/php/file1.php
c:/webroot/php/file2.php
translate into
get src="localhost/php/file1.php"
get src="localhost/php/file2.php"
and then have the output overwrite itself.
Why not use the php command line combined with the ANT apply task?
Something like:
<apply executable="C:\PHP5\php.exe">
<arg value="-f"/>
<fileset dir="c:/webroot/php" includes="*.php"/>
</apply>
It's not clear to me why you want to overwrite your source files....
Update
Alternatively use an embedded groovy script
<fileset id="phpfiles" dir="c:/webroot/php" includes="*.php"/>
<groovy>
project.references.phpfiles.each {
def file = new File(it.toString())
ant.get(src:"http://localhost/php/${file.name}", dest:"output/${file.name}")
}
</groovy>

Ant Task to FTP only specified files

I have an Ant task that FTPs all files in a specified directory, and it uses a fileset:
<fileset dir="${publicDirectory}">
<include name="media/**/*" />
</fileset>
I have a file that contains all the files that I would like to include:
media/some/dir/1.txt
media/some/other/2.txt
...
How can I have the fileset read the file and only include whatever I've listed there?
I've tried quite a few things, but nothing seems to be able to get around a basic issue: The <ftp> task works only on filesets and not other types of resources. I've tried various filterchains, but to no avail.
The best I could come up with was using the Ant-Contrib <for> or <foreach> task to loop through the file and then use an <exec> task to execute the command line version of ftp.

How to create temporary directory in ant?

I'd like to create a temporary directory in ant (version 1.6.5) and assign it to a property.
The command "mktemp -d" would be ideal for this, but I cannot find similar functionality from inside ant
I can't find any official function in the docs apart from the tempfile task which apparently only creates files, not directories.
I'm considering using exec to call tempfile and get the result, however this will make my build.xml dependent on UNIX/linux, which I'd like to avoid.
Background: I'm trying to speed up an existing build process which builds inside networked filesystem. The build already copies all the source to a temporary directory, however this is on the same filesystem. I've tested changing this to /tmp/foo and it gives a worthwhile speed increase: 3mins vs 4mins.
You could combine the tempfile task with the java.io.tmpdir system property to get a file path to use to create a temporary dir:
<project default="test">
<target name="test">
<echo>${java.io.tmpdir}</echo>
<tempfile property="temp.file" destDir="${java.io.tmpdir}" prefix="build"/>
<echo>${temp.file}</echo>
</target>
</project>
Note that the tempfile task does not create the file (unless you ask it to). It just sets a property which you can use to create a file or dir.
This task sets a property to the name of a temporary file. Unlike
java.io.File.createTempFile, this task does not actually create the
temporary file, but it does guarantee that the file did not exist when
the task was executed.
Output in my environment:
test:
[echo] C:\Users\sudocode\AppData\Local\Temp\
[echo] C:\Users\sudocode\AppData\Local\Temp\build1749402932
The answer above only hints at how to create a temporary directory. The point is that merely returns a string. A more complete answer is
<target name="temptest" description="test making tempdir">
<tempfile property="mytempdir" destdir="${java.io.tmpdir}"/>
<tempfile property="mytempfile" destdir="${mytempdir}"/>
<tstamp>
<format property="now" pattern="MMMM dd yyyy"/>
</tstamp>
<copy tofile="${mytempfile}">
<string value="today=${now}"/>
</copy>
<property file="${mytempfile}"/>
<echo message="It it now ${today}"/>
</target>

Resources