Ant identifiying the a line above the current line - ant

So i can retrieve a certain line in a file. Now i want to check the values in the line just above this line. How do i go about doing this in ant?

The following example uses the Groovy ANT task to print the 3rd and 4th line of every Java file.
<project name="demo" default="run">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy">
<classpath>
<pathelement location="lib/groovy-all-2.1.0-rc-1.jar"/>
</classpath>
</taskdef>
<target name="run">
<fileset id="javafiles" dir="src" includes="**/*.java"/>
<groovy>
ant.project.references.javafiles.each {
def lines = new File(it.toString()).readLines()
ant.echo lines[2]
ant.echo lines[3]
}
</groovy>
</target>
</project>

Related

use ant1.7.1.jar as external library in ant build -Error

My ant versions is ANT1.6 , this is Appserver customized versionn, which has some AppServer specific customized Tasks defined and it is preventing me from upgrading ANT as it is tightly coupled with AppServer upgrade, We have a requirement where user passes a list of files and I have to check if that files exists in a target location , if they exist then delete them and if they do not exist then throw a build failure error. After some troubleshooting I have decided to use ANT1.7 as an external library , below is my code snippet for validating if the file exists
<target name="validate.file" depends="defineResource,validate.dir">
<echo message = " The Filelist is : ${file.list} "/>
<condition property="is.missing">
<resourcecount when="ne" count="0">
<difference id="is.missing">
<intersect>
<filelist id="required" dir="${target.location}" files="${file.list}"/>
<fileset id="existing" dir="${target.location}" includes="*.*"/>
</intersect>
<filelist refid="required"/>
</difference>
</resourcecount>
</condition>
<fail if="is.missing" message= " File ${toString:missing} is missing from the list of files provided for removing, please recheck and submit correct "/>
</target>
I am using taskdef to load the ANT1.7.1.jar and Tried the below snippet
1 Approach
<target name = "defineResource">
<echo message = "Present in Resource Class "/>
<taskdef name="resourcecount" classname="org.apache.tools.ant.taskdefs.ResourceCount">
<classpath>
<pathelement location="/jass/deploy-process/deploy-engine/build/lib/apache-ant-1.7.1.jar"/>
</classpath>
</taskdef>
<taskdef name="difference" classname="org.apache.tools.ant.types.resources.Difference">
<classpath>
<pathelement location="/jass/deploy-process/deploy-engine/build/lib/apache-ant-1.7.1.jar"/>
</classpath>
</taskdef>
<taskdef name="intersect" classname="org.apache.tools.ant.types.resources.Intersect">
<classpath>
<pathelement location="/jass/deploy-process/deploy-engine/build/lib/apache-ant-1.7.1.jar"/>
</classpath>
</taskdef>
</target>
2nd Approach
<target name = "defineResource">
<echo message = "Present in Resource Class "/>
<taskdef name="resourcecount" classname="org.apache.tools.ant.taskdefs.ResourceCount,org.apache.tools.ant.types.resources.Difference,org.apache.tools.ant.types.resources.Intersect">
<classpath>
<pathelement location="/jass/deploy-process/deploy-engine/build/lib/apache-ant-1.7.1.jar"/>
</classpath>
</taskdef>
</target>
<target name = "defineDifference">
<echo message = "Present in Difference Class "/>
<taskdef name="difference" classname="org.apache.tools.ant.types.resources.Difference">
<classpath>
<pathelement location="/jass/deploy-process/deploy-engine/build/lib/apache-ant-1.7.1.jar"/>
</classpath>
</taskdef>
</target>
<target name = "defineIntersect">
<echo message = "Present in Intersect Class "/>
<taskdef name="intersect" classname="org.apache.tools.ant.types.resources.Intersect">
<classpath>
<pathelement location="/jass/deploy-process/deploy-engine/build/lib/apache-ant-1.7.1.jar"/>
</classpath>
</taskdef>
</target>
But none of my approach to use Taskdef is working , they are throwing build failure error
No public execute() in class org.apache.tools.ant.types.resources.Difference
No public execute() in class org.apache.tools.ant.types.resources.Intersect
I am really confused on how to fix this , Please advice me if I am doing the right approach or else if there is any other way that i can achieve my actual requirement to search the files from file list.

Get directories with modified files with Ant

I need to process a directory if it has at least one modified file in it. I wrote a block that reduces a fileset to a unique list of the directories that contain those files, but I think this would be easier if there was a way to do this without the script.
Is there a way?
Tricky to do this with core ANT.
Here's an example using an embedded groovy script:
<project name="demo" default="process-modified-dirs">
<path id="build.path">
<pathelement location="/path/to/groovy-all/jar/groovy-all-2.1.1.jar"/>
</path>
<fileset id="modifiedfiles" dir="src">
<modified/>
</fileset>
<target name="process-modified-dirs">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
<groovy>
dirs = project.references.modifiedfiles.collect {
new File(it.toString()).parent
}
dirs.unique().each {
ant.echo("Do something with this dir: ${it}")
}
</groovy>
</target>
</project>

How to <foreach> in a <macrodef>?

