Read Ant properties file generated by Apache FOP - ant

I am new to Ant. I am trying to read two property files, The first is static and second is created during the build process. Please see below.
There is one static property file which I read in at the top: ./cfg/build.properties.
Then again reading another property file inside a target tag. The flow is described below.
There are two targets which will get executed in sequences.
First I am trying to create a property file using FOP in target GENERATE_PROPERTYFILE.
Then on the second target, READ_PROPERTY_FILE_GENERATE_XML, I am reading the property file created in the first step.
But the issue is, it is not picking a value for ${IssueObjects.ID} from the second property file.
Below is the snapshot of script.
<project name="fop4ant" default="run" basedir=".">
<property file="./cfg/build.properties" prefix="System"/>
<property environment="env"/>
<tstamp>
<format property="param_TouchTimeStamp" pattern="yyyyMMddHHmmssSS"/>
</tstamp>
<property name="param_TouchTimeStamp" value="" />
<property name="prop_File_XSL_FetchProbNSolObjects" value="${System.prop_Dir_stylesheet}/${System.prop_File_Stylesheet_FetchProbSol_CNReport}" />
<property name="prop_File_XML" value="${System.prop_Dir_temp}/Object-${param_TouchTimeStamp}.xml" />
<property name="prop_File_XML_Prob" value="${System.prop_Dir_temp}/Prob-${param_TouchTimeStamp}.xml" />
<property name="prop_File_COIDs" value="${System.prop_Dir_temp}/probCO-${param_TouchTimeStamp}.properties" />
<property name="prop_Dir_FOP" value="${env.FOP_HOME}"/>
<property name="prop_Dir_GS" value="${env.GS_HOME}"/>
<taskdef name="fop"
classname="org.apache.fop.tools.anttasks.Fop">
<classpath>
<fileset dir="${prop_Dir_FOP}/lib">
<include name="*.jar"/>
</fileset>
<fileset dir="${prop_Dir_FOP}/build">
<include name="fop.jar"/>
</fileset>
</classpath>
</taskdef>
<!-- #SECTION_BEGIN :: READ_PROPERTY_FILE_GENERATE_XML -->
<target name="generate-problem-item-productview">
<echo message="prop_XML_FILE_PROB :: ${prop_File_XML_Prob}" level="info" />
<!--Reading dynamically created property file-->
<property file="$prop_File_COIDs" prefix="IssueObjects"/>
<exec executable="export.exe">
<arg line="-xml_file=${prop_File_XML_Prob} -transfermode=${IssueObjects.ID}">
</exec>
</target>
<!-- #SECTION_END :: READ_PROPERTY_FILE_GENERATE_XML -->
<!-- #SECTION_BEGIN :: GENERATE_PROPERTYFILE-->
<target name="fetch-prob-sol-items">
<echo message="prop_File_XML :: ${prop_File_XML}" level="info" />
<echo message="prop_File_XSL_FetchProbNSolObjects :: ${prop_File_XSL_FetchProbNSolObjects}" level="info" />
<echo message="prop_File_COIDs :: ${prop_File_COIDs}" level="info" />
<echo message="prop_Dir_FOP :: ${prop_Dir_FOP}/fop.bat" level="info" />
<exec executable="${prop_Dir_FOP}/fop.bat">
<arg value="-xml"/>
<arg value="${prop_File_XML}"/>
<arg value="-xsl"/>
<arg value="${prop_File_XSL_FetchProbNSolObjects}"/>
<arg value="-foout"/>
<arg value="${prop_File_COIDs}"/>
</exec>
</target>
<!-- #SECTION_END :: GENERATE_PROPERTYFILE -->
<target name="run" depends="">
<echo message="start :: run" level="info" />
<antcall target="fetch-prob-sol-items" />
<antcall target="generate-problem-item-productview" />
<echo message="end :: run" level="info" />
</target>
</project>

In the READ_PROPERTY_FILE_GENERATE_XML section, replace the following...
<property file="$prop_File_COIDs" prefix="IssueObjects"/>
...with...
<property file="${prop_File_COIDs}" prefix="IssueObjects"/>
In the above example, curly braces have been added around the prop_File_COIDs reference.

Related

How to develope a method to substitute repetitive code block

