How to execute the JAXB compiler from ANT - 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>

Related

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"/>

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

Ant task to compile stylus file to normal css file?

I am trying to use stylus in a project, that is using ANT build.
I need to compile the stylus file to normal css before packaging the war.
is there a way to do it?
Note : I am not using node.js, it is a simple java web application
Here is a full example how to use it:
<property name="css.stylus" value="../folder/to/stylus/files"/>
<property name="cmd.node" value="C:\Program Files\nodejs\nodevars.bat"/>
<property name="cmd.stylus" value="C:\Program Files\nodejs\stylus.cmd"/>
<target name="css_compile" description="compile l'ensemble des CSS avec stylus">
<exec executable="${cmd.node}" />
<exec dir="${css.stylus}" executable="${cmd.stylus}" >
<arg value="myCSS.styl"/>
<arg value="-c"/>
<arg value="-o"/>
<arg value="..\cssFolder"/>
</exec>
</target>
This example (I used) transform myCSS.styl in a obfuscate CSS to /cssFolder
nodevars.bat is necessary for launching stylus.
stylus.cmd is stylus itself, because the path not working in ant. To know where is it, (it's in nodejs), you can type "path" in terminal, on Windows.
That's all!

ant java jar classpath problem

<target name="results">
<echo message="Calculating QI" />
<java jar="jmt.jar" fork="true" failonerror="true" maxmemory="1024m" classpath="jmt/jmt">
<arg value="-name:KIS"/>
<arg value="-results:CONSOLE"/>
<arg value="../allJavas.jar"/>
</java>
</target>
i want from folder tmp run jar file in folder jmt/jmt. It must be run inside jmt/jmt folder becouse of dependencies files.
i can run it like <java jar="jmt/jmt/jmt.jar" but then dependencies files are not ok. I try to use classpath but not working. What i am doing wrong?
Use the dir="jmt/jmt" attribute to specify the folder to launch the java process in, and use jar="jmt/jmt/jmt.jar" to specify the jar. You probably don't need to classpath attribute at all.
See http://ant.apache.org/manual/Tasks/java.html
The java ant task takes an option parameter dir="jmt/jmt" that will tell the forked VM where to execute.

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