Ant: Apply with fileset, but only exec once on one file? - ant

I'm working on something similar to the question here: ANT script to compile all (css) LESS files in a dir and subdirs with RHINO
However, I'm having a hard time customizing this to one particular requirement:
If any .less files in dir.less change: Run LESS on just one file (as it imports the other less files, making a single, combined output).
This is the state of my current build.xml:
<target name="less" description="Compile LESS files">
<echo message="Checking for LESS file changes..."/>
<apply dir="${dir.less}" executable="${tool.less}" parallel="false" failonerror="true">
<fileset dir="${dir.less}" includes="*.less" />
<srcfile/>
<mapper type="glob" from="*.less" to="${dir.css}/*.css"/>
<targetfile/>
<arg value="-compress" />
</apply>
</target>
This currently builds all of the .LESS files and outputs them toe the appropriate location (Which is livable). If I replace the mapper glob with:
<mapper type="glob" from="MainFileThatImportsOthers.less" to="${dir.css}/MainFileThatImportsOthers.css"/>
The fileset directive is effectively reduced to that one file, and changing the other .LESS files in that directory don't cause output from the task.
Can someone point me in the right direction so I can avoid setting this up wrong and recusing through each .LESS file every time?

I worked out a solution that works correctly, I used an upToDate task to set a property to conditionally trigger Exec for the compiler:
<target name="scanLess" description="Scan for LESS file changes">
<echo message="Checking for LESS file changes..."/>
<uptodate property="tool.less.changed" targetfile="${dir.css}/MyFile.css" >
<srcfiles dir="${dir.less}" includes="*.less" />
</uptodate>
</target>
<target name="less" depends="scanLess" unless="tool.less.changed" description="Compile LESS files" >
<echo message="LESS files changed, running lessc" />
<exec executable="${tool.less}" failonerror="true">
<arg value="${dir.less}/MyFile.less" />
<arg value="${dir.css}/MyFile.css" />
<arg value="-compress" />
</exec>
</target>

Investigate how selectors work in ANT

Related

How to create an Ant's symlink with wildcard?

I've tried to create a symlink like:
<symlink link="${basedir}/docroot" resource="${basedir}/drupal-7.*" overwrite="true"/>
however, the line doesn't expand the wildcard but creates a link which points literally to drupal-7.*, not to drupal-7.56.
I've tried to use examples with fileset on this page, but it doesn't really cover my scenario.
How can I create an Ant's symlink which points to a dynamic folder (the one expanded by drupal-7.*)?
You should use dirset instead of fileset to include directories, not files. Also, it would probably be a good idea to check for an unexpected number of directories as well.
Here is the example:
<dirset dir="${basedir}" includes="drupal-7.*" id="link.target" />
<fail message="Multiple or zero drupal-7.* directories found.">
<condition>
<not>
<resourcecount refid="link.target" when="equal" count="1" />
</not>
</condition>
</fail>
<symlink link="${basedir}/docroot" resource="${toString:link.target}" />
Here is the solution by invoking shell command:
<exec executable="sh" failonerror="true" dir="${basedir}" outputproperty="drupaldir">
<arg value="-c"/>
<arg value="echo drupal-7*"/>
</exec>
<symlink link="${basedir}/docroot" resource="${basedir}/${drupaldir}" overwrite="true"/>

Specify Multiple Output Files or Target Files for an Ant Apply Task

I'm trying to figure out a way to have Ant run a .jar executable that accepts a file and spits out several generated files from the single input file. Specifically, I'm trying to generate compiled .js files and at the same time generate .map files.
Normally, the command would look something like this:
java -jar compiler-latest --js a.js --js_output_file a.min.js --create_source_map a.js.map
Where:
compiler-latest is the closure-compiler jar
a.js is the JavaScript file to compile
a.min.js is the compiled JavaScript
a.js.map is the source map
My Ant script looks like this:
<project name="BuildTest" default="Build" basedir=".">
<description>
HTML Build Test with Ant
</description>
<property name="src" location="../js"/>
<property name="dst" location="../build"/>
<property name="compiler" location="../compiler.jar"/>
<!--Make Dest Directory-->
<target name="-destination">
<mkdir dir="${dst}"/>
</target>
<!--Compile JS-->
<target name="Build" depends="-destination">
<!--Filesets and Mappers-->
<fileset id="sourceFiles" dir="${src}" includes="*.js"/>
<mapper id="compiledJs" type="glob" from="*.js" to="*.compiled.js"/>
<mapper id="mapJs" type="glob" from="*.js" to="*.js.map"/>
<!--Apply Everything-->
<apply executable="java" parallel="false" dest="${dst}">
<!--Closure Compiler-->
<arg value="-jar"/>
<arg path="${compiler}"/>
<arg value="--compilation_level=SIMPLE_OPTIMIZATIONS"/>
<!--Source Files-->
<arg value="--js"/>
<srcfile/>
<fileset refid="sourceFiles"/>
<!--Output Files-->
<arg value="--js_output_file"/>
<targetfile/>
<mapper refid="compiledJs"/>
<!--Source Maps-->
<arg value="--source_map_format=V3"/>
<arg value="--create_source_map"/>
<arg value="--js_output_file"/>
<targetfile/>
<mapper refid="mapJs"/>
</apply>
</target>
<!--Clean Project-->
<target name="Clean" description="Cleans the project">
<delete dir="${dst}"/>
</target>
</project>
However, I get an error saying I can't have multiple <targetfile/> elements
apply doesn't support multiple targetfile elements.
This is a workaround, not nice, but effective.
You can use an Ant <compositemapper> to construct the command line for your application.
Below is an illustration. You need to set relative="yes" on the task in order that filenames relative to the build directory are used in preference to absolute filenames, otherwise mapping is harder. To build the command line provide a list of mappers inside the <compositemapper>. Use a <mergemapper> for fixed parts (args like --output_file), and use a suitable other mapper, maybe a glob, when you need to generate filenames.
A series of mappers is needed to separate the arguments passed to the java by <apply>, otherwise they will be passed as one long arg with embedded spaces.
<apply executable="java" parallel="false" relative="yes">
<arg line="-jar compiler-latest --js"/>
<srcfile />
<targetfile />
<compositemapper>
<mergemapper to="--js_output_file" />
<globmapper from="*.js" to="*.compiled.js" />
<mergemapper to="--source_map_format=V3" />
<mergemapper to="--create_source_map" />
<globmapper from="*" to="*.map" />
</compositemapper>
<fileset dir="." includes="*.js" />
</apply>
For a simple test that leads to a command line like:
java -jar compiler-latest --js 1.js --js_output_file 1.compiled.js --source_map_format=V3 --create_source_map 1.js.map

