Ant build to fail but always execute a target in Jenkins - ant

[Solved] - The correct ant contrib jar was not getting picked up from the default location on my system. Have to give path manually in the build xml like this:
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="/usr/share/ant/lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
-------------------------------------------
Q: I have a python script that is run through ant/Jenkins like this:
<project name="prjName">
<target name="preRun" description="do something">
....
</target>
<target name="Run" description="Run the python script">
<exec executable="python" failonerror="true">
<arg value="${basedir}/run.py" />
<arg value="something" />
</exec>
</target>
<target name="other1" description="do something">
....
</target>
<target name="other2" description="do something">
....
</target>
</project>
Now this python script runs an external tool (web-inject which produces some result files) and keeps on scanning for the word FAIL in the logs. As soon as it finds FAIL, it does sys.exit("Error")
Thus the build fails but I still want to execute the target - other1. Is it possible through try-catch? I am doing it like this but it isn't working
<macrodef name="test-case">
<sequential>
<trycatch>
<try>
<exec executable="python" failonerror="true">
<arg value="${basedir}/read.py" />
</exec>
</try>
<catch>
<echo>Investigate exceptions in the run!</echo>
</catch>
<finally>
<antcall target="other1" />
</finally>
</trycatch>
</sequential>
</macrodef>
<target name="other1" description="do something">
....
</target>

Related

how to test symlinks with ant 1.9

I need to perform a command if the jar file is a file instead of a symlink. I have found a solution that works only with ant 1.10.
Does anyone know how to do it with ant 1.9 ?
Here is my build.xml.
<?xml version="1.0"?>
<project name="AsterixDecoder" default="bm" basedir=".">
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<property environment="env"/>
<condition property="exists.CCM_ADDR">
<isset property="env.CCM_ADDR"/>
</condition>
<target name="compile" description="compile the source " >
<mkdir dir="${build}"/>
<javac srcdir="${src}" destdir="${build}" includeantruntime="false"/>
<mkdir dir="${build}/resources"/>
<copy todir="${build}/resources">
<fileset dir="resources"/>
</copy>
</target>
<target name="checkout" if="exists.CCM_ADDR">
<ccmcheckout file="${dist}/AsterixDecoder.jar"/>
</target>
<target name="dist" depends="compile, checkout"
description="generate the distribution" >
<jar jarfile="${dist}/AsterixDecoder.jar" filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class" value="fr.eurocontrol.escape.ground.asterixdecoder.AsterixDataTree"/>
<attribute name="Class-Path" value="."/>
</manifest>
<fileset dir="${build}"/>
</jar>
</target>
<target name="check.symlink">
<fileset dir="${dist}" id="fileset" includes="AsterixDecoder.jar">
<symlink/>
</fileset>
<pathconvert refid="fileset" property="is.symlink" setonempty="false"/>
</target>
<target name="reconcile" depends="check.symlink" if="exists.CCM_ADDR" unless="is.symlink">
<exec executable="ccm">
<arg value="reconcile"/>
<arg value="-udb"/>
<arg value="${dist}/AsterixDecoder.jar"/>
</exec>
</target>
<target name="bm" description="build management" depends="dist, reconcile">
</target>
</project>
Do not hesitate to make any suggestion of improvements. I am still a beginner in writing ant files.
The most straightforward way to do this would be to use the record function of Ant's symlink task. This creates a property file that lists all of the symlinks found within a given resource collection. Here's an example target:
<target name="default">
<symlink link="testdir" resource="build" />
<symlink action="record" linkfilename="links.record">
<fileset dir="." includes="*" />
</symlink>
<property file="links.record" />
<condition property="testdir.is.symlink">
<isset property="testdir" />
</condition>
<echo message="${testdir.is.symlink}" />
</target>

XJC: migrating ant task from JAXB to JDK 1.6

We have migrated to Java 1.6 and as part of the there is a re-write of the ant tasks that used the older xjc ant task to use the executable: xjc.exe provided in Java 1.6
We also need to retain older ant task parameters like using commons-lang plugin to generate toString() methods in the generated value objects.
Earlier:
<target name="generate_vos" description="Compile all Java source files">
<echo message="Compiling the schema..." />
<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
<classpath refid="ERPSimulator.classpath"/>
</taskdef>
<delete dir="${jaxb.src}" />
<mkdir dir="${jaxb.src}" />
<xjc schema="${jaxb.schema}/SOAPClientObjects.xsd" package="xxx.jaxb.vo" destdir="${jaxb.src}">
<arg value="-Xcommons-lang" />
<arg value="-Xcommons-lang:ToStringStyle=SIMPLE_STYLE" />
<produces dir="${jaxb.src}" includes="**/*.java" />
</xjc>
</target>
Now:
<target name="generate_vos" description="Compile all Java source files">
<delete dir="${jaxb.src}" />
<mkdir dir="${jaxb.src}" />
<echo message="Compiling the schema..." />
<exec executable="xjc">
<arg value="-extension"/>
<arg value="-d"/>
<arg value="${jaxb.src}"/>
<arg value="-p"/>
<arg value="xxxx.jaxb.vo"/>
<arg value="${jaxb.schema}/SOAPClientObjects.xsd"/>
<arg value="-Xcommons-lang"/>
<arg value="-Xcommons-lang:ToStringStyle=SIMPLE_STYLE" />
</exec>
</target>
However, running the new task results in errors as the -Xcommons* plugins are not carried forward in this version.
I have explicitly set the plugin jar files for the commons-lang toString plugin in the path too with no luck.
Any idea how to make XJC.exe generate the toString() method for the resultant Objects?
Thanks!

Ant jar file for swing program

