If isset in apache ant is failing - apache

I am trying to check if a property exists in ant with the following:
<target name="test">
<property name="testproperty" value="1234567890"/>
<if>
<isset property="testproperty"/>
<then>
<echo message="testproperty exists"/>
</then>
<else>
<echo message="testproperty does not exist"/>
</else>
</if>
</target>
The result is a failure with the message:
build.xml:536: Problem: failed to create task or type if
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
I must be doing something wrong with isset as the following runs smoothly:
<target name="test">
<property name="testproperty" value="1234567890"/>
<echo message="'${testproperty}'"/>
</target>
Please advice :)

The if task is not a standard ANT task. This is how conditional execution of a target works
<project name="demo" default="test">
<target name="property-exists" if="testproperty">
<echo message="testproperty exists"/>
</target>
<target name="property-no-exists" unless="testproperty">
<echo message="testproperty does not exist"/>
</target>
<target name="test" depends="property-exists,property-no-exists">
</target>
</project>

Apparently I was missing the ant-contrib jar file that can be found here:
http://ant-contrib.sourceforge.net/

You've found the missing jar, now you need to define the tasks that use that jar. I would put the jar under the antlib/antcontrib directory inside your project. This way, others who download your project will have the needed jar.
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<fileset dir="${basedir}/antlib/antcontrib"/>
</classpath>
</taskdef>
If you use a lot of optional jar files, you might want to use namespaces:
<project name="..." basedir="." default="...."
xmlns:ac="antlib://net/sf/antcontrib">
<taskdef resource="net/sf/antcontrib/antlib.xml"
uri="antlib://net/sf/antcontrib">
<classpath>
<fileset dir="${basedir}/antlib/antcontrib"/>
</classpath>
</taskdef>
Now, when you use a antcontrib task, you have to preface it with ac:. This allows optional jars to be used without namespace collisions.
<target name="test">
<property name="testproperty" value="1234567890"/>
<ac:if>
<isset property="testproperty"/>
<then>
<echo message="testproperty exists"/>
</then>
<else>
<echo message="testproperty does not exist"/>
</else>
</ac:if>
</target>

Related

ant junit selenium wont run in jenkins

I have junit tests that use selenium to test web server.
When i run the tests using ant from command line, everything is working fine, browser gets opened and tests are going as planed.Browser gets open and i can see tests running.
Recently ive tried to add automatic tests as part of Ci cycle running on jenkins.
I run it as ant build command.
I can see that ant is executing properly (test classes are built i can see output from tests to console) but browser window never gets opened and test fails because of it. here is my ant file
<?xml version="1.0"?>
<project name="JUNIT" default="main" basedir="../../project" xmlns:artifact="antlib:org.apache.maven.artifact.ant">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="api.dir" location="src/java" />
<property name="build.api.dir" location="target/classes" />
<property name="test.dir" location="src/test/java" />
<property name="build.test.dir" location="target" />
<!-- Variables used for JUnit testin -->
<property name="test.report.dir" location="testreport" />
<!-- Define the classpath which includes the junit.jar and the classes after compiling-->
<path id="api.class.path">
<pathelement location="${build.api.dir}" />
</path>
<artifact:dependencies cacheDependencyRefs="true" pathId="pomdeps.path">
<pom file="pom.xml"/>
</artifact:dependencies>
<target name="clean">
<delete dir="${test.report.dir}" />
<delete dir="${build.api.dir}" />
<delete dir="${build.test.dir}" />
</target> <!-- Creates the build, docs and dist directory-->
<target name="makedir">
<echo message="Make dir"/>
<mkdir dir="${build.test.dir}" />
<mkdir dir="${build.api.dir}" />
<mkdir dir="${test.report.dir}" />
</target> <!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir">
<echo message="Compile"/>
<javac srcdir="${api.dir}" destdir="${build.api.dir}" includeantruntime="false">
<!--classpath refid="junit.class.path" />
<classpath refid="libs.class.path" /-->
<classpath refid="pomdeps.path" />
</javac>
<javac srcdir="${test.dir}" destdir="${build.test.dir}" includeantruntime="false">
<!--classpath refid="junit.class.path" /-->
<classpath refid="api.class.path" />
<classpath refid="pomdeps.path" />
</javac>
</target>
<!-- Run the JUnit Tests --> <!-- Output is XML, could also be plain-->
<echo message="Classes folder ${build.test.dir}"/>
<target name="junit" depends="compile" >
<echo message="junit"/>
<junit printsummary="on" fork="false" haltonfailure="no" showoutput="true">
<classpath refid="pomdeps.path" />
<classpath>
<pathelement location="${build.test.dir}"/>
<pathelement location="${build.api.dir}"/>
</classpath>
<formatter usefile="false" type="plain"/>
<batchtest fork="no" todir="${test.report.dir}">
<fileset dir="${test.dir}">
<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
</target>
<target name="main" depends="compile, junit">
<description>Main target</description>
</target>
</project>
Jenkins is 1.591 i installed it with default parameters as windows installation downloaded from their site.
Can it be something wrong with jenkins? Do i miss something?
As i mentioned earlier the problem was lack of UI permissions for Jenkins server.
1.configure Jenkins to run as service and make it login with real user name
2.Make sure that windows host that runs Jenkins logs on automaticaly after restart.

