ant exec task error with code=3 - java

Im trying to invoke a bash scrip from an ant target. This is my target :
<target name="report" depends="test">
<!-- Step 3: Create coverage report -->
<exec executable="./checkStyle.sh"
failonerror="true"
osfamily="unix"/>
<jacoco:report>
<!-- This task needs the collected execution data and ... -->
<executiondata>
<file file="${result.exec.file}" />
</executiondata>
<!-- the class files and optional source files ... -->
<structure name="JaCoCo Ant Example">
<classfiles>
<fileset dir="${result.classes.dir}" />
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${src.dir}" />
</sourcefiles>
</structure>
<!-- to produce reports in different formats. -->
<html destdir="${result.report.dir}" />
<csv destfile="${result.report.dir}/report.csv" />
<xml destfile="${result.report.dir}/report.xml" />
</jacoco:report>
</target>
And my bash script is :
#!/bin/bash
DEST_FOLDER='./target/checkstyle'
CLASSES_FILE='classes.txt'
REPORT_FILE='report.xml'
mkdir -p "$DEST_FOLDER"
rm -f "$DEST_FOLDER/$CLASSES_FILE"
rm -f "$DEST_FOLDER/$REPORT_FILE"
find ./project -name "*.java" >> "$DEST_FOLDER/$CLASSES_FILE"
while read p; do
java -jar ./utilJars/checkstyle-6.5-all.jar -c ./sun_checks.xml -f xml $p >> "$DEST_FOLDER/$REPORT_FILE"
done < $DEST_FOLDER/$CLASSES_FILE
When typing ./checkStyle everything works fine, but when I try "ant report" the following error is raised:
BUILD FAILED
/home/luci/workspace/operations/build.xml:60: exec returned: 3
Total time: 4 seconds
I`ve searched on google and that code seems to be "permision denied", but i dont know how i could solve this problem.

Generally with Ant (and Java), you cannot directly execute shell scripts. You need to execute the interpreter/shell and give the script as an argument.
For example:
<exec executable="/bin/bash" failonerror="true" osfamily="unix">
<arg value="-c"/>
<arg value="./checkStyle.sh"/>
</exec>

Related

Adding autoprefix-cli to ANT build

I am trying to add autoprefix-cli to my ANT build. Below is my code.
<target name="auto">
<apply executable="autoprefixer-cli.bat" verbose="true" force="true" failonerror="true">
<arg value="-d" /> <!-- Turn on verbose -->
<arg value="prefix" />
<arg value="*.css" />
</apply>
</target>
When i do a ant build, it gives me an error saying resource not specified.
BUILD FAILED
D:\tempTest\AntTestProject\build.xml:25: no resources specified
Note: I can access autoprefix-cli from command line, its installed with -g flag and also it works when i directly use it from commandline.
The apply task basically loops the exec task on a batch of resources (files, directories, URLs, etc). If you only want to run one command, use exec instead.
However, you will likely also need to alter your command. From Ant's exec task 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.
https://ant.apache.org/manual/Tasks/exec.html
So instead you should have:
<exec executable="cmd" verbose="true" force="true" failonerror="true">
<arg value="/c" />
<arg value="autoprefixer-cli.bat" />
<arg value="-d" />
<arg value="prefix" />
<arg value="*.css" />
</exec>

Trying to use exec executable in ant to run R

I am using the following open source version of image J in an experiment:
https://github.com/pcj/arterioj
it uses ANT.
Of of the ant targets is:
stats
<!-- ================================================================ -->
<!-- Stats and plot generation tasks -->
<!-- ================================================================ -->
<target name="stats">
<exec executable="R">
<arg line="--vanilla --no-readline --quiet --slave --file=${basedir}/ldip-data.R --args"/>
</exec>
</target>
when I try to use this target I get the following build error message:
BUILD FAILED
/Users/James/ArterioJ/build.xml:179: Execute failed: java.io.IOException: Cannot run program "R" (in directory "/Users/James/ArterioJ"): error=2, No such file or directory
I am unsure how to fix this. I have tried to copy the R package into the /users/James/ArterioJ directory to see if that would help but to no effect.
Thanks
You might want to specify the full path of the R executable, for instance:
<property name="r.home" value="/home/me/R_INSTALL"/>
<target name="stats">
<exec executable="${r.home}/R">
<arg line="--vanilla --no-readline --quiet --slave --file=${basedir}/ldip-data.R --args"/>
</exec>
</target>
Alternatively, if the path to the executable is part of the PATH environment variable, you could just add the searchpath="true" attribute to the exec task:
<exec executable="R" searchpath="true">
<arg line="--vanilla --no-readline --quiet --slave --file=${basedir}/ldip-data.R --args"/>
</exec>
Here is an extract from the Ant documentation about searchpath:
When this attribute is true, then system path environment variables
will be searched when resolving the location of the executable

How to run testng.xml using command prompt

I was trying to run testng.xml from command prompt but not able to figure out the following error:
Error: Could not find or load main class org.testng.TestNG
or either
Error: Could not find or load main class java.
While using two different method.
Please provide a prominent method to do the same.
Thanks guys, but i have solve this problem using Apache ant.
By using following - build.xml
<project name="name" basedir=".">
<!-- ========== Initialize Properties =================================== -->
<!-- set global properties for build -->
<property name="basedir" value="." />
<property name="lib" value="${basedir}/lib" />
<property name="src" value="${basedir}/src" />
<property name="bin" value="${basedir}/bin" />
<property name="report-dir" value="${basedir}/Test-Report" />
<property name="testng-report-dir" value="${report-dir}/TestNGreport" />
<!-- ====== Set the classpath ==== -->
<path id="classpath">
<pathelement location="${bin}" />
<fileset dir="${lib}">
<include name="*.jar" />
</fileset>
</path>
<!-- Delete directories -->
<target name="delete-dir">
<delete dir="${bin}" />
<delete dir="${report-dir}" />
</target>
<!-- Creating directories -->
<target name="create" depends="delete-dir">
<mkdir dir="${bin}" />
<mkdir dir="${report-dir}" />
</target>
<!-- Compile the java code from ${src} into ${bin} -->
<target name="compile" depends="create">
<javac srcdir="${src}" classpathref="classpath" includeAntRuntime="No" destdir="${bin}" />
<echo> /* Compiled Directory Classes */ </echo>
</target>
<!-- Runs the file and generates Reportng report for TestNG-->
<taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="classpath" />
<target name="testng-execution" depends="compile">
<mkdir dir="${testng-report-dir}" />
<testng outputdir="${testng-report-dir}" classpathref="classpath" useDefaultListeners="true">
<xmlfileset dir="${basedir}" includes="testng.xml" />
</testng>
</target>
</project>
And then by typing: ant testng-execution on command prompt
Paths are separated using : under Unix-like systems and not ; as in Windows:
java -cp ./src/lib/*:./bin org.testng.TestNG testng.xml
If you are using bash under Windows, you should use ; and any other place use :.
The ; character means end of statement to a Unix shell, so what you are probably attempting to execute is:
java -cp ./src/lib/*
./bin org.testng.TestNG testng.xml
I use testNg on windows, in the windows command prompt the execution, I use the command: java -cp bin;libs/* org.testng.TestNG testng.xml
Result: test run successfully
always on windows: I use git bash command, the command does not execute successfully
Try to run via cmd prompt, like this:
1. Go to project folder
2. Then: `java -cp [projectpath]\lib\*;[projectpath]\target org.testng.TestNG testng.xml`

Getting illegal character error while using closure-compiler.jar using ANT for r.js optimization

I am using closure compiler jar for minification in r.js optimization in windows environment.
While running this task using ANT exec, getting illegal character error but while running same task using .bat file it working fine.
ANT exec task
<target name="do-optimization" description="It will do optimization using r.js.">
<exec dir="." executable="java" failonerror="true">
<arg value="-jar" />
<arg path="${src.dir}/r-js/lib/rhino/js.jar" />
<arg path="${src.dir}/r-js/lib/closure/compiler.jar" />
<arg path="${src.dir}/r-js/dist/r.js" />
<arg value="-o"/>
<arg path="${src.dir}/r-js/build.js" />
</exec>
</target>
console output
do-optimization:
[exec] js: "C:\workspace\test\ui\r-js\lib\closure\compiler.jar", line 2: illegal character
[exec] js: ╝MOC ♦ META-INF/■╩ PK♥♦
[exec] js: ^
[exec] js: "C:\workspace\test\ui\r-js\lib\closure\compiler.jar", line 1: Compilation produced 1 syntax errors.
[exec]
BUILD FAILED
optimize.bat
java -classpath "r-js\lib\rhino\js.jar";"r-js\lib\closure\compiler.jar" org.mozilla.javascript.tools.shell.Main r-js/dist/r.js -o build.js
It doesn't seem that the Ant script is calling java the same way as the batch file. The exec task is calling the following command:
java -jar ${src.dir}/r-js/lib/rhino/js.jar ${src.dir}/r-js/lib/closure/compiler.jar ${src.dir}/r-js/dist/r.js -o ${src.dir}/r-js/build.js
which is clearly different than the one in the .bat file, i.e. the jars are not being added to the classpath properly.
In Ant, you can simply use the java task to run a Java class. Try using the following:
<java classname="org.mozilla.javascript.tools.shell.Main" failonerror="true">
<arg path="${src.dir}/r-js/dist/r.js" />
<arg value="-o"/>
<arg path="${src.dir}/r-js/build.js" />
<classpath>
<pathelement location="${src.dir}/r-js/lib/rhino/js.jar" />
<pathelement location="${src.dir}/r-js/lib/closure/compiler.jar" />
</classpath>
</java>

Execute bower update from ANT task

i want to create an ANT Task that permit to execute bower update in my project.
I've written this target:
<target name="update-static-web-dependecy" description="Update static web dependency">
<exec dir="${static-web.dir}" executable="/usr/local/bin/bower">
<arg value="update"/>
</exec>
</target>
But when i'm going to run it i get this error :
env: node: No such file or directory
Result: 127
In my OS Bower is installed correctly with the arg -g and if i launch it from the shell it works well.
Can someone help me?
Regards,
Lorenzo
I'll take a guess that you could resolve by specifying the path to node in an env element in your exec block. Something like this
<property name="node.dir" value="/usr/local/bin"/>
<exec dir="${static-web.dir}" executable="/usr/local/bin/bower">
<env key="PATH" value="${env.PATH}:${node.dir}"/>
<arg value="update"/>
</exec>
For anyone wanting to do this in Phing, it's a little bit less involved, but the Ant answer might throw you. You can do it all in one go if you know where bower is:
<exec command="/usr/bin/bower install" dir="${dirs.httpdocs}" />
If you're doing something generic, you can check for the existence of bower.json first:
<!-- bower exists -->
<property name="bowerExists" value="false" />
<exec command="if [ -f '${dirs.httpdocs}/bower.json' ]; then echo 'true'; else echo 'false'; fi;"
outputProperty="bowerExists" />
<if>
<equals arg1="${bowerExists}" arg2="true" />
<then>
<echo>Installing Bower Packages</echo>
<exec command="/usr/bin/bower install" dir="${dirs.httpdocs}" />
</then>
</if>
Source: http://www.szabogabor.net/how-to-check-in-phing-if-a-file-or-directory-exists/

Categories