When I use a path reference ID, Ant seems to evaluate any variables inside the definition before any tasks run. For example, ${common.dist} and ${common.lib} below seem to be evaluated before any tasks run.
<path id="compile.classpath">
<fileset dir="lib">
<include name="*.jar" />
</fileset>
<fileset dir="${common.dist}">
<include name="*.jar" />
</fileset>
<fileset dir="${common.lib}">
<include name="*.jar" />
</fileset>
</path>
In the Ant output I see something like this:
Adding reference: compile.classpath
Property "common.dist" has not been set
Property "common.lib" has not been set
...
Build sequence for target(s) `package' is [...]
Complete build sequence is [...]
This makes it seem like the path reference is being processed before any targets are run.
I have a compile target like this:
<target name="compile" depends="init,common">
<javac destdir="build/classes" debug="true" deprecation="true" optimize="true">
<src path="src/java" />
<classpath>
<path refid="compile.classpath" />
</classpath>
</javac>
</target>
If I copy the guts of the path reference into the classpath element inside the compile target, things seem to work fine.
Any tasks outside a target are executed on every build, in order of appearance in the build.xml, before any targets are run. If you want to use properties in a <path> defined outside a target then you need to put the <property> task that defines the properties also outside a target, and before the <path>. If you need to load the properties within a target then you'll have to put the <path> definition inside a target too (either the same one or one that runs after the one defining the properties).
See this question (and my answer) for more details.
The answer is in the Ant manual - path like structures:
By default a path like structure will re-evaluate all nested resource
collections whenever it is used, which may lead to unnecessary
re-scanning of the filesystem ...
I think you maybe forgot to set the ${common.dist} and ${common.lib} properties. They should be outside any target:
<property name="common.dist" location="dist"/>
<property name="common.lib" location="lib"/>
Related
So I'm extending my company's ant build script to add in a special module we want build in some cases. I've written an ant script that points to where I know the compiled class files for the rest of our codebase are, because they get compiled earlier in the build process. I know with 100% certainty the files are in this location.
However, whenever I try to compile this module, the classpath reference can't see those classes, and I get a bunch of "package does not exist" and "can't find symbol" errors.
I just can't seem to figure out what I'm doing wrong. Hoping for help here.
Here's my build script code:
<property name="classpath" value="${dir.dev}/out/production/Main"
<path id="pfClasspath">
<fileset dir="${classpath}">
<include name="**/*.class"/>
</fileset>
<fileset dir="${dir.dev.lib}">
<include name="**/*.jar" />
</fileset>
<fileset file="${lib.json}" /> <!-- TODO try removing this -->
</path>
<target name="compile" depends="prepare">
<javac source="1.7" classpathref="pfClasspath" srcdir="${dir.project}/src" destdir="${dir.project.build.classes}" />
</target>
The directory the "classpath" property is pointing at 100% contains all of the class files for the rest of the project. That level is the equivalent of the "src" directory on the sources side, immediately within it are the com/companyName/etc... folders.
My code contains references to the classes compiled at this location. Yet ant isn't finding them. Any help?
Try
<path id="pfClasspath">
<pathelement path="${classpath}" />
...
</path>
instead. Specifying the classpath does not mean to specify every single class file that's on the classpath, which is what you do when you define your <path> element using a <fileset>.
I'm looking for a way to load properties from a file in ant script. Specifically, I want to loop through a list of properties files, and on each loop load the properties from the current file and do something with it. Something like this:
<for param="file">
<path>
<fileset containing my properties files.../>
</path>
<sequential>
<property file="#{file}" prefix="fromFile"/>
<echo message="current file: #{file}"/>
<echo message="property1 from file: ${fromFile.property1}"/>
</sequential>
</for>
The code above results in only the first properties file from being read, even though each loop does go through each properties file name. I know property is immutable, and I can get around it by using local task or variable task from ant-contrib. However, I don't know how to apply them here, or if they even contribute to a solution in this case.
Here I used Antcontrib and two property files in the same directory as the build.xml.
p1.properties:
property1=from p1
p2.properties:
property1=from p2
The trick is to use antcall inside the for loop to call another target. Properties set in the called target at not propagated back to the caller.
build.xml:
<project name="test" default="read.property.files">
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="ant-contrib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<target name="read.property.files">
<for param="file">
<path>
<fileset dir="." includes="*.properties"/>
</path>
<sequential>
<antcall target="read.one.property.file">
<param name="propfile" value="#{file}"/>
</antcall>
</sequential>
</for>
</target>
<target name="read.one.property.file">
<property file="${propfile}" />
<echo message="current file: ${propfile}" />
<echo message="property1 from file: ${property1}"/>
</target>
</project>
The output is:
Buildfile: /home/vanje/anttest/build.xml
read.property.files:
read.one.property.file:
[echo] current file: /home/vanje/anttest/p1.properties
[echo] property1 from file: from p1
read.one.property.file:
[echo] current file: /home/vanje/anttest/p2.properties
[echo] property1 from file: from p2
BUILD SUCCESSFUL
Total time: 0 seconds
In my original question I had trouble loading entire properties file from within for loop (from ant-contrib). Turns out inside for loops, ant-contrib's own var task works just the same as property task from pure ant. All I have to do is replace <property file="#{file}" prefix="fromFile"/> with <var file="#{file}"/>. The properties loaded will be overwritten with the latest values and I don't even need prefix attribute to keep track of which loop I'm currently on.
Let's say I have a directory structure like this:
animals/dog/details
animals/cat/details
animals/frog/details
animals/horse/details
Using ant, I would like to rename all sub-directories under animals called details to now be named new. So the result would be this:
animals/dog/new
animals/cat/new
animals/frog/new
animals/horse/new
I've tried something like this:
<move tofile="new">
<path id="directories.to.rename">
<dirset dir="animals">
<include name="**/details"/>
</dirset>
</path>
</move>
But get this error:
Cannot concatenate multiple files into a single file.
You can carry out the rename you describe by means of a mapper. For example:
<move todir="animals">
<dirset dir="animals" includes="**/details" />
<globmapper from="*/details" to="*/new"/>
</move>
(There's a similar example at the end of the move task docs.)
The error you saw arose because you've mixed the single-file mode of the move task (tofile) with multiple-file mode.
There's no need to nest the dirset in a path as the move task accepts any file-based resource collection, including dirset.
Use Ant-Contrib's for task and propertyregex task.
<target name="test">
<for param="detailsDir">
<dirset dir="animals">
<include name="**/details"/>
</dirset>
<sequential>
<propertyregex property="output.dir" input="#{detailsDir}" regexp="(.*)/details" replace="\1" />
<move file="#{detailsDir}" toFile="${output.dir}/new" />
</sequential>
</for>
</target>
I am getting the following exception when running an applet:
Exception in thread "AWT-EventQueue-4" java.lang.NoClassDefFoundError: ice/net/SnapshotCacheManager
but the file is inside the jar.
I searched online and found it might be related to the applet not looking in the current directory and i need to add .; to the CLASSPATH but i am not sure how to add it to the build.xml
Thanks
Doron
Edit: Finally I figured it out, it wasn't an ant problem or the build XML, I got this exception because I signed two jars containing the same package differently, so there was a collision, not a very informative exception....
it might be useful to see what is in your current build.xml file, but the section you probably want to look at is the <target> element specifically the <src path> and <fileset> elements. Here is a VERY rough example with some guiding variables.
<property name="classes.home" value="/myproject/src"/>
<target name="compile_myproject" depends="clean">
<javac destdir="${classes.home}" debug="off" optimize="on" deprecation="on">
<classpath>
<fileset dir="/location/of/jars/">
<include name="*.jar"/>
<exclude name="jar-I-dont-want.jar"/>
</fileset>
<fileset dir="/location/of/axis2/jars">
<include name="**/*.jar"/>
</fileset>
</classpath>
<src path="${classes.home}"/>
<include name="/test/**/*.java"/>
<include name="other/location/*.java"/>
<exclude name="/debug/and/useless/files/**/*.java"/>
</javac>
</target>
note that ${classes.home} is a special variable defined at the top of the build.xml file. Many variables can be used to make things easier and specify relative paths.
I have the following in my build.xml:
<target name="compile.baz" depends="init">
<javac destdir="${build.dir}/classes" debug="on">
<compilerarg value="-Xlint:deprecation"/>
<src>
<pathelement location="${src.dir}/com/foo/bar/baz/" />
<pathelement location="${src.dir}/com/foo/bar/quux/" />
<!-- Need to exclude ${src.dir}/com/foo/bar/quux/dontwant/ -->
</src>
<classpath refid="classpath.jars" />
</javac>
...
</target>
This mostly does what I want, except that (as the comment says) I do not want the files in
${src.dir}/com/foo/bar/quux/dontwant/ to be compiled by this task (but I do want everything else under ${src.dir}/com/foo/bar/quux/ to be compiled in this task).
I'm a complete ant n00b, and the documentation hasn't been much help to me. I see several places where it says there are various exclude/excludes elements/attributes, but every variation I can think of either has no effect or results in an error like "blah doesn't support the 'exclude' attribute".
A couple of people suggested using <exclude>. This didn't work with the way my task was specified. trashgod's answer linked to the sixth example on this page which gave me an idea of how to restructure my task specification.
It looks like my problem was related to the way I was specifying the source files. Rather than using <pathelement> elements in a <src>, like this:
<src>
<pathelement location="${src.dir}/com/foo/bar/baz/" />
<pathelement location="${src.dir}/com/foo/bar/quux/" />
</src>
I switched to using a single <src> with a path and then a set of <include> elements, like this:
<src path="${src.dir}" />
<include name="com/foo/bar/baz/**" />
<include name="com/foo/bar/quux/**" />
This appears to be functionally identical, but is compatible with the use of <exclude>:
<exclude name="${src.dir}/com/foo/bar/quux/dontwant/**"/>
(Actually, I'm surprised that what was there in the first place worked at all.)
From my experiments you should not include full path for file you want to exclude.
This one doesn't work:
<javac>
(...>
<exclude name="${src.dir}/com/foo/blah/blah1/FILENAME.java"/>
(...)
</javac>
but this one does:
<javac>
(...>
<exclude name="com/foo/blah/blah1/FILENAME.java"/>
(...)
</javac>
Try
<javac>
(...>
<exclude name="${src.dir}/com/foo/bar/quux/dontwant/*" />
(...)
</javac>
I'm not sure about the rest, but the <exclude/> nested element should work in the Javac task. See the sixth example down.
Addendum: Patterns, including the ** notation, are discussed in Directory-based Tasks.
<target name="compile.baz" depends="init">
<javac destdir="${build.dir}/classes" debug="on">
<compilerarg value="-Xlint:deprecation"/>
<src>
<pathelement location="${src.dir}/com/foo/bar/baz/" />
<pathelement location="${src.dir}/com/foo/bar/quux/" />
</src>
<exclude name="${src.dir}/com/foo/bar/quux/dontwant/**"/>
...
</javac>
...
</target>
If you are trying to exclude Java classes but Ant is still trying to compile them then this may be due to references to these classes in your code.
If A.java has a reference to B.java and you try to exclude B.java then compiling A.java will require that B.java is compiled too.
This is one reason for interfaces in Java, so you can compile the interface without needing to compile the implementations.
try
folder "test" under {source.dir} would not be complied
<javac destdir="${compile.dir}" ...>
<src path="${source.dir}" />
<exclude name="test/**"/>
</javac>