apache ant loadfile with new line in the results - ant

I am trying to use LoadFile from Apache Ant to read a file which will have only the date in it. I am using this date to generated target file with appended date.
`<loadfile property="DATE" srcFile="DATE.txt" />`
Source file: ABC.txt
Target file: ABC_${DATE}.txt
File DATE.txt content is 10182017
But the target file is being created as
ABC
_10182017.txt
Is there a way I can avoid the new/next line while reading the date from the file ?

You can fix this problem with nested filterchain functions.
<loadfile property="DATE" srcfile="DATE.txt">
<filterchain>
<striplinebreaks />
</filterchain>
</loadfile>
Check out https://ant.apache.org/manual/Types/filterchain.html for more details.

Related

Apache Ant: copy files using regex

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.

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>

Can't delete a symlink using ANT Script

I am trying to delete a symlink using the below line :
<symlink action="delete" link="/path/of/link/symlink"/>
It throws an error saying:
Could not create tempfile in /directory/where/symlink/points
The /directory/where/symlink/points is supposed to be read-only. Is there a way in which I could just delete the symlink ?
Symlinks pointing to read-only resources may be deleted using the <delete> Ant task.
<target name="delete-symlink">
<delete file="/path/of/link/symlink" followsymlinks="false"
removenotfollowedsymlinks="true" />
</target>
From the <delete> Ant task documentation:
removeNotFollowedSymlinks Whether symbolic links (not the files/directories
they link to) should be removed if they haven't been
followed because followSymlinks was false or the
maximum number of symbolic links was too big. Since
Ant 1.8.0

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>

Ant build scripts, antcall, dependencies, etc

I have a build script and as part of that script it copies a jar file to a directory, for ease lets call it the utils jar. the utils jar is built by another build script sitting in another directory. What im trying to do have my build script run the utils build script so that I can ensure the utils jar is up to date.
So I know I need to import the utils build file.
<import file="../utils/build/build.xml" />
Which doesn't work because the import task, unlike almost every other ant taks, doesn't run from basedir, it runs from the pwd. So to get around that I have this little ditty, which does successfully import the build file
<property name="baseDirUpOne" location=".." />
<import file="${baseDirUpOne}/utils/build/build.xml" />
So now that ive solved my import problem I need to call the task, well that should be easy right:
<antcall target="utils.package" />
note that in the above, utils is the project name of ../utils/build/build.xml
the problem I'm now running into is that ant call doesn't execute in ../utils/build so what I need, and cant find, is a runat property or something similar, essentially:
<antcall target="utils.package" runat="../utils/build" />
The reason I need this is that in my utils build file the step to select which code to copy to the jar is based on relative paths so as to avoid hardcoding paths in my ant file. Any ideas?
I've got something similar set up: I have a main Ant build.xml which calls a separate build.xml that takes care of building my tests. This is how I do it:
<target name="build-tests">
<subant target="build">
<fileset dir="${test.home}" includes="build.xml"/>
</subant>
</target>
The trick is to use subant instead of antcall. You don't have to import the other build file.
Try using the "ant" task instead of the "antcall" task, which runs the imported build directly instead of importing it into the current build file. It has a "dir" parameter:
the directory to use as a basedir
for the new Ant project. Defaults to
the current project's basedir, unless
inheritall has been set to false, in
which case it doesn't have a default
value. This will override the basedir
setting of the called project.
So you could do:
<ant antfile="${baseDirUpOne}/utils/build/build.xml" dir="../utils/build" />
or something like that.
You can pass params down to antcall using nested in the antcall block. So, you can pass the properties down that way (probably even basedir since properties are immutable).

Resources