<?xml version="1.0" ?>
<project name="javaGui" default="execute">
<target name="init" depends="clean">
<mkdir dir="build/classes" />
<mkdir dir="dist" />
</target>
<target name="compile" depends="init">
<javac srcdir="src" destdir="build/classes" />
</target>
<target name="execute" depends="compile">
<java classname="Swing" classpath="build/classes" />
<jar destfile="dist/final.jar" basedir="build/classes" />
</target>
<target name="clean">
<delete dir="build" />
<delete dir="dist" />
</target>
This is ant script to generate jar file.problem is those code will generate jar but when i click on that jar it is not opening means it not showing any GUI.
im new to this please let me know what is going wrong.
javaGUI is project and Swing is class name
jar file will not open in GUI. You need to run jar file from console.
Go to dist directory -> run this command:
$ java -jar final.jar [optional parameters]
For more details : see this reference
UPDATE
Instead of giving ant from target execute, try this :
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="Swing"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/HelloWorld.jar" fork="true"/>
</target>

problem with ant script properties

The following is my ant script:
<project name="nightly_build" default="main" basedir="C:\Work\6.70_Extensions\NightlyBuild">
<target name="init">
<sequential>
<exec executable="C:/Work/Searchlatestversion.exe">
<arg line='"/SASE Lab Tools" "6.70_Extensions/6.70.102/ANT_SASE_RELEASE_"'/>
</exec>
<property file="C:/Work/latestbuild.properties"/>
<sleep seconds="10"/>
<echo message="The product version is ${Product_Version}"/>
<exec executable="C:/Work/checksnapshot.exe">
<arg line='"ANT_SASE_RELEASE_${Product_Version}_SASE Lab Tools-NightlyBuild" ANT_SASE_RELEASE_${Product_Version}_AnalyzerCommon-NightlyBuild ${Product_Version}-AppsMerge' />
</exec>
<property file="C:/Work/checksnapshot.properties"/>
<tstamp>
<format property="suffix" pattern="ddMMyyyyHHmm"/>
</tstamp>
</sequential>
</target>
<target name="main" depends="init">
<echo message="loading properties files.." />
<sleep seconds="10"/>
<echo message="Backing up folder" />
<move file="C:\NightlyBuild\NightlyBuild" tofile="C:\NightlyBuild\NightlyBuild.${suffix}" failonerror="false" />
<parallel>
<exec executable="C:/Work/sortfolder.exe">
<arg line="6" />
</exec>
<exec executable="C:/Work/6.70_Extensions/NightlyBuild/antc.bat">
</exec>
</parallel>
</target>
</project>
Basically the sequence goes something like this:
I will run Searchlatestversion.exe and write latestbuild.properties
Using the latestbuild.properties i will obtain ${Product_Version} and would like to allow checksnapshot.exe access to latestbuild.properties and obtain ${Product_Version}
checksnapshot.exe will then generate checksnapshot.properties which will then be used by the target in main antc.bat
am i doing something wrong over here? seems like ${Product_Version} is not being received well by checksnapshot.exe
You appear to have a hard coded wait period of 10 seconds for Searchlatestversion to write out your file. If the executable does not complete inside that time, ${Product_Version} cannot be read from file.
Have you considered using the Waitfor Ant Task? As the name implies, this will wait for a certain condition before it will allow the rest of the task to progress. You could do something like
<property name="props.file" value="C:/Work/latestbuild.properties"/>
<waitfor maxwait="10" maxwaitunit="second">
<available file="${props.file}"/>
</waitfor>
<property file="${props.file}"/>
Does Searchlatestversion.exe produce the file C:/Work/latestbuild.properties?
If so, should you not sleep/wait before you load that properties file?
You have this:
<exec .../>
<property file="C:/Work/latestbuild.properties"/>
<sleep seconds="10"/>
Should you not have this:
<exec ... />
<sleep seconds="10"/>
<property file="C:/Work/latestbuild.properties"/>

How to macro-ify ant targets?

I want to be able to have different targets doing nearly the same thing, as so:
ant build <- this would be a normal (default) build
ant safari <- building the safari target.
The targets look like this:
<target name="build" depends="javac" description="GWT compile to JavaScript">
<java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">
<classpath>
<pathelement location="src"/>
<path refid="project.class.path"/>
</classpath>
<jvmarg value="-Xmx256M"/>
<arg value="${lhs.target}"/>
</java>
</target>
<target name="safari" depends="javac" description="GWT compile to Safari/JavaScript">
<java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">
<classpath>
<pathelement location="src"/>
<path refid="project.class.path"/>
</classpath>
<jvmarg value="-Xmx256M"/>
<arg value="${lhs.safari.target}"/>
</java>
</target>
(Nevermind the first thought that strikes: throw out ant! That's not an option just yet.) I tried using macrodef, but got a strange error message (even though the message didn't imply it, it think it had to do with putting a target in sequential). I don't want write a cmdline as so: ant -Dwhatever=nevermind. Any ideas?
My first try (without being able to test it at the moment):
<target name="build" depends="javac, create.mymacro" description="GWT compile to JavaScript">
<mymacro target="${lhs.target}"/>
</target>
<target name="safari" depends="javac, create.mymacro" description="GWT compile to Safari/JavaScript">
<mymacro target="${lhs.safari.target}"/>
</target
<target name="create.mymacro">
<macrodef name="mymacro">
<attribute name="target" default="${lhs.target}"/>
<sequential>
<java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">
<classpath>
<pathelement location="src"/>
<path refid="project.class.path"/>
</classpath>
<jvmarg value="-Xmx256M"/>
<arg value="#{target}"/>
</java>
</sequential>
</macrodef>
</target>

Resources