I am writing a javascript ant task using scriptdef:
<scriptdef name="xxx" language="javascript">
<element name="dirset" type="dirset"/>
<![CDATA[
importClass(java.io.File);
var dirsets = elements.get("dirset");
var t = dirsets.get(0);
self.log(t.indexOf("T"));
]]>
</scriptdef>
When I run this script, it complains that it can't find the indexOf function. Any ideas?
You can use the pathconvert ANT task:
<pathconvert property="dirs.str" pathsep=" " refid="dirset.id"/>
Related
I have a requirement to get a substring out of a string in to ant property.
Example string:
1=tibunit-1.4.2.projlib\=
I want to extract the part before .projlib\= and after the first =.
The result should be:
tibunit-1.4.2
Any ideas?
Use script task with builtin javascript engine (JDK >= 1.6.0_06) and something like :
if you need substring 'tibunit-1.4.2.projlib\' :
<project>
<property name="foo" value="1=tibunit-1.4.2.projlib\="/>
<script language="javascript">
// simple echo
println(project.getProperty('foo').split('=')[1]);
// create property for later use
project.setProperty('foobar', project.getProperty('foo').split('=')[1]);
</script>
<echo>$${foobar} => ${foobar}</echo>
</project>
output :
[script] tibunit-1.4.2.projlib\
[echo] ${foobar} => tibunit-1.4.2.projlib\
if you need substring 'tibunit-1.4.2' :
<project>
<property name="foo" value="1=tibunit-1.4.2.projlib\="/>
<script language="javascript">
s = project.getProperty('foo').split('=')[1];
// simple echo
println(s.substring(0, s.lastIndexOf(".")));
// create property for later use
project.setProperty('foobar', s.substring(0, s.lastIndexOf(".")));
</script>
<echo>$${foobar} => ${foobar}</echo>
</project>
output:
[script] tibunit-1.4.2
[echo] ${foobar} => tibunit-1.4.2
For reuse put that stuff into a macrodef.
Use the ant-contrib task PropertyRegex :
<propertyregex property="tibunit.version"
input="1=tibunit-1.4.2.projlib\="
regexp="1=(tibnunit-[0-9]+.[0-9]+.[0-9]+).+"
select="\1"
casesensitive="false" />
I need to calculate time difference using Ant.
Basically it has 2 variables. One is assigned the current time, and the other one has a different time. I need to get the time difference using Ant. Something like below. If anyone have code please reply.
variable a = current time;
variable b = different time
echo (a - b) ;
Here is a much simpler solution:
<script language="javascript">
project.setProperty('startTime', new Date().getTime());
</script>
...
<script language="javascript">
project.setProperty('elapsedTime', new Date().getTime() - startTime)
</script>
<echo>Elapsed time: ${elapsedTime} ms</echo>
Alternative to #LeFunes answer (and uses the tstamp task)
<tstamp prefix="task.start">
<format property="millis" pattern="SSS"/>
</tstamp>
<tstamp prefix="task">
<format property="start" pattern="E, dd MMM YYYY hh:mm:ss"/>
</tstamp>
<time-consuming-task/>
<tstamp prefix="task.stop">
<format property="millis" pattern="SSS"/>
</tstamp>
<tstamp prefix="task">
<format property="stop" pattern="E, dd MMM YYYY hh:mm:ss"/>
</tstamp>
<script language="javascript">
project.setProperty("task.diff",
Math.abs(
(Date.parse(project.getProperty("task.stop")) +
+project.getProperty("task.stop.millis")) -
(Date.parse(project.getProperty("task.start")) +
+project.getProperty("task.start.millis"))))
</script>
<echo>
task completed in ${task.diff} ms
</echo>
NOTE:
this doesn't consider milliseconds
updated to consider the milliseconds
<?xml version="1.0" encoding="UTF-8"?>
<project name="TEST ANT" default="test" basedir="..">
<target name="test" description="">
<script language="javascript"> <![CDATA[
var ts1 = new Date();
project.setProperty("current.time.1", ts1.toLocaleString());
project.setProperty("current.time.1.mill", ts1.getTime());
]]></script>
<echo>Timestamp 1: ${current.time.1} [${current.time.1.mill}]</echo>
<sleep milliseconds="1300"></sleep>
<script language="javascript"> <![CDATA[
var ts2 = new Date();
project.setProperty("current.time.2", ts2.toLocaleString());
project.setProperty("current.time.2.mill", ts2.getTime());
]]></script>
<echo>Timestamp 2: ${current.time.2} [${current.time.2.mill}]</echo>
<script language="javascript"> <![CDATA[
project.setProperty("res", project.getProperty("current.time.2.mill")-project.getProperty("current.time.1.mill"));
]]></script>
<echo>Diff: ${res}</echo>
</target>
</project>
If you don't want to use JavaScript, you can use the Math task provided by the Ant-Contrib utilities.
The Ant-Contrib are fairly old, and I don't know if anyone is still maintaining them, but they're very popular to use in Ant build files since they add some very useful tasks.
I recommend including the ant-contrib-1.0b3.jar into the project itself. When other people use your project, they'll also have the Ant-Contrib jar. I put ant-lib/ac/ant-contrib-1.0b3.jar under your project's home directory. I use ant-lib for all of my optional jars:
<project name="my.project"
...
xmlns:ac="antlib:net.sf.antcontrib">
...
<taskdef uri="antlib:net.sf.antcontrib"
resource="net/sf/antcontrib/antlib.xml">
<classpath>
<fileset dir="${basedir}/antlib/ac"/>
</classpath>
</taskdef>
....
Now, you can use your math task like this:
<ac:math result="time.diff"
operation="-"
operand1="${diff.time}"
operand2="${initial.time}"/>
The ac: is an XML namespace that was declared in your <project/> entity, and was connected to your tasks via the uri parameter in the <taskdef/> entity. This allows you to have multiple optional Ant tasks that may have tasks with duplicate names. This is a good idea in case you use multiple optional task libraries that have the same task names.
I have two properties in Ant that both contain integers. I want to check if one is greater than the other. How can I accomplish that? Is there a way to use subtraction in ant? Then I could just subtract the two and check if the result is greater than 0.
Thanks!
You can try to use this sample:
<scriptdef name="intCompare" language="javascript">
<attribute name="leftside"/>
<attribute name="rightside"/>
<attribute name="diff"/>
<![CDATA[
var leftSide = attributes.get("leftside");
var rightSide = attributes.get("rightside");
project.setProperty(attributes.get("diff"), leftSide-rightSide);
]]>
</scriptdef>
<target name="test">
<intCompare leftside="555" rightside="9" diff="deviation"/>
<echo message="The difference is: ${deviation}"/>
</target>
Use a groovy task
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
<groovy>
properties["greater"] = properties["x"] > properties["y"]
</groovy>
How can I find out which of two numeric properties is the greatest?
Here's how to check wheather two are equal:
<condition property="isEqual">
<equals arg1="1" arg2="2"/>
</condition>
The Ant script task allows you to implement a task in a scripting language. If you have JDK 1.6 installed, Ant can execute JavaScript without needing any additional dependent libraries. For example, this JavaScript reads an Ant property value and then sets another Ant property depending on a condition:
<property name="version" value="2"/>
<target name="init">
<script language="javascript"><![CDATA[
var version = parseInt(project.getProperty('version'));
project.setProperty('isGreater', version > 1);
]]></script>
<echo message="${isGreater}"/>
</target>
Unfortunately, Ant's built in condition task does not have an IsGreaterThan element. However, you could use the IsGreaterThan condition available in the Ant-Contrib project. Another option would be to roll out your own task for greater than comparison. I'd prefer the former, because it's easier and faster, and you also get a host of other useful tasks from Ant-Contrib.
If you don't want to (or cannot) use the Ant-Contrib libraries, you can define a compare task using javascript:
<!-- returns the same results as Java's compareTo() method: -->
<!-- -1 if arg1 < arg2, 0 if arg1 = arg2, 1 if arg1 > arg2 -->
<scriptdef language="javascript" name="compare">
<attribute name="arg1" />
<attribute name="arg2" />
<attribute name="result" />
<![CDATA[
var val1 = parseInt(attributes.get("arg1"));
var val2 = parseInt(attributes.get("arg2"));
var result = (val1 > val2 ? 1 : (val1 < val2 ? -1 : 0));
project.setProperty(attributes.get("result"), result);
]]>
</scriptdef>
You can use it like this:
<property name="myproperty" value="20" />
...
<local name="compareResult" />
<compare arg1="${myproperty}" arg2="19" result="compareResult" />
<fail message="myproperty (${myproperty}) is greater than 19!">
<condition>
<equals arg1="${compareResult}" arg2="1" />
</condition>
</fail>
How can I get the value of the current target ant?
Does it exist a special variable something called TARGET?
Based on the issue you have to patch ant or used javascript:
<target name="test">
<script language="javascript">
project.setNewProperty("current_target", self.getOwningTarget());
</script>
<echo>${current_target}</echo>
</target>
In ant 1.8.2 you can use ${ant.project.invoked-targets}
However, looking at the commit logs
http://svn.apache.org/viewvc?view=revision&revision=663061
I'm guessing its been available since 1.7.1
My answer, using antcontrib
<macrodef name="showtargetname">
<attribute name="property"/>
<sequential>
<!-- make temporary variable -->
<propertycopy name="__tempvar__" from="#{property}"/>
<!-- Using Javascript functions to convert the string -->
<script language="javascript"> <![CDATA[
currValue = [project-name].getThreadTask(java.lang.Thread.currentThread()).getTask().getOwningTarget().getName();
[project-name].setProperty("__tempvar__", currValue);
]]>
</script>
<!-- copy result -->
<var name="#{property}" value="${__tempvar__}"/>
<!-- remove temp var -->
<var name="__tempvar__" unset="true"/>
</sequential>
</macrodef>
Usage:
<showtargetname property="mycurrenttarget"/>
I think you can't, unless you spend some time coding your own custom tasks (http://ant.apache.org/manual/tutorial-writing-tasks.html)
The built-in properties you can display are: basedir, ant.file, ant.version, ant.project.name, ant.java.version
If you run ant using the -projecthelp arg:
ant -projecthelp
you will get a listing of the main targets specified in the build.xml (or other build file as declared on the commandline).