Ant file set using path with spaces in it - macos

I'm making someone else's ant build work with a source path that contains a space. Most of it has been basic shell script stuff, adding double-quotes around paths, but this one has me stumped:
(from build.xml):
<path id="headers">
<fileset dir="${source.main.dir}">
<include name="**/*.h"/>
</fileset>
</path>
source.main.dir is a path containing spaces. It's prepended to each of the include file names found by the pattern. How do I get this to happen with quotes around each one?
In case it matters, I'm running the build from the command line on OS X Lion 10.7.4.

I think I got it! I changed:
<pathconvert pathsep=" " property="doc.files.list" refid="headers" />
to
<pathconvert pathsep=" " property="doc.files.list" refid="headers">
<map from='${source.main.dir}' to='"${source.main.dir}"' />
</pathconvert>
And it seems to be working now.

Related

Return a custom data type from Ant to Maven

I have an Ant script called from Maven to perform some tasks. One such task is to read a text file linewise and use its content to perform text replacements on a huge set of files. The text file will be in the following format on each line:
name#URL#Title
Ant task for this is as follows:
<target name="update-urls" depends="load-ant-contrib">
<loadfile property="file" srcfile="${mapping.file}"/>
<foreach param="file.entry" list="${file}" delimiter="${line.separator}" target="update-template"></foreach>
</target>
<!--Get the name,url and title of each line-->
<target name="update-template">
<propertyregex property="name"
input="${file.entry}"
regexp="(.*)#(.*)#(.*)$"
select="\1"/>
<propertyregex property="url"
input="${file.entry}"
regexp="(.*)#(.*)#(.*)$"
select="\2"/>
<propertyregex property="title"
input="${file.entry}"
regexp="(.*)#(.*)#(.*)$"
select="\3"/>
<echo>${template.name}</echo>
<!--Use title and url to match and replace the URL with the URL in text file-->
<replaceregexp byline="true"
match="(<title>${title} *</title>.*)${url} *"
replace="\1../modified-path/${name}.zip"
flags="g"
encoding="utf-8"
>
<!--Following are the files on which replacement has to happen-->
<fileset dir="${huge.set.of.files}/items">
<include name="**/info.xml"/>
</fileset>
</replaceregexp>
</target>
Since this needs to work on huge set of files, the above solution is not very efficient. I have coded a mutithreaded solution for it in Java which takes up multiple regex expressions(name, value pairs) at once.I've integrated it into my maven project as a mojo.
I'm having trouble with two things:
Framing a data-type to contain a list of name, url, title triplets in Ant.
If I'm successful with step 1., how to return this data structure to POM from where the Ant task got called and decode this data structure to be able to access each element's name, url and title separately..
Can someone guide me on how to go about this..

When causing a makefile to change directory and run ant in that new directory error "JAVA_HOME is not defined correctly"