I am new in ant, so I wasn't able to find an approach to make my buld file a bit more elegant. I believe there is an approach to substitute repetitive block of code into my build. So here is build file:
<project basedir="../../../" name="do-report" default="zip-all">
<xmlproperty keeproot="false" file="implementation/xml/ant/properties.xml"/>
<!-- -->
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${infrastructure-base-dir}/apache-ant-1.9.6/lib/ant-contrib-0.3.jar"/>
</classpath>
</taskdef>
<!-- -->
<target name="clean">
<delete dir="${dita-odt.path.odt-unzipped-base-dir}" includeemptydirs="true" failonerror="no"/>
<delete dir="examples/intermediate/odt-files" includeemptydirs="true" failonerror="no"/>
</target>
<!-- -->
<target name="unzip-writing-odt-file" depends="clean">
<unzip src="${dita-odt.path.writing-odt}" dest="${dita-odt.path.writing-odt-unzipped}"/>
</target>
<!-- -->
<target name="extract-common-paths" depends="unzip-writing-odt-file">
<foreach target="copy-text-path" param="file">
<path>
<fileset dir="${dita-odt.path.text-xml-base-dir}">
<include name="**/content.xml"/>
</fileset>
</path>
</foreach>
</target>
<!-- -->
<target name="copy-text-path" description="copy text-xml path relative to text-xml-base-dir">
<dirname property="text-absolute-dir-path" file="${file}"/>
<property name="absolute-path-text-base-dir" location="${dita-odt.path.text-xml-base-dir}"/>
<pathconvert property="common-path" dirsep="/">
<path location="${text-absolute-dir-path}"/>
<map from="${absolute-path-text-base-dir}/" to=""/>
</pathconvert>
<antcall target="copy-writing-unzipped">
<param name="common-path" value="${common-path}"/>
</antcall>
</target>
<!-- -->
<target name="copy-writing-unzipped">
<echo>${common-path}</echo>
<copy todir="${dita-odt.path.odt-unzipped-base-dir}/${common-path}">
<fileset dir="${dita-odt.path.writing-odt-unzipped}">
<include name="**/*"/>
</fileset>
</copy>
</target>
<!-- -->
<target name="transform-all" depends="extract-common-paths">
<foreach target="transform" param="file">
<path>
<fileset dir="${dita-odt.path.text-xml-base-dir}">
<include name="**/content.xml"/>
</fileset>
</path>
</foreach>
</target>
<!-- -->
<target name="transform">
<basename property="file-base-name" file="${file}"/>
<dirname property="file-dir-absolute-path" file="${file}"/>
<property name="text-xml-base-dir-absolute-path" location="${dita-odt.path.text-xml-base-dir}"/>
<pathconvert property="common-path" dirsep="/">
<path location="${file-dir-absolute-path}"/>
<map from="${text-xml-base-dir-absolute-path}/" to=""/>
</pathconvert>
<!--Substitutes backslashes with forword slashes. Basedir is a reserved property that returns absolute path with separator symbols of the current OS.-->
<pathconvert dirsep="/" property="base-dir-unix">
<path location="${basedir}"/>
</pathconvert>
<echo>TRANSFORM TO: ${dita-odt.path.odt-unzipped-base-dir}/${common-path}/${file-base-name}</echo>
<xslt in="${file}" out="${dita-odt.path.odt-unzipped-base-dir}/${common-path}/${file-base-name}" style="${dita-odt.path.text-odt-xsl}" extension=".xml" force="true">
<param name="dir-path-styles-xml" expression="${dita-odt.path.odt-unzipped-base-dir}/${common-path}"/>
<param name="project-base-dir-absolute-path" expression="${base-dir-unix}"/>
<classpath location="${infrastructure-base-dir}/${dita-odt.text-odt-xsl.processor}"/>
</xslt>
</target>
<!-- -->
<target name="zip-all" depends="transform-all" description="Turns all unzipped text folders into ODT files">
<foreach target="zip-odt" param="file">
<path>
<fileset dir="${dita-odt.path.odt-unzipped-base-dir}" includes="**/content.xml" excludes="writing/**"/>
</path>
</foreach>
</target>
<!-- -->
<target name="zip-odt">
<basename property="file-base-name" file="${file}"/>
<dirname property="file-dir-absolute-path" file="${file}"/>
<!--This property will be used to provided name for the produced ODT file. The document will have the same name as the folder that contains it.-->
<basename property="odt-doc-name" file="${file-dir-absolute-path}.odt"/>
<property name="odt-unzipped-base-dir-absolute-path" location="${dita-odt.path.odt-unzipped-base-dir}"/>
<pathconvert property="common-path" dirsep="/">
<path location="${file-dir-absolute-path}"/>
<map from="${odt-unzipped-base-dir-absolute-path}/" to=""/>
</pathconvert>
<echo>COMMON PATH: ${common-path}</echo>
<zip destfile="examples/intermediate/odt-files/${common-path}/${odt-doc-name}" basedir="${dita-odt.path.odt-unzipped-base-dir}/${common-path}" update="true"/>
</target>
<!-- -->
</project>
So this part of the script does pretty much the same, but shared among almost all the target in the project:
<dirname property="file-dir-absolute-path" file="${file}"/>
<property name="text-xml-base-dir-absolute-path" location="${dita-odt.path.text-xml-base-dir}"/>
<pathconvert property="common-path" dirsep="/">
<path location="${file-dir-absolute-path}"/>
<map from="${text-xml-base-dir-absolute-path}/" to=""/>
</pathconvert>
This part does nothing but to obtain part of a path. For example if ${file} stands for /folder/subfolder1/subfolder2 then take the path after /folder namely subfolder1/subfolder2 and assign it to a property. I this case that property is named common-path that holds same path for all the target. I examined MacroDef Task, but as far as I understand it doesn't return, only accepts some parameters in form of attributes. Anyway, any help would be much appreciated.
You are on the right track in considering <macrodef> to reduce repetitive code.
While it's true that <macrodef> doesn't return anything, <macrodef> can be given the name of a property to set. For example...
<macrodef name="my-hello">
<attribute name="person"/>
<attribute name="output-property"/>
<sequential>
<property name="#{output-property}" value="Hello, #{person}!"/>
</sequential>
</macrodef>
<my-hello person="Riko" output-property="say-hi-to-riko"/>
<echo>my-hello said: ${say-hi-to-riko}</echo>
...outputs...
[echo] my-hello said: Hello, Riko!
In this example, the caller of <my-hello> tells the macrodef to "return" its results in the say-hi-to-riko property.
Knowing this, several of the <target>s in your script can be converted to <macrodef>s that set properties...
<project name="ant-macrodef-pathconvert" default="extract-common-paths">
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<property name="dita-odt.path.text-xml-base-dir" value="C:\temp\dita-odt"/>
<macrodef name="my-pathconvert">
<attribute name="file"/>
<attribute name="common-path-property"/>
<sequential>
<!-- <local> allows multiple calls to a macrodef. -->
<local name="file-dir-absolute-path"/>
<echo>In my-pathconvert for #{file}</echo>
<dirname property="file-dir-absolute-path" file="#{file}"/>
<property name="text-xml-base-dir-absolute-path"
location="${dita-odt.path.text-xml-base-dir}"/>
<pathconvert property="#{common-path-property}" dirsep="/">
<path location="${file-dir-absolute-path}"/>
<map from="${file-dir-absolute-path}/" to=""/>
</pathconvert>
</sequential>
</macrodef>
<macrodef name="copy-text-path"
description="copy text-xml path relative to text-xml-base-dir">
<attribute name="file"/>
<sequential>
<local name="common-path"/>
<echo>In copy-text-path for #{file}</echo>
<my-pathconvert file="#{file}" common-path-property="common-path"/>
<copy-writing-unzipped common-path="${common-path}"/>
</sequential>
</macrodef>
<macrodef name="copy-writing-unzipped">
<attribute name="common-path"/>
<sequential>
<echo>In copy-writing-unzipped for #{common-path}</echo>
<echo>copy task goes here.</echo>
</sequential>
</macrodef>
<target name="extract-common-paths">
<for param="file">
<path>
<fileset dir="${dita-odt.path.text-xml-base-dir}">
<include name="**/content.xml"/>
</fileset>
</path>
<sequential>
<copy-text-path file="#{file}"/>
</sequential>
</for>
</target>
</project>
In general, it's better to prefer calling <macrodef>s over calling <target>s directly. In the above example, <foreach> is replaced with <for> because <for> lets us call <macrodef>s.
Output
[echo] In copy-text-path for C:\temp\dita-odt\dir1\content.xml
[echo] In my-pathconvert for C:\temp\dita-odt\dir1\content.xml
[echo] In copy-writing-unzipped for C:/temp/dita-odt/dir1
[echo] copy task goes here.
[echo] In copy-text-path for C:\temp\dita-odt\dir2\content.xml
[echo] In my-pathconvert for C:\temp\dita-odt\dir2\content.xml
[echo] In copy-writing-unzipped for C:/temp/dita-odt/dir2
[echo] copy task goes here.