TestNG Selenium Test fails in Ant through command prompt

I am using Selenium testNG in eclipse to run my test cases.The testNg.xml file executes the test correctly.
But when I try to run in command prompt through ant the build is success but the test fails.
one peculiar thing happening is : when the driver i use to test is HtmlUnitdriver build is successful and also the test passes . But it fails when i use other browsers like firefox, chrome, explorer in my test case.
the build.xml i use is:
<project name="Automation" default="clean" basedir=".">
<property name="build.dir" value="${basedir}/build"/>
<property name="lib.dir" value="${basedir}/lib"/>
<property name="src.dir" value="${basedir}/src"/>
<target name="setClassPath">
<path id="classpath_jars">
<pathelement path="${basedir}/" />
<fileset dir="${lib.dir}" includes="*.jar" />
</path>
<pathconvert pathsep=":" property="test.classpath" refid="classpath_jars" />
</target>
<target name="loadTestNG" depends="setClassPath">
<taskdef resource="testngtasks" classpath="${test.classpath}"/>
</target>
<target name="init">
<mkdir dir="${build.dir}"/>
<tstamp>
<format property="timestamp" pattern="dd-MM-yyyy_(HH-mm-ss)"/>
</tstamp>
<property name="build.log.dir" location="${basedir}/buildlogs"/>
<mkdir dir="${build.log.dir}"/>
<property name="build.log.filename" value="build_${timestamp}.log"/>
<record name="${build.log.dir}/${build.log.filename}" loglevel="verbose" append="false"/>
<echo message="build logged to ${build.log.filename}"/>
</target>
<target name="clean">
<echo message="deleting existing build directory"/>
<delete dir="${build.dir}"/>
</target>
<target name="compile" depends="clean,init,setClassPath,loadTestNG">
<echo message="classpath:${test.classpath}"/>
<echo message="compiling.........."/>
<javac destdir="${build.dir}" srcdir="${src.dir}" classpath="${test.classpath}"/>
</target>
<target name="runTests" depends="compile">
<testng classpath="${test.classpath}:${build.dir}">
<xmlfileset dir="${basedir}" includes="testNg.xml"/>
</testng>
</target>
</project>
I appreciate any help.

java.lang.NoClassDefFoundError when trying to run JUnit tests from ANT