Even if you have Java and/or ant installed through the windows installers, you still need to set your environment variables. Make sure you are linking your java to a JDK rather than a JRE, my guess as a newbie is that when you are compiling your own code you should always use the JDK as it has all the tools you need.
Original post:
This is all happening on Windows. I call make. It checks out a "code" folder from svn into the directory that makefile is in. Inside "code", is a build.xml. I want my makefile to change to that directory and then run ant on it. I am getting the error in my command prompt:
C:\Users\Ryan\Desktop\maketest>make
svn co (address removed)
Checked out revision 107.
cd code; ant clean compile jar run
uname: not found
basename: not found
dirname: not found
which: not found
Error: JAVA_HOME is not defined correctly.
We cannot execute java
make: *** [runant] Error 1
I checked here http://sunsite.ualberta.ca/Documentation/Gnu/make-3.79/html_chapter/make_5.html#SEC46 and it seems that this should be do-able in my makefile:
runant:
cd code; ant clean compile jar run
as this will open the command in a subshell but it will also run ant in that subshell.
This all works fine if I manually change into the code folder and run ant. I only get this JAVA_HOME not defined correctly error when I try to do this all from a single makefile.
makefile:
commands = checkoutcode checkoutdocs runant
svnCode = https://version-control.adelaide.edu.au/svn/SEPADL15S2UG7/code/
svnDocs = https://version-control.adelaide.edu.au/svn/SEPADL15S2UG7/updateDocs
ifdef version
REVISION = -r$(version)
endif
all: full
full: checkoutcode checkoutdocs runant
# ensure you use a single tab for the commands being run under that name
checkoutcode:
svn co $(svnCode) $(REVISION)
checkoutdocs:
svn co $(svnDocs) $(REVISION)
#change directory into the code folder and compile the code
runant:
cd code; ant clean compile jar run
build.xml:
<project>
<!-- add some property names to be referenced later. first one is lib folder to hold all libraries -->
<property name="lib.dir" value="lib"/>
<!-- images directory used to compile with the images and also add them to the jar -->
<property name="src/images.dir" value="images"/>
<!-- ensure the classpath can see all classes in the lib folder by adding **/*.jar -->
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<!-- "Target" attribute is what we will write as a parameter to ant in the command prompt or terminal -->
<!-- eg. "ant clean" will run the commands inside the target tags, which is just to delete the directory
"build" -->
<target name="clean">
<delete dir="build"/>
<echo message="Cleaned "/>
</target>
<!-- compile command. creates a directory for the classes to be stored in, instead of the root folder
and then compiles everything in the src folder, using the classpath properties we set earlier -->
<target name="compile">
<mkdir dir="build/classes"/>
<javac includeantruntime="false" srcdir="src" destdir="build/classes" classpathref="classpath"/>
<echo message="Compiled "/>
</target>
<!-- jar command. this creates an executable jar file called Robot.jar which can run the program in full.
it uses the images and it uses the lejos libraries and embeds them into the jar -->
<target name="jar">
<jar destfile="Robot.jar" basedir="build/classes">
<fileset dir="src/images" />
<manifest>
<attribute name="Main-Class" value="Main"/>
</manifest>
<!-- this line adds everything in the lib folder to the classes being added to the jar,
ie. the lejos classes ev3 and pc -->
<zipgroupfileset dir="${lib.dir}" includes="**/*.jar" />
</jar>
<echo message="Created JAR "/>
</target>
<!-- run command. runs the program. Running the program in the command prompt or terminal is good
because it allows you to see any exceptions as they happen, rather than hiding them in eclipse -->
<target name="run">
<java jar="Robot.jar" fork="true"/>
<echo message="Run "/>
</target>
</project>
I am required to run this on different machines without any extra configuration performed, and will not be able to confirm the location of Java on those machines - the only guarantee I will get is that Java is installed and the environment variables work.
Just answering to allow you to mark as solved.
Even if you have installed Java with the oracle installer, you still need to create environements variables.
Even if you have Java and/or ant installed through the windows installers, you still need to set your environment variables. Make sure you are linking your java to a JDK rather than a JRE, my guess as a newbie is that when you are compiling your own code you should always use the JDK as it has all the tools you need.

In eclipse executing Sass with ANT task. Why have I to set 'executable' to 'sass.bat' instead of 'sass'?

I have this ANT task to execute Sass in an eclipse project:
<project basedir="." default="sass">
<target name="sass">
<apply dest="www/styles" executable="sass">
<srcfile/>
<targetfile/>
<fileset dir="styles" includes="*.scss"/>
<mapper from="*.scss" to="*.css" type="glob"/>
</apply>
</target>
</project>
It works fine in Ubuntu. In Windows 7 I have to set the executable as sass.bat.
This is the error:
Buildfile: D:\my_workspace\my_project\build.xml
sass:
BUILD FAILED
D:\my_workspace\my_project\build.xml:3: Execute failed: java.io.IOException:
Cannot run program "sass" (in directory "D:\my_workspace\my_project"):
CreateProcess error=2, The system cannot find the file specified
Total time: 326 milliseconds
Both, sass and sass.bat can be invoked from the command line so as the Ruby/bin folder is in system PATH variable.
I don't want to mantain two versions of this file for different OS.
How can I solve this?
[not an answer-but too big for comment ]
Windows shell understand how to expand pathext. Ant does not interpret - it only try .exe, not others.
See comments for Windows Users
The task delegates to Runtime.exec which in turn apparently
calls ::CreateProcess. It is the latter Win32 function that defines
the exact semantics of the call. In particular, if you do not put a
file extension on the executable, only ".EXE" files are looked for,
not ".COM", ".CMD" or other file types listed in the environment
variable PATHEXT. That is only used by the shell.
Note that .bat files cannot in general by executed directly. One
normally needs to execute the command shell executable cmd using the
/c switch.
A common problem is not having the executable on the PATH. In case you get an error message
Cannot run program "...":CreateProcess error=2. The system cannot find
the path specified. have a look at your PATH variable. Just type the
command directly on the command line and if Windows finds it, Ant
should do it too. (Otherwise ask on the user mailinglist for help.) If
Windows can not execute the program add the directory of the program
to the PATH (set PATH=%PATH%;dirOfProgram) or specify the absolute
path in the executable attribute in your buildfile.
I've solved it by adding the conditional variable exec_file with value sass.bat for Windows family OS and sass for other.
<project basedir="." default="sass">
<condition property="exec_file" value="sass.bat" else="sass" >
<os family="windows" />
</condition>
<target name="sass">
<apply dest="www/styles" executable="${exec_file}">
<srcfile/>
<targetfile/>
<fileset dir="styles" includes="*.scss"/>
<mapper from="*.scss" to="*.css" type="glob"/>
</apply>
</target>
</project>

