CMake commands in Ant Script - ant

I have a CMakeLists.txt file which I want to make by using ant script. Is there a way to give commands for cmake . and make through ant? I used to have a MakeFile and I had the following command to make it:
<?xml version="1.0" encoding="UTF-8"?>
<project name="sampleProject" default="make">
<target name="make">
<exec executable="make" failonerror="true">
<arg line="-f Makefile"/>
</exec>
</target>
</project>
Is it somehow similar to this? Does anyone know how to do it? I am looking for an alternative to cmakeant

Invoking CMake from Ant should be analogical. Something like this:
<target name="cmake">
<mkdir dir="build" />
<exec executable="cmake" dir="build" failonerror="true">
<arg line="../" />
</exec>
</target>
Basically, it creates build subdirectory and invokes CMake from it. Passing some other CMake arguments should be trivial aswell.
Also, You might check projects like cmakeant, for more sophisticated CMake wrapper.
NOTE: invoking CMake in source directory is considered to be a bad practice which causes pollution of source subdirectory.

Related

How to generate Avro .avsc files in ANT build

I need to generate files from .avsc file (avro) in ANT build. I'm not sure what plugin should I use for this. I tried with xjc.XJC2Task for generating the files but it seem to not work. Can some one suggest what plugin / class should I use to generate the files from avro schema in ANT ?
Tried with xjc:
<target name="generate" description="generate">
<mkdir dir="generated" />
<taskdef name="xjc" classname="com.sun.tools.xjc.XJC2Task" classpathref="classpath"/>
<xjc destdir="generated">
<schema dir="setup/kafka" includes="*.avsc" />
</xjc>
</target>
Error:
generate:
[xjc] Consider using <depends>/<produces> so that XJC won't do unnecessary compilation
[xjc] Compiling file:/D:/2019/My-workspace/my-project/setup/kafka/Sample.avsc
[xjc] [ERROR] Content is not allowed in prolog.
[xjc] line 1 of file:/D:/2019/My-workspace/my-project/setup/kafka/Sample.avsc
[xjc] failure in the XJC task. Use the Ant -verbose switch for more details
Used java jar command to compile the avro-tools jar . it worked.
<target name="run-avro">
<java jar="lib/avro-tools-1.10.1.jar" fork="true">
<arg line="compile schema"/>
<arg file="avrolocation/Sample.avsc"/>
<arg file="generated/"/>
</java>
</target>

How to specify lib directory for ant task?

I'm currently running an exec task in my build like this:
<target name="bar">
<exec executable="ant">
<arg value="-f"/>
<arg value="/path/to/my/build.xml"/>
<arg value="-lib"/>
<arg value="/path/to/my/libs"/>
</exec>
</target>
I don't really like it and want to replace the exec task with an ant task:
<target name="bar">
<ant antfile="/path/to/my/build.xml"/>
</target>
However, I don't know how to specify the lib directory in this case. Is this possible somehow?
What are you trying to achieve, by launching ANT from within ANT in this manner?
For example if you need custom ANT extensions, the path to these jars can be specified at runtime within the code as follows:
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${lib.dir}/ant-contrib-0.3.jar"/>
</classpath>
</taskdef>
Better again, you could integrate a dependency management system like Apache ivy to manage 3rd party jar dependencies.
You can call ant script inside an ant script like below.
If you use the attribute inheritrefs="true" any Ids that are set in the parent build script will be passed to child build script as well.
Eg:
<ant antfile="subbuild.xml" inheritrefs="true"/>

Apache Ant: Unable to call Composer's vendor/bin scripts

I am using Apache Ant for my builds. I have some composer scripts belonging to several vendors in vendor/bin folder. I have added this folder to the system path and if I run the commands on my command window in works but in the build file i get an error.
Is there anything I should be doing differently? Before is an example:
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" default="build" basedir=".">
<target name="phpcpd" description="Find duplicate code using PHPCPD">
<exec executable="phpcpd">
<arg value="--version" />
</exec>
</target>
</project>'
I get this when I run ant phpcpd
phpcpd:
BUILD FAILED
C:\xxxxxx\xxxxxxx\build.xml:96: Execute failed: java.io.IO Exce
ption: Cannot run program "phpcpd": CreateProcess error=2, The system
cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
at java.lang.Runtime.exec(Runtime.java:617)
at org.apache.tools.ant.taskdefs.launcher.Java13CommandLauncher.exec(Jav
a13CommandLauncher.java:41)
at org.apache.tools.ant.taskdefs.Execute.launch(Execute.java:428)
at org.apache.tools.ant.taskdefs.Execute.execute(Execute.java:442)
at org.apache.tools.ant.taskdefs.ExecTask.runExecute(ExecTask.java:628)
at org.apache.tools.ant.taskdefs.ExecTask.runExec(ExecTask.java:669)
at org.apache.tools.ant.taskdefs.ExecTask.execute(ExecTask.java:495)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
...
But phpcpd --version works on command prompt
ANT is not aware where phpcpd is as it doesn't share path with your Command Promt.
One way around it is to create a .bat file to run phpcpd
Create a phpcpd.bat with the following:
#echo off
phpcpd --version
Your build scripts to be updated from :
<exec executable="phpcpd">
<arg value="--version" />
</exec>
To: <exec executable="phpcpd.bat"/>
Above assumed with Windows Command Promt
My solution was to use the .phar files of the scripts. That way, the build file became platform independent to a large extent. So
<target name="phpcpd" description="Find duplicate code using PHPCPD">
<exec executable="phpcpd">
<arg value="--version" />
</exec>
</target>
Became:
<target name="phpcpd" description="Find duplicate code using PHPCPD">
<exec executable="php">
<arg value="${phpcpd}" />
<arg value="--version" />
</exec>
</target>
Where ${phpcpd} is the path to the phar file
I'm using absolute paths and a configurable executable.properties like this:
build.xml
<project name="build">
<property file="executable.properties" />
<target name="run-phpcd" unless="${phpcpd.skip}">
<exec executable="${phpcpd.executable}"><!-- .. --></exec>
</target>
</project>
executable.dist.properties
phpcpd.skip = no
#phpcpd.executable = C:\path\to\phpcpd.bat
phpcpd.executable = /path/to/phpcpd.sh
Both files can be committed to your VCS, for usage copy the template file (*.dist.properties) and rename it to executable.properties. Add this file to the ignore list of your VCS.