Having directory reference trouble with Jenkins & Phing

I have set up my first CI environment with Jenkins and Phing and it is mostly running perfectly, though I have a few hiccups I can't resolve. I am constantly having trouble in determining how to reference directories within various parts of my build process, appreciate any thoughts on the below:
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="myProject" default="dev" basedir=".">
<property name="baseDir" value="." />
<property name="resourceDir" value="${baseDir}/resources" />
<property name="srcDir" value="${baseDir}/src" />
<property name="outputDir" value="${baseDir}/build" />
<property name="logDir" value="${baseDir}/build/logs" />
<property name="phpmdRulesets" value="${resourceDir}/phpmd/rulesets" />
<property name="devServer" value="C:\Apache24\htdocs\myProject.com" />
<target name="init">
<mkdir dir="${outputDir}" />
<mkdir dir="${logDir}" />
</target>
<target name="clean">
<delete dir="${devServer}" includeemptydirs="true" verbose="true" failonerror="true" />
<mkdir dir="${devServer}" />
</target>
<target name="test">
<echo msg="Running unit tests" />
<exec executable="phpunit" checkreturn="true">
<arg value="--log-junit=${logDir}/PHPUnit.xml" />
<arg value="--verbose" />
<arg value="--debug" />
<arg value="--coverage-clover=${logDir}/clover.xml" />
<arg path="${baseDir}" />
</exec>
</target>
<target name="build" depends="phpcs, phpmd, phpcpd, phpDox">
<echo msg="Running build" />
<echo msg="Copying files to build directory..." />
<copy todir="${outputDir}">
<fileset dir="${baseDir}">
<include name="**/src/**" />
<include name="**/resources/**" />
</fileset>
</copy>
</target>
<target name="phpcs">
<echo msg="Running phpcs" />
<exec executable="phpcs">
<arg value="--report=checkstyle" />
<arg value="--ignore=${srcDir}/php/Test/*,${srcDir}/php/lib/*" />
<arg value="--report-file=${logDir}/checkstyle.xml" />
<arg path="${srcDir}" />
</exec>
</target>
<target name="phpmd">
<echo msg="Running phpmd" />
<exec executable="phpmd">
<arg path="${srcDir}" />
<arg value="xml" />
<arg value="${phpmdRulesets}/codesize.xml,${phpmdRulesets}/unusedcode.xml,${phpmdRulesets}/naming.xml,${phpmdRulesets}/design.xml" />
<arg value="--reportfile ${logDir}/messdetector.xml" />
<arg value="--exclude ${srcDir}/php/Test/,${srcDir}/php/lib/" />
</exec>
</target>
<target name="phpcpd">
<echo msg="Running phpcpd" />
<exec executable="phpcpd">
<arg value="--log-pmd=${logDir}/phpcpd.xml" />
<arg value="-vvv" />
<arg value="--exclude=${srcDir}/php/Test/,${srcDir}/php/lib/" />
<arg path="${srcDir}" />
</exec>
</target>
<target name="phpDox">
<echo msg="Running phpDox" />
<exec executable="phpdox">
<arg value="${resourceDir}" />
</exec>
</target>
<target name="dev" depends="init, clean, test, build">
<echo msg="Running dev target" />
<copy file="${outputDir}/src/php/index.php" tofile="${devServer}/index.php" />
<copy todir="${devServer}/php">
<fileset dir="${outputDir}/src/php">
<include name="**" />
<exclude name="**/index.php" />
</fileset>
</copy>
<copy todir="${devServer}/api">
<fileset dir="${outputDir}/api/html">
<include name="**" />
</fileset>
</copy>
<copy file="${outputDir}/src/php/lib/phpLogin/index.php" tofile="${devServer}/php/lib/phpLogin/index.php" />
</target>
</project>
Issue 1: phpmd won't output the reportfile yet there are no errors reported in the Jenkins log. Instead it is logging the entire xml to the stdout and reporting it in the Jenkins logs.
myProject > phpmd:
[echo] Running phpmd
Property ${srcDir} => ./src
Property ${phpmdRulesets} => ./resources/phpmd/rulesets
Property ${phpmdRulesets} => ./resources/phpmd/rulesets
Property ${phpmdRulesets} => ./resources/phpmd/rulesets
Property ${phpmdRulesets} => ./resources/phpmd/rulesets
Property ${logDir} => ./build/logs
Property ${srcDir} => ./src
Property ${srcDir} => ./src
[exec] Executing command: phpmd ./src xml ./resources/phpmd/rulesets/codesize.xml,./resources/phpmd/rulesets/unusedcode.xml,./resources/phpmd/rulesets/naming.xml,./resources/phpmd/rulesets/design.xml "--reportfile ./build/logs/messdetector.xml" "--exclude ./src/php/Test/,./src/php/lib/" 2>&1
[exec] <?xml version="1.0" encoding="UTF-8" ?>
...
Issue 2: I am unable to get phpDox to exclude certain directories within my source code. Below is my phpdox.xml.
<?xml version="1.0" encoding="utf-8" ?>
<phpdox xmlns="http://phpdox.de/config">
<project name="myProject.com" source="src/php" workdir="build/api/xml">
<collector backend="parser" publiconly="false">
<include mask="*.php" />
<exclude mask="/lib/**" />
</collector>
<generator output="build/api">
<build engine="html" output="html"/>
</generator>
</project>
</phpdox>
Does anyone know the correct exclude mask I can use please? My file structure is:
myProject.com
/build.xml
/phpdox.xml
/src
/php
/lib
Both issues resolved.
Issue 1: Changed the phpmd task to only use arg values and not arg path. Fixed below:
<target name="phpmd">
<echo msg="Running phpmd" />
<exec executable="phpmd">
<arg value="${dir.src}" />
<arg value="xml" />
<arg value="${dir.phpmdRulesets}/codesize.xml,${dir.phpmdRulesets}/unusedcode.xml,${dir.phpmdRulesets}/naming.xml,${dir.phpmdRulesets}/design.xml" />
<arg value="--reportfile" />
<arg value="${dir.logs}/messdetector.xml" />
<arg value="--exclude"/>
<arg value="test,lib" />
</exec>
</target>
Issue 2: Finally got the exclude masks to work. Fixed xml below:
<?xml version="1.0" encoding="utf-8" ?>
<phpdox xmlns="http://phpdox.de/config">
<project name="myProject.com" source="src/app" workdir="build/api/xml">
<collector backend="parser" publiconly="false">
<include mask="*.php" />
<exclude mask="**/*lib*" />
<exclude mask="**/*test*" />
</collector>
<generator output="build/api">
<build engine="html" output="html"/>
</generator>
</project>
</phpdox>