ant java task with space issue

I was trying to execute a jar file via ant script. Like this
<java jar="${jar.file}" fork="true" failonerror="true">
<arg line="${jar.args}"/>
</java>
jar.file has full path to the jar file and contains some space in it.
When I executed that on Linux, there was no problem. But when I do the same on Windows I got error. java task couldn't locate the jar! I've tried all different variations like wrapping the file path with quote ("), replaced space with ", tried escaping with backslash, etc. Non works!
Did anyone come across this issue? Just wondering if this is Ant's limitation or I missed something.
P.S Sorry for not providing the full error message I got. I'm away of my Windows PC right now. As a workaround, I decided to copy the jar to C:\ and used that instead.
The recommended way to handle space problems within properties is to put them in
extra '', which should work in most cases, even better to use a path without spaces
<java jar="'${jar.file}'" fork="true" failonerror="true">
<arg line="${jar.args}"/>
</java>
should work, as already mentioned in my comment.
edit
you're right it won't work because of the relative path with only jar attribute
in fact i thought of something like :
<project>
<property name="jar.file" value="foobar.jar"/>
<property name="jar.dir" value="/home/rosebud/temp/path with blanks"/>
<java
dir="${jar.dir}"
jar="${jar.dir}/${jar.file}"
fork="true"
failonerror="true"
>
<arg value="..." />
</java>
</project>
and it works unexpectedly also with spaces in path as f.e. in the snippet above
thought the standard way to handle space problems would fit in as in other cases :
"'${property with blanks}'"
but it doesn't.

Apache ant deleting directories no matter what

How do I get Apache Ant to delete a directory no matter what. I want it to be deleted even if there are locks or usages of the directory on windows.
I am using a continuous integration remote agent on a Windows box which fails to delete the build directory and as a result fails the build. This is extremely annoying and is disruptive to the statistics.
There is nothing actively using the directory, and the antivirus is disabled.
I just want to delete the directory no matter what. How can I achieve that on Windows with Apache Ant?
I think you will need external program to do this.
check this one:
_http://www.codeguru.com/cpp/w-p/files/fileio/article.php/c1287
here you have comparison of unlocking tools.Check this with command line interface:
_http://ccollomb.free.fr/unlocker/
If you know what process is holding your folder you can just call taskkill...(you can even kill explorer.exe but and you can start it again)
and if your folder is shared you can use net delete command
The best you can do in Ant is to set the parameters: quiet="true" and includeemptydirs="true" in order to prevent the build halting when the directory is missing or when a lock exists and to delete the top level directory even if it is empty. Eg:
<delete quiet="true" includeEmptyDirs="true">
<fileset dir="stubbornDir"/>
</delete>
You can also make sure that the resources you are attempting to delete are not read only, so include something like this in your script before your <delete> task:
<!-- The following only works on UNIX -->
<chmod perm="a+w">
<fileset dir="${dist.dir}">
<include name="**/*.jar"/>
<exclude name="${app.context.path}"/>
</fileset>
</chmod>
<!-- Win NT alternative -->
<echo message=" To permit file deletion, execute attrib.exe to change read permissions on: ${dist.dir}"/>
<exec dir="${dist.dir}" executable="attrib.exe" os="Windows NT,Windows 2000,Windows XP">
<arg line="-R **/*.jar"/>
</exec>
But, to answer your question, I'm afraid, using Ant, it is not possible to delete files or directories when a lock exists.
However, also be aware, if you have used the <javac> task earlier in your script, then unless you have set fork="true", the task will lock all files in your classpath and keep them locked during the entirety of your build.
Hope this helps!

Resources