JSCover - Excluding coverage files

Currently trying to get JSCover to exclude js files that are used as libraries. I have a set of ant scripts below which will
start the JSCover server
Run & Generate Json report
Stop the server
Finally, i have a shell command to convert the Json file to LCov so that i can use it with sonarqube. I also get coverage in jscoverage.html but it includes every file under web/ which is something i do not want. Image below
Ant scripts below:
<target name="jstest-start">
<java jar=".../JSCover.jar" fork="true" spawn="true">
<arg value="-ws"/>
<arg value="--report-dir=coverage"/>
<arg value="--document-root=web"/>
<arg value="--port=8082"/>
<!-- Aim is to exclude folder js under web/ as it contains libraries, not source code. Currently does not work -->
<arg value="--no-instrument=web/js/"/>
<arg value="--no-instrument=js/"/>
</java>
<waitfor maxwait="5" maxwaitunit="second" checkevery="250" checkeveryunit="millisecond" timeoutproperty="failed">
<http url="http://localhost:8082/jscoverage.html"/>
</waitfor>
<fail if="failed"/>
</target>
<target name="jstest-run">
<exec dir="/usr/local/CI/phantomjs/bin/" executable="phantomjs" failonerror="true">
<arg line=".../run-jscover-qunit.js http://localhost:8082/index.html"/>
</exec>
</target>
<target name="jstest-stop">
<get src="http://localhost:8082/stop" dest="stop.txt" />
</target>
<target name="jstest" description="Run javascript tests">
<antcall target="jstest-start"/>
<antcall target="jstest-run"/>
<antcall target="jstest-stop"/>
</target>
My folder structure is:
And finally, my sonar standalone analysis settings:
So, what seems to be happening is that JSCover is recursively reading for all js files and i cannot prevent that from sonar or ant.
Can anyone shed some light?
<arg value="--no-instrument=/js/"/>
should work, and to remove the test itself,
<arg value="--no-instrument=/test/"/>
The paths are as seen by the web-server, so the 'web' prefix in:
<arg value="--no-instrument=web/js/"/>
has no effect.
i have resolved my own issue by correcting the shell command which generates an LCOV report.
java -cp JSCover.jar jscover.report.Main --format=LCOV /usr/local/CI/jenkins/workspace/PhantomJS/coverage/phantom/ /usr/local/CI/jenkins/workspace/PhantomJS/web/
Prior to this, the SRC-DIR and REPORT-DIR were the same which was an error on my part. As far as i can understand, SRC-DIR should point to the source folder and REPORT-DIR should point to where the lcov file exists.
I hope this helps someone

Writing Ant build file for several Jenkins jobs

I'm using Jenkins with Ant plug-in to run PHPUnit/Selenium tests. I'm trying to set up several Jenkins jobs (I've only had one job previously).
Tests for these jobs are in the same GitHub repo, but different folders.
So, I could create different Ant targets in my build.xml, but do I need
separate phpunit.xml files for each job (and if so, how do I specify file names in Ant build script?) Or is there a way to make Ant
distinguish between tests in the same phpunit.xml file? Any other good way to go about this? Any examples would be appreciated.
Ant build file:
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" default="build">
<target name="build" depends="clean,prepare,phpunit"/>
<target name="clean" description="Cleanup build artifacts">
<delete dir="${basedir}/build"/>
</target>
<target name="prepare" description="Make log and coverage directories">
<mkdir dir="${basedir}/build/logs"/>
<mkdir dir="${basedir}/build/coverage_selenium"/>
</target>
<target name="phpunit" description="MyTests">
<exec dir="${basedir}" executable="phpunit" failonerror="true"/>
</target>
</project>
phpunit.xml:
<phpunit>
<testsuites>
<testsuite name="MyTests">
<file>path/to/test.php</file>
</testsuite>
</testsuites>
</phpunit>
Thanks!
You can specify the test configuration file using -c or --configuration. The Ant exec task lets you specify arguments for the process you want to run, something like:
<exec dir="${basedir}" executable="phpunit" failonerror="true">
<arg value="-c" />
<arg value="php_unit_1.xml"/>
<exec>
I recommend creating a separate build.xml and phpunit.xml for each project. You can define common targets in a central build-base.xml that you include in each to avoid duplication. Unfortunately, there's no equivalent mechanism for phpunit.xml that I know of.

Resources