I'm having problems with a java.lang.NoClassDefFoundError when trying to run my JUnit tests from ANT using the runtestsreport task in thefollowing build.xml; I know the other tasks work, it's just the actual running of the tests that's the problem.
I've tried lots of adjustments based on questions posted on here but I can't fix it; what I really need is a dev to pair with but they've all gone home for the day.
Can anyone help?
The build.xml file is
<property name="build" location="C:\<path on my machine>\workspace\ESS\build"/>
<property name="testsbuild" location="C:\<path on my machine>\workspace\ESS\build\esstestapp\tests"/>
<property name="frameworkbuild" location="C:\<path on my machine>\workspace\ESS\build\esstestapp\framework"/>
<property name="rawtestreports" location="C:\<path on my machine>\workspace\ESS\reports\raw"/>
<property name="htmltestreports" location="C:\<path on my machine>\workspace\ESS\reports\html"/>
<property name="reports" location="C:\<path on my machine>\workspace\ESS\reports"/>
<property name="src" location="C:\<path on my machine>\workspace\ESS\src"/>
<property name="testsrc" location="C:\<path on my machine>\workspace\ESS\src\esstestapp\tests"/>
<property name="seleniumtools" location="C:\<path on my machine>\workspace\ESS\tools\junit-4.8.2"/>
<property name="junittools" location="C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2"/>
<target name="init">
<tstamp/>
<mkdir dir="${build}"/>
<mkdir dir="${rawtestreports}"/>
<mkdir dir="${htmltestreports}"/>
</target>
<path id="tools.classpath">
<fileset dir="${seleniumtools}" includes="**/*.jar"/>
<fileset dir="${junittools}" includes="**/*.jar"/>
</path>
<target name="compile" depends="init" description="compile the source" >
<javac srcdir="${src}" destdir="${build}" classpathref="tools.classpath" includeantruntime="false"/>
</target>
<target name="frameworkjar" depends="compile">
<jar destfile="${frameworkbuild}/jar/framework.jar" basedir="${frameworkbuild}" />
</target>
<target name="testsjar" depends="compile">
<jar destfile="${testsbuild}/jar/tests.jar" basedir="${testsbuild}" />
</target>
<path id="test.classpath">
<fileset dir="${seleniumtools}" includes="**/*.jar"/>
<fileset dir="${junittools}" includes="**/*.jar"/>
<fileset dir="${testsbuild}/jar" includes="**/*.jar"/>
<fileset dir="${frameworkbuild}/jar" includes="**/*.jar"/>
</path>
<target name="runtests" depends="compile, frameworkjar, testsjar" description="runs the tests" >
<junit printsummary="yes" haltonfailure="no" showoutput="yes" fork="no">
<classpath refid="test.classpath" />
<batchtest fork="yes" todir="${rawtestreports}">
<formatter type="xml"/>
<fileset dir="${testsrc}">
<include name="**/*.java"/>
</fileset>
</batchtest>
</junit>
</target>
<target name="runtestsreport" depends="runtests">
<junitreport todir="${reports}">
<fileset dir="${rawtestreports}">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${htmltestreports}" />
</junitreport>
</target>
<target name="clean" description="Clean up">
<delete dir="${build}"/>
<delete dir="${reports}"/>
</target>
I get this error (and this type of error for each test class)
Credentials (wrong name: esstestapp/tests/Credentials)
java.lang.NoClassDefFoundError: Credentials (wrong name: esstestapp/tests/Credentials)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)...
I've ran ant in -verbose mode to check the classpath out but I can't see the problem (even though I know that it's staring me in the face)
[junit] Implicitly adding C:\Program Files\apache-ant-1.8.2\lib\ant-launcher
.jar;C:\Program Files\apache-ant-1.8.2\lib\ant.jar;C:\Program Files\apache-ant-1
.8.2\lib\ant-junit.jar;C:\Program Files\apache-ant-1.8.2\lib\ant-junit4.jar to C
LASSPATH
[junit] Executing 'C:\Program Files\Java\jdk1.6.0_25\jre\bin\java.exe' with
arguments:
[junit] '-classpath'
[junit] Executing 'C:\Program Files\Java\jdk1.6.0_25\jre\bin\java.exe' with
arguments:
[junit] '-classpath'
[junit] 'C:\<path on my machine>\workspace\ESS\tools\junit-4.8.2\junit.jar;C:\User
s\waltersj\workspace\ESS\tools\junit-4.8.2\junitsrc.jar;C:\<path on my machine>\worksp
ace\ESS\tools\selenium-2.0rc2\apache-mime4j-0.6.jar;C:\<path on my machine>\workspace\
ESS\tools\selenium-2.0rc2\bsh-1.3.0.jar;C:\<path on my machine>\workspace\ESS\tools\se
lenium-2.0rc2\cglib-nodep-2.1_3.jar;C:\<path on my machine>\workspace\ESS\tools\seleni
um-2.0rc2\commons-codec-1.4.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2
.0rc2\commons-collections-3.2.1.jar;C:\<path on my machine>\workspace\ESS\tools\seleni
um-2.0rc2\commons-io-2.0.1.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.
0rc2\commons-lang-2.4.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\
commons-logging-1.1.1.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\
cssparser-0.9.5.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\guava-
r09.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\hamcrest-all-1.1.j
ar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\htmlunit-2.8.jar;C:\Use
rs\waltersj\workspace\ESS\tools\selenium-2.0rc2\htmlunit-core-js-2.8.jar;C:\User
s\waltersj\workspace\ESS\tools\selenium-2.0rc2\httpclient-4.0.2.jar;C:\Users\wal
tersj\workspace\ESS\tools\selenium-2.0rc2\httpcore-4.0.1.jar;C:\<path on my machine>\w
orkspace\ESS\tools\selenium-2.0rc2\httpmime-4.0.1.jar;C:\<path on my machine>\workspac
e\ESS\tools\selenium-2.0rc2\jcommander-1.13.jar;C:\<path on my machine>\workspace\ESS\
tools\selenium-2.0rc2\jna.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0
rc2\json-20080701.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\juni
t-dep-4.8.1.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\nekohtml-1
.9.14.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\sac-1.3.jar;C:\U
sers\waltersj\workspace\ESS\tools\selenium-2.0rc2\selenium-java-2.0rc2-srcs.jar;
C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\selenium-java-2.0rc2.jar;C
:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\serializer-2.7.1.jar;C:\Use
rs\waltersj\workspace\ESS\tools\selenium-2.0rc2\testng-6.0.1-nobsh-noguice.jar;C
:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\xalan-2.7.1.jar;C:\Users\wa
ltersj\workspace\ESS\tools\selenium-2.0rc2\xercesImpl-2.9.1.jar;C:\Users\walters
j\workspace\ESS\tools\selenium-2.0rc2\xml-apis-1.3.04.jar;C:\<path on my machine>\work
space\ESS\build\esstestapp\tests\jar\tests.jar;C:\<path on my machine>\workspace\ESS\b
uild\esstestapp\framework\jar\framework.jar;C:\<path on my machine>\workspace\ESS;C:\P
rogram Files\Java\jre6\lib\ext\QTJava.zip;C:\Program Files\apache-ant-1.8.2\lib\
ant-launcher.jar;C:\Program Files\apache-ant-1.8.2\lib\ant.jar;C:\Program Files\
apache-ant-1.8.2\lib\ant-junit.jar;C:\Program Files\apache-ant-1.8.2\lib\ant-jun
it4.jar'
Any eyes over this to help me get the tests running would be greatly appreciated.
Thanks
James
Update
I observe that if I edit tests.classpath to remove the following 2 lines then the error I see switches to a ClassNotFound which makes sense as it has no references to the .jar files that contain the classes.
<fileset dir="${testsbuild}/jar" includes="**/*.jar"/>
<fileset dir="${frameworkbuild}/jar" includes="**/*.jar"/>
With the above 2 lines present in the tests.classpath is happily finds them but with the wrong name and it's that error I don't know how to fix.
Problem caused by the following jar command:
<jar destfile="${testsbuild}/jar/tests.jar" basedir="${testsbuild}" />
The testsbuild property should be set to
<property name="testsbuild" location="C:\<path on my machine>\workspace\ESS\build"/>
Explanation
The file Credentials.class was compiled as the class esstestapp.tests.Credentials. It needs to be found on the classpath as the file:
esstestapp/tests/Credentials.class

Unexpected attribute "BuildInParallel" on element <msbuild>

I am trying to use the BuildInParallel option on MsBuild.
I have an NAnt & NAntContrib script e.g.
<project xmlns="http://nant.sf.net/release/0.90/nant.xsd" name="Css Manager Solution Build" ToolsVersion="3.5" default="deploy">
<target name="clean" description="Delete all previously compiled binaries.">
<delete>
<fileset>
<include name="**/bin/**" />
<include name="**/obj/**" />
<include name="**/*.suo" />
<include name="**/*.user" />
</fileset>
</delete>
</target>
<target name="deploy" description="Build and deploy all targets.">
<msbuild project="CssManager.sln" BuildInParallel="true">
<property name="Configuration" value="${configuration}"/>
<property name="OutDir" value="${bin.output.dir}"/>
</msbuild>
</target>
</project>
but I get this error message:
Unexpected attribute "BuildInParallel" on element <msbuild>
Please advise?
The MSBuild task of nant-contrib doesn't have a BuildInParallel attribute. You'll have to use the Maxcpucount command line argument.
<target name="deploy" description="Build and deploy all targets.">
<msbuild project="CssManager.sln" BuildInParallel="true">
<property name="Configuration" value="${configuration}"/>
<property name="OutDir" value="${bin.output.dir}"/>
<arg value="/maxcpucount:${environment::get-variable('NUMBER_OF_PROCESSORS')}"/>
</msbuild>
</target>
More info on MaxCpuCount
More info on msbuild task

Do I have a way to check the existence of a directory in Ant (not a file)?

How do I check for the existence of a folder using Ant?
We can check the existence of a file, but can we do the same for a folder as well?
You use the available task with type set to "dir".
For example:
<available file="${dir}" type="dir"/>
The standard way to do conditional processing is with the condition task. In the example below, running doFoo will echo a message if the directory exists, whereas running doBar will echo a message unless the directory exists.
The dir.check target is required by both doFoo and doBar, it sets the dir.exists property to true or false depending on the result of the available task. The doFoo target will only run if that propery is set to true and doBar will only run if it is not set or set to false.
<?xml version="1.0"?>
<project name="test" default="doFoo" basedir=".">
<property name="directory" value="c:\test\directory"/>
<target name="doFoo" depends="dir.check" if="dir.exists">
<echo>${directory} exists</echo>
</target>
<target name="doBar" depends="dir.check" unless="dir.exists">
<echo>${directory} missing"</echo>
</target>
<target name="dir.check">
<condition property="dir.exists">
<available file="${directory}" type="dir"/>
</condition>
</target>
</project>
Antelope provides additional tasks, including an If task that can make the processing simpler (and to me, more intuitive), you can download the Antelope tasks from the download page.
Here's a small example incorporating the available element into an if test.
<!-- Test if a directory called "my_directory" is present -->
<if>
<available file="my_directory" type="dir" />
<then>
<echo message="Directory exists" />
</then>
<else>
<echo message="Directory does not exist" />
</else>
</if>
Warning: you need ant-contrib.jar in your ANT_HOME\lib directory otherwise you won't have access to the if elements, and your script will fail with this error:
Problem: failed to create task or type if
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
Here's my solution, which doesn't require setting properties and using targets with 'if' or 'unless':
Macro:
<macrodef name="assertDirAvailable">
<attribute name="dir" />
<sequential>
<fail message="The directory '#{dir}' was expected to be available but is not">
<condition>
<not>
<available file="#{dir}" type="dir" />
</not>
</condition>
</fail>
</sequential>
</macrodef>
Usage:
<assertDirAvailable dir="${dirToCheck}" />
My solution using ANT 1.8 version, older versions may not work due if/unless not supporting ${evalTrueOrFalse} syntax.
<?xml version="1.0" encoding="UTF-8"?>
<project name="DoMagic" default="build" basedir=".">
<property environment="env" />
<property name="name" value="Do the ANT Magic" />
<property name="somedir" value="./must_exist_folder"/>
<tstamp><format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss" /></tstamp>
<target name="doMagic" if="${dir.exists}">
<echo message="Do the magic stuff" />
</target>
<target name="doUsage" unless="${dir.exists}">
<echo message="Do usage and help" />
</target>
<target name="build">
<echo message="Do the magic" />
<condition property="dir.exists" else="false"><available file="${somedir}" type="dir" /></condition>
<echo message="Folder found: ${dir.exists}" />
<antcall target="doCustomize"></antcall>
<antcall target="doUsage"></antcall>
</target>
</project>
ANT 1.6 or early ANT 1.7 does not work, upgrade to ANT 1.8 release.
Target attributes if and unless evaluates ${var} syntax to true/false
Condition attribute else value is set to property if available condition was false, without it variable is not set. NotSet value is not same as an explicit false value.
call any target but if/unless attribute defines whether its actually run
http://ant.apache.org/manual/properties.html#if+unless
[If/Unless] In Ant 1.7.1 and earlier, these attributes could only be property names. As of Ant 1.8.0, you may instead use property expansion. Compared to the older style, this gives you additional flexibility.
Here is another approach, allows to call just one task without using ant-contrib.jar.
<target name="my-task" depends="dir-check">
<antcall target="my-task-install"/>
<antcall target="my-task-update"/>
</target>
<target name="my-task-install" unless="dir.exists" >
{some task}
</target>
<target name="my-task-update" if="dir.exists" >
{another task}
</target>
<target name="dir-check">
<condition property="dir.exists">
<available file="my-dir" type="dir" />
</condition>
</target>
Here is another example involving for loop. Fail if a directory does not exist.
<for list="dir1/, dir2/, dir3/" param="local.dir" >
<sequential>
<fail message="Directory #{local.dir} does not exist">
<condition>
<not>
<available file="#{local.dir}" type="dir" />
</not>
</condition>
</fail>
</sequential>
</for>