A sample of build.xml for ANTLR project?

A project's building process is suffering, unless it becomes automatic.
I have started with ANTLR since recently. ANT seems to be the very building tool for that purpose. Compile, jar, and test... But I have found little code source of the script build.xml for that purpose.
So would you guys would like to share your template build.xml for your antlr project (either Java task or ANTLR task will be fine)? Thanks.
This is roughly what I use:
<?xml version="1.0" encoding="UTF-8"?>
<project name="YourProject">
<property name="main.package" value="yourproject"/>
<property name="parser.package" value="${main.package}/parser"/>
<property name="main.src.dir" value="src/main"/>
<property name="test.src.dir" value="src/test"/>
<property name="grammar.src.dir" value="src/grammar"/>
<property name="grammar.file" value="${grammar.src.dir}/YourGrammar.g"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="main.build.dir" value="${classes.dir}/main"/>
<property name="test.build.dir" value="${classes.dir}/test"/>
<path id="classpath">
<pathelement location="${main.src.dir}"/>
<pathelement location="${test.src.dir}"/>
<pathelement location="${main.build.dir}"/>
<pathelement location="${test.build.dir}"/>
<!-- the ANTLR jar is in the lib directory, of course -->
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
</path>
<!-- init target -->
<target name="compile" depends="init" description="Compiles all source files.">
<javac srcdir="${main.src.dir}" destdir="${main.build.dir}" includeantruntime="false">
<classpath refid="classpath"/>
</javac>
<javac srcdir="${test.src.dir}" destdir="${test.build.dir}" includeantruntime="false">
<classpath refid="classpath"/>
</javac>
</target>
<target name="generate" depends="init" description="Generates the lexer and parser from the .g grammar file.">
<echo>Generating the lexer and parser...</echo>
<java classname="org.antlr.Tool" fork="true" failonerror="true">
<arg value="-fo"/>
<arg value="${main.src.dir}/${parser.package}"/>
<arg value="${grammar.file}"/>
<classpath refid="classpath"/>
</java>
<!--
compile the generated parser and lexer source file to see
if there's no illegal code inside these source files
-->
<antcall target="compile"/>
</target>
<!-- other targets -->
</project>
Here's the core pieces of mine, which I think integrates a little better. I'm not sure when ANTLR's -make option was added--I'm using 3.2.
It assumes that grammars are kept in the packages where their generated parsers will be going.
Keeps generated source files separate from normal source files so that they can be cleaned
Only regenerates parser+lexer sources when they are older than grammar file
multiple grammars can be processed in a single pass
ANTLR errors are reported correctly by ant
<project name="MyProject">
<property name="lib.antlr" value="lib/antlr-3.2.jar" />
<property name="src.dir" value="${user.dir}" />
<property name="src.java" value="${src.dir}/java" />
<property name="build.dir" value="build" />
<property name="build.src" value="${build.dir}/src" />
<property name="build.classes" value="${build.dir}/classes" />
<path id="compile.class.path">
<pathelement location="${build.classes}" />
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</path>
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${reports.dir}" />
</target>
<target name="generate" description="Generate parsers from ANTLR grammars">
<mkdir dir="${build.src}" />
<java jar="${lib.antlr}" fork="true" dir="${src.java}" failonerror="true">
<arg value="-verbose" />
<arg value="-make" />
<arg value="-o" />
<arg path="${build.src}" />
<arg value="com/example/io/Foo.g" />
<arg value="com/example/text/Bar.g" />
</java>
</target>
<target name="compile" depends="generate">
<property name="javac.debug" value="on" />
<mkdir dir="${build.dir}" />
<mkdir dir="${build.classes}" />
<javac destdir="${build.classes}" source="1.6" target="1.6" includeantruntime="false" debuglevel="lines,vars,source">
<src path="${src.java}" />
<src path="${build.src}" />
<include name="com/example/**/*.java" />
<classpath refid="compile.class.path"/>
</javac>
</target>
</project>
You can also look at How to use ant with ANTLR3.