ant : apply executable when output file doesn't exist

I try to create a ant target which processes some GLSL shaders from an input directory, output them in another folder. And I'd like to avoid the processing if the output file already exists.
The executable I use can either take an output directory argument, or directly the output file path.
Currently, I have :
<target name="optimize_programs">
<apply executable="TOOLS/glsl_processor" dir="." verbose="true" >
<srcfile/>
<arg value="-output_directory=OUTPUT/PROGRAMS/" />
<fileset dir="INPUT/PROGRAMS/OPENGLES2" includes="**/*.glfx" />
<flattenmapper />
</apply>
</target>
The shaders are correctly processed, but the problem is they are processed each time, even when the output file already exists.
I suspect this is because the flattenmapper is not aware of the glsl_processor output.
I've tried to use to tell the glsl_processor where to output the file :
<target name="optimize_programs">
<apply executable="TOOLS/glsl_processor" dir="." verbose="true" >
<srcfile/>
<targetfile/>
<fileset dir="INPUT/PROGRAMS/OPENGLES2" includes="**/*.glfx" />
<flattenmapper />
</apply>
</target>
But I don't know how to make targetfile point to the output folder.
Any idea?
Thanks!
Well the answer is actually pretty simple:
<target name="optimize_programs">
<apply executable="TOOLS/glsl_processor" dir="INPUT/PROGRAMS/OPENGLES2/" dest="OUTPUT/PROGRAMS" verbose="true" >
<targetfile/>
<srcfile/>
<fileset dir="INPUT/PROGRAMS/OPENGLES2" includes="**/*.glfx" />
<mapper type="glob" from="*.glfx" to="*.glfx"/>
</apply>
</target>
I was just missing to fill the dest attribute of the apply task.

modify ant classpath on the fly

I want to use resolvers (ssh) which are dependant on ant classpath.
Something like
<resolvers>
...
<ssh ...
...
</resolvers>
To use it I need jsch in ant classpath. Ant script should depends only on common lib (which also includes resolved jsch dependencies) - to use it on any client PC. Scenario is:
task to download lib.
Extract libs (jsch and etc.)
ivy:configure
But ivy:configure does not have any classpathref param, so it is unclear for me how to load jars I extracted.
Is it possible?
Or, probably, somehow run ant again internally with extended classpath?
Ok,
so my comment to question looked good for me but at the end it did not work.
The only way I found (working way I mean) is to to run ant script with
Download common-lib (with ) which includes all jar-libs required for optional ivy processing
Construct new classpath and run exec on same build file with required target:
<target name="call.task" if="wrapped.task.name">
<path id="ant.class.path">
<fileset dir="${tools.lib.dir}" >
<include name="*.jar" />
</fileset>
<pathelement location="${java.class.path}" />
</path>
<condition property="append.dest.dir" value="-Ddest.dir=${dest.dir}" else="">
<isset property="dest.dir"/>
</condition>
<exec executable="ant" failonerror="true">
<arg line="-f ivy-build.xml" />
<arg line='-lib "${toString:ant.class.path}"' />
<arg value="${wrapped.task.name}" />
<arg value="${append.dest.dir}" />
</exec>
</target>

Inconsistent NoClassDefFoundError in subant java task

My ant build script starts with a java task that uses fork=true
<java fork="true"
classname="org.apache.tools.ant.launch.Launcher"
jvm="${java.home}/bin/java"
classpathref="class.path">
<arg value="-f" />
<arg value="${ant.file}" />
<arg value="generate" />
</java>
The <arg value="generate" /> points to another task in the same ant build file.
This task starts another target with a subant task that points to another file.
<subant verbose="true" target="replace">
<fileset dir="${basedir}" includes="refactor.xml" />
</subant>
This file refactor.xml starts a java task again with fork=true.
<java classpathref="class.path"
classname="namespace.Tool"
fork="true"/>
The strange behaviour is: everything works fine, except once in a while I get the NoClassDefFoundError error for the namespace.Tool java source file.
After e.g. closing, reopening the file the error may disappear, however there is no reproducible behaviour.
I tried avoiding the subant construction (used to unclutter) but this doesn't help.
Finally the class.path that is referenced is like this:
<path id="class.path">
<pathelement location="../common/bin" />
<pathelement location="./bin" />
<fileset dir="${build.dir}">
<include name="...jar" />
</fileset>
</path>
Any ideas?
Cause was <pathelement location="./bin" />.
This bin folder was recompiled by Eclipse as soon as in other steps in the sequence of Ant tasks e.g. a folder was deleted. The default setting in Eclipse is to recompile all code at such a moment.
As a result the Ant process may or may not find a specific class in this bin folder resulting in the inconsistent NoClassDefFoundError.

Resources