How to print the exec error code using ANT - http

I have my ANT file which uses exec task to create Jenkins job using cURL. Whether exec task is success or failed, the jenkins build will be succeeded. So I tried to use resultproperty in exec and tried to print the result, but it returns only 0.
<exec executable="curl" resultproperty="MyExecResult" failonerror="false">
<arg value="-k" />
<arg value="-X" />
<arg value="GET" />
<arg value="<MyJenkinsURL>config.xml" />
<arg value="-o" />
<arg value="<MyPath>\GET\config.xml" />
<arg value="-u" />
<arg value=":" />
<arg value="--ntlm" />
</exec>
<echo>MyExecResult-GET ::: ${MyExecResult}</echo>
How can I print the resultproperty value in this scenario to get the error code. Kindly provide inputs. Thanks!

You have to use the erroproperty attribute, see Ant manual exec task
errorproperty The name of a property in which the standard error of
the command should be stored. since Ant 1.6

Related

Cannot run program npm error when running Cordite network map JAR on Windows

When running the network map using Java (as described here: https://gitlab.com/cordite/network-map-service#using-java) on Windows, I get the following error:
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-antrun-plugin:1.8:run (build-website)
on project network network-map-service: An Ant BuildException has
occured: Execute failed: java.io.IOException: Cannot run program
"npm": CreateProcess error-2, The system cannot find the file
specified around Ant part ...... # 4:45 in
C:\Users\x.x\network-map-service\target\antrun\build-main.xml
What is the cause of this error?
You are encountering this issue because the build file is as follows:
<?xml version="1.0" encoding="UTF-8" ?>
<project name="maven-antrun-" default="main" >
<target name="main">
<exec failonerror="true" executable="npm">
<arg value="install"/>
<arg value="-g"/>
<arg value="brunch"/>
</exec>
<exec failonerror="true" dir="website" executable="npm">
<arg value="install"/>
</exec>
<exec failonerror="true" dir="website" executable="brunch">
<arg value="build"/>
</exec>
</target>
</project>
But Windows requires the executable names to be npm.bat and brunch.bat instead.
If you change the executable names in the build file, it will work correctly.

Can I silence Closure Compiler warnings for external libraries?

When I run the closure compiler, I get a bunch of warnings like this:
[exec] jquery/3.2.1/dist/jquery.js:733: WARNING - Suspicious code. The result of the 'getprop' operator is not being used.
[exec] arr[ preferredDoc.childNodes.length ].nodeType;
[exec] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I end up with about 30 warnings in my build, all from jquery and bootstrap.js
I'm handling my build using an Ant script. The call to the Closure Compiler looks like this:
<target name="build_scripts" description="Compile frontend scripts">
<exec executable="${java.exec}" failonerror="true">
<arg line="-client -d64 -jar ${closure.jar}"/>
<arg line="--js ${src.dir}/assets/js/*.js"/>
<arg line="--js ${src.dir}/assets/js/**/*.js"/>
<arg line="--js ${weblib.dir}/jquery/3.2.1/dist/jquery.js"/>
<arg line="--js ${weblib.dir}/bootstrap-sass/3.3.7/assets/javascripts/bootstrap.js"/>
<arg line="--externs ${build.dir}/jquery-3.2.externs.js"/>
<arg line="--dependency_mode=STRICT"/>
<arg line="--entry_point ${src.dir}/assets/js/main.js"/>
<arg line="--js_output_file ${out.js.dir}/main.js"/>
<env key="JAVA_HOME" path="${java_home}"/>
</exec>
</target>
I thought the point of the externs file was to get rid of warnings like this? The answers to this question seem to suggest I'd need to manually change it in the external libraries.
I don't want to silence all warnings; just these from external libraries. Is that possible?
The --hide_warnings_for='path/segment' flag should do what you need.

Compile SASS to CSS using ANT build task

I am successfully able to compile my SASS into CSS by using the following script in my build.xml file
<apply executable="sass.bat" dest="${css.dir}" verbose="true" force="true" failonerror="true">
<arg value="--unix-newline" />
<srcfile />
<targetfile />
<fileset dir="${sass.dir}" includes="**/*.scss,**/*.sass" excludes="**/_*" />
<firstmatchmapper>
<globmapper from="*.sass" to="*.css" />
<globmapper from="*.scss" to="*.css" />
</firstmatchmapper>
</apply>
<echo message="Done compiling scss files!" />
</target>
How can I pass SASS options as arguments? For instance I would like to compile the SASS to a --style:compressed state
You are already passing arguments to sass.bat. See the line
<arg value="--unix-newlines" />
You can add as many arg element as you need.
See http://ant.apache.org/manual/using.html#arg
<arg value="--unix-newlines" />
will work. So will other command-line arguments to the compiler as long as you include them as separate "arg"s

Exclude certain directories and files in pdepend, phpmd, phpcpd, phpcs, phpdoc, phploc

In my project there are certain directories and certain php files which are very large in size due to which my build is failing and I want to exclude them in my build.xml
Ques1- Do I have to write the --ignore="path/filename" for every php file in the project?
Ques2- There are certain files which are not php but .dat so should I mention these files in the --ignore too?
Ques3- Can I specify to exclude files based on their size such that I exclude all the files larger than 500kb?
My current xml file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<project name="name-of-project" default="build" basedir=".">
<property name="root.dir" value="${basedir}/.."/>
<property name="source" value="${root.dir}"/>
<target name="clean"
description="Clean up and create artifact directories">
<delete dir="${basedir}/build/api"/>
<delete dir="${basedir}/build/code-browser"/>
<delete dir="${basedir}/build/coverage"/>
<delete dir="${basedir}/build/logs"/>
<delete dir="${basedir}/build/pdepend"/>
<mkdir dir="${basedir}/build/api"/>
<mkdir dir="${basedir}/build/code-browser"/>
<mkdir dir="${basedir}/build/coverage"/>
<mkdir dir="${basedir}/build/logs"/>
<mkdir dir="${basedir}/build/pdepend"/>
</target>
<target name="phpunit"
description="Run unit tests using PHPUnit and generates junit.xml and clover.xml">
<exec executable="phpunit" failonerror="true">
<env key="DOCUMENT_ROOT" value="${source}/api"/>
<env key="MODEL_ROOT" value="${source}/model"/>
</exec>
</target>
<target name="parallelTasks"
description="Run the pdepend, phpmd, phpcpd, phpcs, phpdoc and phploc tasks in parallel using a maximum of 2 threads.">
<parallel threadCount="2">
<sequential>
<antcall target="pdepend"/>
<antcall target="phpmd"/>
</sequential>
<antcall target="phpcpd"/>
<antcall target="phpcs"/>
<antcall target="phpdoc"/>
<antcall target="phploc"/>
</parallel>
</target>
<target name="pdepend"
description="Generate jdepend.xml and software metrics charts using PHP_Depend">
<exec executable="pdepend">
<arg value="--jdepend-xml=${basedir}/build/logs/jdepend.xml" />
<arg value="--jdepend-chart=${basedir}/build/pdepend/dependencies.svg" />
<arg value="--overview-pyramid=${basedir}/build/pdepend/overview-pyramid.svg" />
<arg value="--ignore=${source}/web" />
<arg path="${source}" />
</exec>
</target>
<target name="phpmd"
description="Generate pmd.xml using PHPMD">
<exec executable="phpmd">
<arg path="${source}" />
<arg value="xml" />
<arg value="${basedir}/build/phpmd.xml" />
<arg value="--reportfile" />
<arg value="${basedir}/build/logs/pmd.xml" />
<arg value="--exclude" />
<arg value="${basedir}/web" />
</exec>
</target>
<target name="phpcpd"
description="Generate pmd-cpd.xml using PHPCPD">
<exec executable="phpcpd">
<arg value="--log-pmd" />
<arg value="${basedir}/build/logs/pmd-cpd.xml" />
<arg value="--exclude" />
<arg value="${basedir}/web" />
<arg path="${source}" />
</exec>
</target>
<target name="phploc"
description="Generate phploc.csv">
<exec executable="phploc">
<arg value="--log-csv" />
<arg value="${basedir}/build/logs/phploc.csv" />
<arg value="--exclude" />
<arg value="${basedir}/web" />
<arg path="${source}" />
</exec>
</target>
<target name="phpcs"
description="Generate checkstyle.xml using PHP_CodeSniffer">
<exec executable="phpcs" output="/dev/null">
<arg value="--report=checkstyle" />
<arg value="--report-file=${basedir}/build/logs/checkstyle.xml" />
<arg value="--standard=${basedir}/build/phpcs.xml" />
<arg value="--ignore=${source}/web" />
<arg path="${source}" />
</exec>
</target>
<target name="phpdoc"
description="Generate API documentation using PHPDocumentor">
<exec executable="phpdoc">
<arg value="--directory" />
<arg path="${source}" />
<arg value="--target" />
<arg path="${basedir}/build/api" />
<arg value="--ignore" />
<arg path="${basedir}/web" />
</exec>
</target>
<target name="phpcb"
description="Aggregate tool output with PHP_CodeBrowser">
<exec executable="phpcb">
<arg value="--log" />
<arg path="${basedir}/build/logs" />
<arg value="--source" />
<arg path="${source}" />
<arg value="--ignore" />
<arg path="${basedir}/web" />
<arg value="--output" />
<arg path="${basedir}/build/code-browser" />
</exec>
</target>
<target name="build" depends="clean,parallelTasks,phpunit,phpcb"/>
</project>
And I am getting errors like these
pdepend:
[exec] PHP_Depend 0.10.5 by Manuel Pichler
[exec] Parsing source files:
[exec] phpcpd 1.3.2 by Sebastian Bergmann.
[exec] PHP Fatal error: Allowed memory size of 262144000 bytes exhausted (tried to allocate 71 bytes) in /usr/share/pear/PHPCPD/Detector.php on line 115
[exec] PHP Stack trace:
[exec] PHP 1. {main}() /usr/bin/phpcpd:0
[exec] PHP 2. PHPCPD_TextUI_Command::main() /usr/bin/phpcpd:51
[exec] PHP 3. PHPCPD_Detector->copyPasteDetection() /usr/share/pear/PHPCPD/TextUI/Command.php:216
[exec] PHP 4. token_get_all() /usr/share/pear/PHPCPD/Detector.php:115
[exec] Result: 255
phploc:
[exec] phploc 1.6.1 by Sebastian Bergmann.
[exec] PHP Fatal error: Allowed memory size of 262144000 bytes exhausted (tried to allocate 71 bytes) in /usr/share/pear/PHPLOC/Analyser.php on line 279
[exec] PHP Stack trace:
[exec] PHP 1. {main}() /usr/bin/phploc:0
[exec] PHP 2. PHPLOC_TextUI_Command::main() /usr/bin/phploc:51
[exec] PHP 3. PHPLOC_Analyser->countFiles() /usr/share/pear/PHPLOC/TextUI/Command.php:215
[exec] PHP 4. PHPLOC_Analyser->countFile() /usr/share/pear/PHPLOC/Analyser.php:170
[exec] PHP 5. token_get_all() /usr/share/pear/PHPLOC/Analyser.php:279
[exec] Result: 255
What should I do to solve it? If I manually remove the large files from the directory then the build just passes but I want the build to run automatically checking out the code base from svn which will have these large files.
Ques4- Can I configure my checkout in such a way that these files dont get checked out?
For every target you can use fileset property or add exclude directories manually
<target name="phploc" description="Measure project size using PHPLOC">
<exec executable="phploc">
<arg value="--log-csv"/>
<arg value="${basedir}/build/logs/phploc.csv"/>
<arg value="--exclude"/>
<arg value="${src}/api/"/>
<arg value="--exclude"/>
<arg value="${src}/external/"/>
<arg path="${src}"/>
</exec>
</target>
<target name="pdepend"
description="Calculate software metrics using PHP_Depend">
<exec executable="pdepend">
<arg value="--jdepend-xml=${basedir}/build/logs/jdepend.xml"/>
<arg value="--jdepend-chart=${basedir}/build/pdepend/dependencies.svg"/>
<arg value="--overview-pyramid=${basedir}/build/pdepend/overview-pyramid.svg"/>
<arg value="--ignore=/dir/one/,/dir/two/"/>
<arg path="${src}"/>
</exec>
</target>
<target name="phpcpd" description="Find duplicate code using PHPCPD">
<exec executable="phpcpd">
<arg value="--log-pmd"/>
<arg value="${basedir}/build/logs/pmd-cpd.xml"/>
<arg value="--exclude"/>
<arg value="${src}/api/"/>
<arg value="--exclude"/>
<arg value="${src}/exclude/"/><arg path="${src}"/>
</exec>
</target>
Hope my answer will be helpful for someone.
In my project there are certain directories and certain php files
which are very large in size due to which my build is failing and I
want to exclude them in my build.xml
There are certain files which are not php but .dat so should I mention
these files in the --ignore too?
What I'm doing in this case is to make a copy of the needed files, and work from there. Either copy the whole directory and delete certain files, or copy over just the needed files. In the former case, the find program would do:
cp /my/large/project /tmp/work
find /tmp/work -name "*.dat" -delete
find /tmp/work -size 500k -delete
In the latter, I'm using ant's <copy> task:
<copy todir="${dir.source.our}" includeEmptyDirs="true">
<fileset dir="${dir.source}/ext" includes="${glob.source.our}"/>
</copy>
PHP Fatal error: Allowed memory size of 262144000 bytes exhausted
You'll have to increase PHP memory size in php.ini.
Search for memory_limit and set the value to something like 512 MB, e.g.
memory_limit = 512M

Custom Flex Ant build task

A beginner's question.
I'm building a .swf with Flex Ant.
To my .swf I link a file, target.as, which I generate from file source.txt with command
./tool.sh source.txt > target.as
How can I add what is described in the above sentence to my ant build process?
The exec task executes any external program:
<exec executable="${basedir}/tool.sh" dir="${basedir}" output="target.as">
<arg path="source.txt"/>
</exec>
So if you use the mxmlc ant task to compile your swf, you can define your build task like this:
<target name="build">
<exec executable="${basedir}/tool.sh" dir="${basedir}" output="target.as">
<arg path="source.txt"/>
</exec>
<mxmlc ....>
...
</mxmlc>
</target>
To run that command in Ant use the exec task.
<exec executable="tool.sh" dir="toolshdir" output="target.as">
<arg value="source.txt" />
</exec>
http://livedocs.adobe.com/flex/3/html/anttasks_1.html
You may also want to use the Flex "mxmlc" task instead of calling it with exec. You can do a lot of configuration right within the XML if you'd prefer not to have to maintain the shell script.

Resources