YUI Compressor: Output with line breaks using Ant - ant

My Ant build script has this statement:
<java
jar="../tools/yuicompressor-2.4.8.jar" fork="true"
args="../js/navigation.js -o ../js/navigation.min.js">
</java>
The newly created file ../js/navigation.min.js is not formatted. Is there any way by which I can format the populated output file so that it isn't all on one line?

From the YUI Compressor documentation:
--line-break
...
Specify 0 to get a line break after each semi-colon in JavaScript, and
after each rule in CSS.
Here's how you would use --line-break in an Ant script:
<java jar="../tools/yuicompressor-2.4.8.jar" fork="true">
<arg value="../js/navigation.js"/>
<arg value="-o"/>
<arg value="../js/navigation.min.js"/>
<arg value="--line-break"/>
<arg value="0"/>
</java>

Related

Ant exec, How to arg

So I have the below build file:
<target name="test">
<echo message="test"></echo>
<exec executable="C:\Users\Abc\Desktop\cmd.bat" >
<arg value="exit"/>
<arg value="mkdir C:\Users\Abc\Desktop\ant\test"/>
</exec>
</target>
I thought it would create a test folder there but its not. It pops out another command prompt window, I thought the exit would close that but it doesn't. I'm not sure what I am doing wrong.
The content of bath script test1.bat:
#echo off
echo %1
And ant's build.xml:
<target name="test">
<echo message="test"></echo>
<exec executable="C:\Users\AAA\Desktop\ant\test1.cmd">
<arg value="hello"/>
</exec>
</target>
As per the official documentation:
Note that .bat files cannot in general by executed directly. One
normally needs to execute the command shell executable cmd using the
/c switch
Follow this format instead:
<target name="help">
<exec executable="cmd">
<arg value="/c"/>
<arg value="ant.bat"/>
<arg value="-p"/>
</exec>
</target>
Source: https://ant.apache.org/manual/Tasks/exec.html

No output running yui-compressor from Ant

I'm going to minify my js files by yuicomressor via ant, I wrote this:
<property name="concat-js-file-name" value="main.concat.js"/>
<property name="concat-js-file-path" value="${temp-folder}/js/${concat-js-file-name}"/>
<property name="yui-jar-path" value="lib/yuicompressor-2.4.7.jar"/>
<target name="minification" depends="concatation">
<echo>---Minification is started</echo>
<java jar="${yui-jar-path}" fork="true">
<arg value="${concat-js-file-path}"/>
<arg value="-o minified.js"/>
</java>
<echo>---Minification is finished successfully...</echo>
</target>
The problem is the output file is not generated!
Any idea?
You should set <java ... failonerror="true"/> and increase the noiselevel to see what's going on, means start your ant build with ant -f yourbuild.xml -debug
Actually, after some tries I found a solution:
I used <arg line="-o outputfile inputfile"/> instead, and it worked.
I suggest using <arg value="..."> instead of <arg line="...">. <arg value="..."> ensures that each command line argument has quotes around it, if necessary.
In the case of yui-compressor, the "-o" and "<file>" arguments should each go in their own <arg value="..."> elements:
<java jar="${yui-jar-path}" fork="true">
<arg value="-o"/>
<arg value="minified.js"/>
<arg value="${concat-js-file-path}"/>
</java>

Variable number of parameters using <macrodef> and exec

I am using exec to invoke a command line program multiple times from an ant build.xml. This command line program takes variable number of arguments for different cases.
At the moment I am calling this external program multiple time using exec and code looks cluttered. For example:
<exec dir="./deploy_abc/bin" executable="python" failonerror="true" >
<arg line="tests.py"/>
<arg line="-h aaa"/>
<arg line="-u bbb"/>
<arg line="-p ccc"/>
</exec>
<exec dir="./deploy_abc/bin" executable="python" failonerror="true" >
<arg line="tests.py"/>
<arg line="-h ddd"/>
<arg line="-u eee"/>
<arg line="-p fff"/>
<arg value="this is second test"/>
</exec>
<exec dir="./deploy_abc/bin" executable="python" failonerror="true" >
<arg line="tests.py"/>
<arg line="-u bbb"/>
<arg line="-p ccc"/>
<arg value="this is another test"/>
</exec>
So I am planning to refactor this build.xml file using macrodef.
My question is how to pass variable number of parameters to macrodef. As it is shown above I need to pass different arguments to the exec-ed program based on the scenario.
You can use a macrodef element to support this:
This is used to specify nested elements of the new task. The contents
of the nested elements of the task instance are placed in the
templated task at the tag name.
For example, you might define your macro like this:
<macrodef name="call-exec">
<attribute name="dir"/>
<attribute name="executable"/>
<attribute name="failonerror"/>
<element name="arg-elements"/>
<sequential>
<exec dir="#{dir}" executable="#{executable}"
failonerror="#{failonerrer}" >
<arg-elements />
</exec>
</sequential>
</macrodef>
And call it like this:
<call-exec dir="./deploy_abc/bin" executable="python" failonerror="true" >
<arg-elements>
<arg line="tests.py"/>
<arg line="-u bbb"/>
<arg line="-p ccc"/>
<arg value="this is another test"/>
</arg-elements>
</call-exec>

How to get %ERRORLEVEL% from batch file in Ant

I want to know if its possible to get the return value from batch file in Ant build xml.
My batch file returns %ERRORLEVEL% value (batch file returns 2 in my case). I want to know if it's possible to capture this and mark as error in Ant. Below is the code snippet I use:
<exec executable = "cmd">
<arg value="/c"/>
<arg value="C:\workspace\Build\cross_Compile.bat"/>
</exec>
Currently after the batch file call, build is reported as success always. It looks like Ant is not processing the %ERRORLEVEL% or I am not sure. How I can make Ant process the %ERRORLEVEL%?
Use the resultproperty and failonerror. By default, the errocode is ignored.
<property name="Batcherrcode" value="0"/>
<exec executable = "cmd" failonerror="true" resultproperty="Batcherrcode">
<arg value="/c"/>
<arg value="C:\workspace\Build\cross_Compile.bat"/>
</exec>
<echo message="Error Code:=${Batcherrorcode}" />

How to pass a list of files to <exec>?

This is a part of my ant script:
<target>
<exec executable="find" outputproperty="found">
<arg value="src/main/java"/>
<arg line="-name '*.java'"/>
</exec>
<exec executable="xgettext">
<arg value="-k_"/>
<arg line="-o gettext.pot"/>
<arg line="${found}"/>
</exec>
</target>
Doesn't work because xgettext receives a quoted list of files and treats this list as a single file name. How to solve it?
You'd need to separate out each file to a separate arg for that to work.
You can supply a list-of-files file to process to 'xgettext' using the --files-from option.
How about something like this: write the 'find' output to a file, then reload into 'xgettext':
<target>
<exec executable="find" outputproperty="found">
<arg value="src/main/java"/>
<arg line="-name '*.java'"/>
</exec>
<echo file="xgettext.files" message="${found}" />
<exec executable="xgettext">
<arg value="-k_"/>
<arg value="-o" />
<arg value="gettext.pot"/>
<arg value="--files-from=xgettext.files"/>
</exec>
</target>
Alternatively, here's a variation that assumes you have the Bourne Shell sh - if you have something else you can probably adapt. This pipes the 'find' output directly to 'xgettext':
<exec executable="sh">
<arg value="-c"/>
<arg value="find src/main/java -name '*.java' | xgettext -k_ -o gettext.pot -f -"/>
</exec>

Resources