How to generate Avro .avsc files in ANT build - ant

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>

Related

CMake commands in Ant Script

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.

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

How to execute the JAXB compiler from ANT

I am using JAXB on a project. the attraction of JAXB is that it is bundled with the JDK, I have been to use xjc.exe on the command line to generate the .java files from a schema. I can't seem to find the JAXB ant task, sure there is a download at http://jaxb.java.net however i want to use the JAXB that is bundled into the JDK is there some way to call JAXB from ant what class does the xjc.exe call on?
<target name="generate-jaxb-code">
<java classname="com.sun.tools.internal.xjc.XJCFacade">
<arg value="-p" />
<arg value="com.example"/>
<arg value="xsd/sample.xsd" />
</java>
</target>
Just went hunting in the tools.jar and found the XJCFacade.class in com.sun.tools.internal tested the above code it works it produces the output as xjc.exe It seems that XJC.exe calls this code com.sun.tools.internal.xjc.XJCFacade
One of my key requirements was that the ant file had work within eclipse without having to include a path name to the JDK that way the file would be portable across operating systems. I am assuming that tools.jar is included on the classpath via the installed JRE preferences options.
Here is a helpful link:
https://jaxb.java.net/nonav/2.0.2/docs/xjcTask.html
Java SE 6 does not ship the Ant task (see 7.1.3):
https://jaxb.java.net/guide/Migrating_JAXB_2_0_applications_to_JavaSE_6.html
Essentially they do the following:
<target name="xjc" description="....">
<exec executable="${jdk.dir}/bin/xjc.exe">
<arg value="-d"/>
<arg value="${src.dir}"/>
<arg value="-p"/>
<arg value="com.mydomain.jaxb"/>
<arg value="${etc.dir}/myschema.xsd"/>
</exec>
</target>

How can I process multiple xsd schemas using jaxb and the Ant xjc task?

I'm using jaxb to generate java object class from xml schemas within an Ant script like so:
<!-- JAXB compiler task definition -->
<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask"
classpathref="master-classpath"/>
<!-- Generates the source code from the ff.xsd schema using jaxb -->
<target name="option-generate" description="Generates the source code">
<mkdir dir="${generated-src.dir}/${option.dir}"/>
<xjc schema="${config.dir}/ff.xsd" destdir="${generated-src.dir}"
package="${option.package.name}">
<arg value="-Xcommons-lang" />
<arg value="-Xcommons-lang:ToStringStyle=SHORT_PREFIX_STYLE" />
<produces dir="${generated-src.dir}" includes="**/*.java" />
</xjc>
</target>
Now, this works brilliantly for one schema (ff.xsd in this example). How can I process several schemas (i.e. several xsd files)?
I tried having a separate ant task per schema, but somehow, this doesn't work as Ant process the first task and then says that the "files are up to date" for the following schemas!
<target name="process-resources" description="Process resources">
<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask"/>
<xjc destdir="${basedir}/target/generated-sources/jaxb"
extension="true">
<schema dir="src/main/xsd"
includes="JaxbBindings.xsd,CoreTypes.xsd"/>
</xjc>
</target>
<target name="generate-jaxb-code">
<java classname="com.sun.tools.internal.xjc.XJCFacade">
<arg value="-p" />
<arg value="com.example"/>
<arg value="xsd/sample.xsd" />
</java>
</target>
works with the JAXB that is part of JDK 6 seems that the ANT task only ships with the downloadable JAXB but since JAXB is part of the JDK its probably not a good idea to take the latest release of the JAXB and add to the classpath of the JDK since that means you probably need to mess around with classloader settings, to pickup the downloaded version rather than the version within the JDK.
You can also just include the other xsd files in your main xsd file, using a command like the following:
<xs:include schemaLocation="path/to/secondschema.xsd"/>

Resources