how to invoke macrodef in cumstom ant task with params? - ant

I want to create a custom ant task, it reads from a file to get a list params, then traverse list to invake macrodef with list item as param.
how to invoke macrodef in cumstom ant task with params?

From within your custom task, you can call
org.apache.tools.ant.taskdefs.MacroInstance macro = (org.apache.tools.ant.taskdefs.MacroInstance) getProject().createTask("myMacroName");
macro.setDynamicAttribute("name", value);
macro.execute();

Related

Display Ant options, properties specified in invocation

I have an Ant buildfile (build.xml) which is called by some application. I would like to know exactly what kind of properties are used to invoke Ant. Therefore I would like to modify the build.xml file to display all properties specified in the call, e.g.:
ant aTarget -Dxslt.parser=SAXON -Dbasedir=aFolder
would display list as below
- target: aTarget
- xslt.parser = SAXON
- basedir=aFolder
Please note that I do not know exactly what is being using to invoke Ant. Therefore, I need to use some sort of a loop get all properties, options.
The simplest thing that comes to mind is to place a line like:
<echo message="Ant invocation is '${sun.java.command}'" />
In the buildfile outside of any target. It'll look something like:
% ant aTarget -Dx=y
[echo] ant invocation is: 'org.apache.tools.ant.launch.Launcher -cp . aTarget -Dx=y'
It shows you what was passed to the Ant Launcher, which might will likely be a little more than what was passed to the ant wrapper script, but should do.
I would avoid trying to parse the line, as you say, you don't know what might be there, and it could quickly get complicated.
Take a look at the <echoproperties> task:
<property name="in.file.prop" value="value2"/>
<echoproperties/>
in.file.prop and its value will be printed. However, over 60 other properties will be printed as well including properties built into Ant.
You can save the results of <echoproperties> to a file and then filter that file with something like a <linecontains> filter.

What's the alternative to ant: waitfor task if I need to look for a file with a pattern?

I have to look for a .xls(say test-results.xls) in a given folder. If not available, it should wait for 60 minutes for *.xls creation before timing-out. The test-execution target will meanwhile create this test result file.
I am trying to use ant: waitfor task and the associate "available" task for filename. But "available" task expects a specific filename(eg: test-results.xls). I can't have that as the file is appended with time-stamp(eg: test-result_08-22-2012 9:45 PM.xls). I tried using fileset task but it says fileset can't be used within waitfor task.
I have to use ant: waitfor task and look for a file with a particular pattern(say: test-result*.xls or *.xls). Please let me know if that's possible or is there an alternative to waitfor task for this particular scenario?
Hi you can use this combination ant-contrib PropertyRegex and waitfor task. We have used this combination for our requirement which is similar to yours.

Can you execute an Ant task from within a custom Ant task?

I am writing a custom Ant task to handle some business logic, and I want to call the Move Ant task.
I presume I would use the Execute java class, but I'm having trouble figuring it out.
Am I going in the right direction, or can you not call one class from another in an Ant way?
For your task, you wanna call the Move task. So you want to move some files.
Generally, when you are coding in Java, you should avoid calling built-in Ant tasks, as they usually have poor performance. For example, you can either use File.renameTo() method from java.io.File or Files.move() from java.nio.file.Files (JDK7 only).
If you do want to take advantage of some Ant things, like <fileset>, you may want your task to be able to take Move task as a nested task in build xml. Just make your task a TaskContainer and do some filtering in its method.
Of course, you can also just import the Move task, set all the needed properties, and the call its execute() method. But I do not like that.
The above answer does not answer the original question "Can you execute an Ant task from within a custom Ant task?"
The answer is definitely yes.
Here is an example with Copy. Move is similar. And while File.renameTo() may have better performance than calling an Ant Move task, there are definitely reasons why you might want to invoke an Ant task from within a custom Ant task, especially in cases where no suitable JRE alternative exists. The Ant API is very extensive and provides a great number of useful tasks. Furthermore, you may want invoke a custom Ant task from within a different custom Ant task.
See also http://www.jajakarta.org/ant/ant-1.6.1/docs/ja/manual/api/org/apache/tools/ant/taskdefs/package-summary.html
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.taskdefs.Copy;
...
// set up the Strings inputPath and outputPath appropriately...
...
try{
FileSet inputSet = new FileSet(); // what to copy FROM
inputSet.setDir(new File(inputPath));
Copy copyTask = new Copy();
copyTask.setProject(getProject());
copyTask.setTodir(outputPath); // where to copy TO
copyTask.setFailOnError(true);
copyTask.addFileset(inputSet);
copyTask.execute();
}
catch(Exception e){
String msg = "Exception while invoking Copy task with inputPath=\"" + inputPath + "\" and outputPath=\"" + outputPath + "\": " + e.toString();
throw new BuildException(msg, e);
}

How to change ant property location attribute with .properties file

Is it possible to change ant property location attribute with .properties file like
property value attribute?
ant.xml
<property name="images" location="some_location" />
ant.properties
name=D:\images
See ant manual propertyfile => there is no attribute called location.
The location attribute from property task is only a 'special' case of a value, that knows how to deal with absolute and relative pathes.
If you need to edit | overwrite existing property values(locations) use either :
ant script task (groovy or groovy task recommended)
or some Ant addon like Flaka or Antcontrib, providing tasks for that purpose.
Declaring a property within the build file will override the same value imported from a properties file.
The only way to do this is setting the property value on the command-line as follows:
ant -Dimages=D:\images

How can I use an Ant foreach iteration with values from a file?

In our Ant build environment, I have to do the same task for a number of items. The AntContrib foreach task is useful for that. However, the list is in a parameter, where I actually have the list in a file. How can I iterate over items in a file in an foreach-like way in Ant? Something like (pseudo-code):
<foreach target="compile-module" listFromFile="$fileWithModules"/>
I'm happy to write a custom Task, and welcome any suggestion on possible solutions.
I tried to load the file into a property and iterate over it, worked fine for me:
<loadfile property="file-content" srcFile="${fileWithModules}"/>
<foreach
target="compile-module"
list="${file-content}"
delimiter="${line.separator}"
param="your-param-used-in-target"/>

Resources