ANT script to compile all (css) LESS files in a dir and subdirs with RHINO

I want do compile all *.less scripts in a specific folder and it subdirs with less-rhino-1.1.3.js.
There is an example on github for doing this for a specific file, which works perfect. But I want to do the same for a complete folder. I tried a lot, here is my last try.
It doesn't work, propertyregex seems not to be standard ANT, I don't want to use such things. I am not even sure if this code would work.
<project name="test" default="main" basedir="../../">
<property name="css.dir" location="public/css"/>
<property name="tool.less" location="bin/less/less-rhino-1.1.3.js"/>
<property name="tool.rhino" location="bin/tools/rhino/js.jar"/>
<macrodef name="lessjs">
<attribute name="input" />
<attribute name="output" />
<sequential>
<java jar="${tool.rhino}" fork="true" output="#{output}">
<arg path="${tool.less}"/>
<arg path="#{input}"/>
</java>
<echo>Lessjs: generated #{output}</echo>
</sequential>
</macrodef>
<target name="main">
<echo>compiling less css</echo>
<fileset dir="${css.dir}" id="myfile">
<filename name="**/*.less" />
</fileset>
<property name="lessfilename" refid="myfile"/>
<propertyregex property="cssfilename"
input="${lessfile}"
regexp="^(.*)\.less$"
replace="^\1\.css$"
casesensitive="true" />
<lessjs input="lessfile" output="cssfilename"/>
</target>
</project>
You could use the <fileset> to include all the less files need to be compiled. Later, you could use<mapper> to mark the corresponding detination css file.
<project name="test" default="main" basedir="../../">
<property name="css.dir" location="public/css"/>
<property name="tool.less" location="bin/less/less-rhino-1.1.3.js"/>
<property name="tool.rhino" location="bin/tools/rhino/js.jar"/>
<target name="less" description="Convert LESS to CSS then concatenate and Minify any stylesheets">
<echo message="Converting LESS to CSS..."/>
<!-- Clear the former compiled css files -->
<delete includeemptydirs="true">
<fileset dir="${css.dir}" includes="*.css, **/*.css" defaultexcludes="false"/>
</delete>
<apply dir="${css.dir}" executable="java" parallel="false" failonerror="true">
<!-- Give the input bundle of less files-->
<fileset dir="${css.dir}">
<include name="*.less"/>
</fileset>
<arg value="-jar" />
<arg path="${tool.rhino}" />
<arg path="${tool.less}" />
<srcfile/>
<!-- Output the compiled css file with corresponding name -->
<mapper type="glob" from="*.less" to="${css.dir}/*.css"/>
<targetfile/>
</apply>
</target>
</project>
I was able to piece together a working solution with the help of a couple of SO answers:
ANT script to compile all (css) LESS files in a dir and subdirs with RHINO
How to correctly execute lessc-rhino-1.6.3.js from command line
I had to download LESS 1.7.5 from GitHub and modify the Ant target to look like this. The -f argument and LESS JavaScript was key:
<property name="css.dir" value="WebContent/css"/>
<property name="less.dir" value="less"/>
<property name="tool.rhino.jar" value="test-lib/rhino-1.7R4.jar"/>
<property name="tool.rhino.lessc" value="test-lib/lessc-rhino-1.7.5.js"/>
<property name="tool.rhino.less" value="test-lib/less-rhino-1.7.5.js"/>
<target name="compile-less" description="compile css using LESS">
<apply dir="${css.dir}" executable="java" parallel="false" failonerror="true">
<fileset dir="${less.dir}">
<include name="styles.less"/>
</fileset>
<arg value="-jar"/>
<arg path="${tool.rhino.jar}"/>
<arg value="-f"/>
<arg path="${tool.rhino.less}"/>
<arg path="${tool.rhino.lessc}"/>
<srcfile/>
<mapper type="glob" from="*.less" to="${css.dir}/*.css"/>
<targetfile/>
</apply>
</target>
If anyone else is coming to this question recently, as I did, they may find that the less-rhino-1.1.3.js file given in the other answers does not work with the latest version of Rhino (which for me, as of now, is 1.7R4 from MDN). But the 1.4.0 version does, which can be obtained from Github here. So the relevant snippet from my build.xml, using these later versions, is shown. Note that I'm only compiling a single .less file to a single .css file, so no iteration or mappers are used (but obviously you can get those from the other answers). Other tweaks I made were to provide the output file as the final arg to less instead of capturing output from the Ant forked process, and to remove the dependency on ant-contrib stuff (not needed for the simple one-file case).
<property name="tool.rhino" value="build/lesscss/rhino1_7R4/js.jar" />
<property name="tool.less" value="build/lesscss/less-rhino-1.4.0.js" />
<property name="single-input-lesscss-file" value="/path/to/my/style.less" />
<property name="single-output-css-file" value="/output/my/style.css" />
<target name="compileLessCss" description="Compile the single less file to css">
<sequential>
<java jar="${tool.rhino}" fork="true">
<arg path="${tool.less}" />
<arg path="${single-input-lesscss-file}" />
<arg path="${single-output-css-file}" />
</java>
</sequential>
</target>
If maven is an option for you, you could try wro4j-maven-plugin or wro4j-runner (which is a command line utility).
Using one of these, all you have do is to create an resource model descriptor (wro.xml):
<groups xmlns="http://www.isdc.ro/wro">
<group name="g1">
<css>/path/to/*.less</css>
</group>
</groups>
The rest will be handled by the wro4j library. No need to carry about how rhino works or other details.
Disclaimer: I'm working on wro4j project
I had the same issue. I developed a solution using ant-contrib. It expects all of your .less files to be in one flat directory and to be moved to another flat directory. It will change the file extension to .css in the process.
<property name="tool.rhino" value="/rhino/js.jar" />
<property name="tool.less" value="src/js/less-rhino-1.1.3.js" />
<property name="tool.ant-contrib" value="/ant-contrib/ant-contrib-1.0b3-1.0b3.jar" />
<property name="less-files-dir" value="src/css/" />
<property name="css-files-dir" value="build/css/" />
<target name="compilecss" depends="setup-ant-contrib-taskdef, get-less-files-in-dir" description="DO THIS THING">
<for list="${less-files-to-convert}" param="file-name" trim="true" delimiter=",">
<sequential>
<propertyregex property="file-name-without-extension"
input="#{file-name}"
regexp="(.*)\..*"
select="\1"
override="yes" />
<java jar="${tool.rhino}" fork="true" output="${css-files-dir}${file-name-without-extension}.css">
<arg path="${tool.less}" />
<arg path="${less-files-dir}#{file-name}" />
</java>
<echo>Lessjs: generated ${css-files-dir}${file-name-without-extension}.css</echo>
</sequential>
</for>
</target>
<target name="check-for-ant-contrib">
<condition property="ant-contrib-available">
<and>
<available file="${tool.ant-contrib}"/>
</and>
</condition>
<fail unless="ant-contrib-available" message="Ant-Contrib is not available."/>
</target>
<target name="setup-ant-contrib-taskdef" depends="check-for-ant-contrib">
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<path location="${tool.ant-contrib}" />
</classpath>
</taskdef>
</target>
<target name="get-less-files-in-dir">
<var name="files-list" value="" />
<for param="file">
<path>
<fileset dir="${less-files-dir}" includes="**/*.less" />
</path>
<sequential>
<propertyregex property="file-name-and-relative-path"
input="#{file}"
regexp=".*\\(.*)"
select="\1"
override="yes" />
<echo>file name: ${file-name-and-relative-path}</echo>
<if>
<equals arg1="${files-list}" arg2="" />
<then>
<var name="files-list" value="${file-name-and-relative-path}" />
</then>
<else>
<var name="files-list" value="${files-list},${file-name-and-relative-path}" />
</else>
</if>
</sequential>
</for>
<property name="less-files-to-convert" value="${files-list}" />
<echo>files to convert: ${less-files-to-convert}</echo>
</target>
I was unable to get this to run using a JDK 1.6 since the javascript stuff has been incorporated to the JDK. The JDK does have a jrunscript executable in the distribution but when I try to run the less-rhino.js file it fails to recognize any readFile() function. Has anyone looked into that. Otherwise I may be giving the lesscss-engine a shot and enhancing it to understand filesets.