I have a xml just like below:
<data>
<foo>value1</foo>
<foo>value2</foo>
<foo>value3</foo>
</data>
I want to create macrodef which implements below function:
<?xml version="1.0"?>
<project name="OATS" default="execute" basedir=".">
<xmlproperty file="data.xml" collapseAttributes="true"/>
<target name="execute">
<foreach list="${data.foo}" target="runScript" param="script"/>
</target>
<target name="runScript">
<echo>Doing things with ${script}</echo>
</target>
</project>
Anybody knows how to ? Thanks in advance.
xmltask is the best choice in the Ant community for this purpose, and you don't have to define your own macrodef.
So for instance:
<tools:xmltask source="data.xml" report="false" >
<tools:call path="data/foo">
<param name="value" path="text()"/>
<actions>
<echo>Doing things with #{value}</echo>
</actions>
</tools:call>
</tools:xmltask>
I encourage you to read the user manual, for xmltask has lots of options. It basically supports XPath to extract and iterate any portion of your xml. It also supports calls to existing targets in addition to anonymous code blocks (as in the example).
It's just hard to beat.
The following example uses the groovy ANT task
<project name="OATS" default="execute" basedir=".">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy">
<classpath>
<pathelement location="lib/groovy-all-2.1.0-rc-2.jar"/>
</classpath>
</taskdef>
<target name="execute">
<groovy>
def data = new XmlSlurper().parse(new File("data.xml"))
data.foo.each {
properties["script"] = it
ant.project.executeTarget("runScript")
}
</groovy>
</target>
<target name="runScript">
<echo>Doing things with ${script}</echo>
</target>
</project>
This is my macrodef.
<?xml version="1.0" encoding="UTF-8"?>
<project name="OATS" default="test" basedir=".">
<property environment = "env"/>
<path id = "antcontrib.path">
<fileset file = "${env.ANT_HOME}/../net.sf.antcontrib_1.1.0.0_1-0b2/lib/ant-contrib.jar"/>
</path>
<taskdef resource="net/sf/antcontrib/antlib.xml" classpathref="antcontrib.path"/>
<macrodef name="runOATS">
<attribute name="suite"/>
<attribute name="toDir"/>
<sequential>
<delete dir="#{toDir}"/>
<mkdir dir="#{toDir}"/>
<xmlproperty file="#{suite}" collapseAttributes="true"/>
<for list="${data.foo}" param="script">
<sequential>
<runScript script="#{script}"/>
</sequential>
</for>
</sequential>
</macrodef>
<macrodef name="runScript">
<attribute name="script"/>
<sequential>
<echo>Doing things with #{script}</echo>
</sequential>
</macrodef>
<target name="test">
<runOATS toDir="/OATS/results" suite="data.xml"/>
</target>
</project>

No output from Checkstyle in ANT

I am not using an automated build tool. Just Checkstyle 5.5 and ANT 1.8.
I am trying to have Checkstyle run in my ANT script. The ANT script executes without error, but doesn't seem to call Checkstyle. I get no output except ANT reports BUILD SUCCESSFUL.
Here is my ant script:
<project name="ccu" xmlns:cs="antlib:com.puppycrawl.tools.checkstyle">
<target name="checkstyle" description="Generates a report of code convention violations.">
<cs:checkstyle config="custom_check.xml">
<fileset dir="src" casesensitive="yes">
<include name="**/*.java"/>
</fileset>
<!--
<fileset dir="src" includes="**\*.java"/>
-->
</cs:checkstyle>
</target>
</project>
what am i missing?
It was a classpath problem. For some reason I needed to direct the ANT classpath to the class files not the jar.
My final script looks like this:
<project name="ccu" xmlns:cs="antlib:com.puppycrawl.tools.checkstyle">
<taskdef resource="checkstyletask.properties">
<classpath>
<pathelement location="C:\myClasses\bin"/>
<pathelement location="C:\checkstyle-5.5\checkstyle-5.5-all.jar"/>
</classpath>
</taskdef>
<checkstyle config="custom_check.xml">
<fileset dir="src" includes="**/*.java"/>
</checkstyle>
</project>

path definition not recognized in ant

I defined a path in a file called unittest.xml in the following way(line 26):
<path id="tasks.path">
<pathelement location="${publish.home}/INSIDE/UnitTest/testinganttasks.jar"/>
</path>
next I tried to use the path in the following way:
<classpath>
<path refid="tasks.path" />
</classpath>
in a taskdef tag.
when I run my ant code, it does everything well until I get the following error:
BUILD FAILED unittest.xml:296: The
following error occurred while
executing this line: unittest.xml:281:
Reference tasks.path not found.
How can I resolve this issue?
You should see this example at http://ant.apache.org/manual/using.html#references
<project ... >
<path id="project.class.path">
<pathelement location="lib/"/>
<pathelement path="${java.class.path}/"/>
<pathelement path="${additional.path}"/>
</path>
<target ... >
<rmic ...>
<classpath refid="project.class.path"/>
</rmic>
</target>
<target ... >
<javac ...>
<classpath refid="project.class.path"/>
</javac>
</target>
</project>
I think your problem is that in classpath you should not nest path element, but give id of the path for the classpath element itself.

Resources