Multiple renaming using ant script

Let me explain the scenario:
D:\project\src\one.txt
D:\project\src\two.txt
D:\project\src\three.txt
D:\project\src\four.txt
The above files should be copied as :
D:\project\dst\one.xls
D:\project\dst\two.xls
D:\project\dst\three.xls
D:\project\dst\four.xls
I need to change the extension without using the mapper and move task. I need to rename as above using a for loop with fte:filecopy function inside. Is this possible ???
For anyone arriving here without the negative requirement afflicting the OP, the much simpler answer is to use a mapper.
<project default="move_files">
<target name="move_files">
<copy todir="dst">
<fileset dir="src">
<include name="*.txt"/>
</fileset>
<globmapper from="*.txt" to="*.xls"/>
</copy>
</target>
</project>
This works for me :
<?xml version="1.0"?>
<project name="so-copy-rename" default="build2">
<property name="ant-contrib-jar" value="${user.home}/.ant/lib/ant-contrib-1.0b3.jar"/>
<target name="setup" unless="ant-contrib.present">
<echo>Getting ant-contrib</echo>
<mkdir dir="${user.home}/.ant/lib"/>
<!--
Note: change this to a locally hosted maven repository manager such as nexus http://nexus.sonatype.org/
-->
<get dest="${ant-contrib-jar}"
src="http://repo1.maven.org/maven2/ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jar"/>
</target>
<target name="taskdefs">
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="${ant-contrib-jar}"/>
</classpath>
</taskdef>
</target>
<target name="build" depends="taskdefs">
<property name="srcdir" value="src"/>
<property name="targetdir" value="target"/>
<property name="files" value="file1,file2,file3,file4"/>
<mkdir dir="${targetdir}"/>
<foreach list="${files}" target="copy-rename" param="srcfile" trim="true">
<param name="srcdir" value="${srcdir}" />
<param name="targetdir" value="${targetdir}" />
</foreach>
</target>
<target name="copy-rename">
<var name="src-suffix" value="txt"/>
<var name="tgt-suffix" value="xls"/>
<copy file="${srcdir}/${srcfile}.${src-suffix}" tofile="${targetdir}/${srcfile}.${tgt-suffix}" />
</target>
<target name="build2" depends="taskdefs">
<property name="srcdir" value="src"/>
<property name="targetdir" value="target"/>
<mkdir dir="${targetdir}"/>
<foreach target="copy-rename2" param="srcfile">
<path id="srcfilepath">
<fileset dir="${srcdir}" casesensitive="yes">
<include name="*.txt"/>
</fileset>
</path>
<param name="targetdir" value="${targetdir}" />
</foreach>
</target>
<target name="copy-rename2">
<var name="basefile" value="" unset="true"/>
<basename property="basefile" file="${srcfile}" suffix=".txt"/>
<var name="tgt-suffix" value="xls"/>
<copy file="${srcfile}" tofile="${targetdir}/${basefile}.${tgt-suffix}" />
</target>
</project>
Can you slice it the other way and perform the renaming inside the fte:filecopy command? Looking at the IBM documentation, you can specify tasks to be carried out at the source or destination agents either before or after the copy, using presrc, postdst etc. This task could be an Ant task that does the renaming?

Resources