sonar ant task or hudson intergration for coverage report - ant

Good morning, i've got a question about cobertura report integration in sonar.
I've tried two way: from hudson and from ant task.
if i used hudson to configure a sonar integration, the cobertura report is well integrated to my sonar dashboard but i've got some warning like
Java bytecode scan...
[WARN] Class 'javax/mail/Folder' is not accessible through the ClassLoader.
[WARN] Class 'javax/mail/Folder' is not accessible through the ClassLoader.
[WARN] Class 'javax/mail/Folder' is not accessible through the ClassLoader.
[WARN] Class 'javax/mail/Folder' is not accessible through the ClassLoader.
[WARN] Class 'com/sun/mail/imap/IMAPFolder' is not accessible through the ClassLoader.
But, my cobertura report is well integrated:
[INFO] Sensor CoberturaSensor...
[INFO] parsing C:\hudson\jobs\Client Mail\workspace\cobertura\reports\coverage.xml
[INFO] Sensor CoberturaSensor done: 1218 ms
So, i decide to use ant task so i can manage my librairies, this is my ant task:
<target name="sonar" depends="cover-report">
<!-- The workDir directory is used by Sonar to store temporary files -->
<sonar:sonar workDir="./" key="fr.simscorps:client.mail" version="0.1-SNAPSHOT" xmlns:sonar="antlib:org.sonar.ant">
<property key="sonar.host.url" value="http://172.30.3.55:1234/sonar/" />
<!-- source directories (required) -->
<sources>
<path location="./src" />
</sources>
<!-- list of properties (optional) -->
<property key="sonar.dynamicAnalysis" value="reuseReports" />
<property key="sonar.projectName" value="client mail" />
<property key="sonar.java.source" value="1.5" />
<property key="sonar.projectVersion" value="0.1-SNAPSHOT" />
<property key="sonar.phase" value="generate-sources"/>
<property key="sonar.cobertura.reportsPath" value="${reports.xml.dir}/coverage.xml"/>
<property key="sonar.surefire.reportsPath" value="${reports.xml.dir}/TESTS-test.TestSuiteClientMail.xml" />
<!-- test source directories (optional) -->
<tests>
<path location="./test" />
</tests>
<!-- binaries directories, which contain for example the compiled Java bytecode (optional) -->
<binaries>
<path location="./bin" />
</binaries>
<!-- path to libraries (optional). These libraries are for example used by the Java Findbugs plugin -->
<libraries>
<path location="./lib/activation.jar" />
<path location="./lib/deltasync.jar" />
<path location="./lib/jaybird_full_2_1_6.jar" />
<path location="./lib/junit.jar" />
<path location="./lib/mail.jar" />
<path location="./lib/libdeltasync/apache-mime4j-0.5.jar"/>
<path location="./lib/libdeltasync/commons-codec-1.5.jar"/>
<path location="./lib/libdeltasync/commons-codec-1.5-javadoc.jar"/>
<path location="./lib/libdeltasync/commons-codec-1.5-sources.jar"/>
<path location="./lib/libdeltasync/commons-logging-1.1.1.jar"/>
<path location="./lib/libdeltasync/httpclient-4.1.1.jar"/>
<path location="./lib/libdeltasync/httpclient-cache-4.1.1.jar"/>
<path location="./lib/libdeltasync/httpcore-4.1.jar"/>
<path location="./lib/libdeltasync/httpmime-4.1.1.jar"/>
<path location="./lib/libdeltasync/logback-classic-0.9.29.jar"/>
<path location="./lib/libdeltasync/logback-classic-0.9.29-sources.jar"/>
<path location="./lib/libdeltasync/logback-core-0.9.29.jar"/>
<path location="./lib/libdeltasync/logback-core-0.9.29-sources.jar"/>
<path location="./lib/libdeltasync/slf4j-api-1.6.1.jar"/>
<path location="./lib/libdeltasync/slf4j-api-1.6.1-sources.jar"/>
</libraries>
</sonar:sonar>
</target>
My report is well generated (about 700ko) and in hudson's log i see the cobertura sensor, but it's like he didn't do anything:
[sonar:sonar] [INFO] Sensor CoberturaSensor...
[sonar:sonar] [INFO] Sensor CoberturaSensor done: 0 ms
Here, my other ant task dependency:
<property name="junit.output.dir" value="junit"/>
<property name="instrumented.dir" value="cobertura/instrumented"/>
<property name="classes.dir" value="bin"/>
<property name="jars.dir" value="lib"/>
<property name="test.dir" value="test"/>
<property name="cobertura.dir" value="${jars.dir}/cobertura"/>
<property name="reports.xml.dir" value="cobertura/reports"/>
<property name="cob.ser.file" value="cobertura.ser" />
<!-- Define the Sonar task if this hasn't been done in a common script -->
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="C:\hudson\tools\my_ant\lib" />
</taskdef>
<path id="cobertura.classpath">
<fileset dir="${cobertura.dir}">
<include name="cobertura.jar" />
<include name="lib/**/*.jar" />
</fileset>
<fileset dir="${jars.dir}">
<include name="**/*.jar"/>
</fileset>
</path>
<taskdef classpathref="cobertura.classpath" resource="tasks.properties" />
<target name="clean" >
<delete dir="${instrumented.dir}" />
<delete dir="${reports.xml.dir}" />
<delete file="${cob.ser.file}" />
</target>
<target name="prepare" depends="clean">
<mkdir dir="${instrumented.dir}" />
<mkdir dir="${reports.xml.dir}" />
</target>
<target name="instrument" depends="prepare">
<cobertura-instrument todir="${instrumented.dir}">
<ignore regex="org.apache.log4j.*" />
<fileset dir="${classes.dir}">
<include name="**/*.class" />
<exclude name="**/*Test.class" />
</fileset>
</cobertura-instrument>
</target>
<target name="testsuite" depends="instrument">
<junit fork="yes" dir="${test.dir}" failureProperty="test.failed" printsummary="withOutAndErr">
<!--
Specify the name of the coverage data file to use.
The value specified below is the default.
-->
<sysproperty key="net.sourceforge.cobertura.datafile"
file="${basedir}/cobertura.ser" />
<!--
Note the classpath order: instrumented classes are before the
original (uninstrumented) classes. This is important.
-->
<classpath location="${instrumented.dir}" />
<classpath location="${classes.dir}" />
<!--
The instrumented classes reference classes used by the
Cobertura runtime, so Cobertura and its dependencies
must be on your classpath.
-->
<classpath refid="cobertura.classpath" />
<formatter type="xml" />
<test name="test.TestSuiteClientMail" todir="${reports.xml.dir}" />
<!--<batchtest todir="${reports.xml.dir}" unless="testcase">
<fileset dir="${test.dir}">
<include name="**/*Test*.java" />
</fileset>
</batchtest>-->
</junit>
</target>
<target name="cover-report" depends="testsuite">
<cobertura-report format="xml" destdir="${reports.xml.dir}" srcdir="${test.dir}" />
</target>
Does anyone have an idea?

[ANSWER]
Ok, my fault, the problem is in syntax i used: it's
<property key="sonar.cobertura.reportPath" value="${reports.xml.dir}/coverage.xml"/>
and not
<property key="sonar.cobertura.reportsPath" value="${reports.xml.dir}/coverage.xml"/> as we can see here sorry

As for the Class Loader errors you are getting, I got rid of mine by making sure the paths in my binaries & libraries property are made from the root of my project.
<property name="sonar.libraries" value="./Module/resources/web/WEB-INF/lib" />
rather than
<property name="sonar.libraries" value="resources/web/WEB-INF/lib" />
Which gives the class loader error. Hope this helps someone. Took my a few days to figure it out!

Related

Build fails on Jenkins but works in Eclipse

I got a Spring project built using Ant that fails when executed on Jenkins, but not when I run the exact same Ant target from Eclipse (Ant view).
Jenkins version is 2.19.4
Jenkins Console Output excerpt
Started by user Morin, Charles
Building in workspace D:\APPS\jenkins-2.19.4\workspace\MY-PROJECT
Using locally configured password for connection to :pserver:MY-SERVICE-ACCOUNT#MY-CVS-SERVER:D:/DATA/REPOSITORIES/MY-CVS-REPO
cvs update -d -P -r HEAD -D 26 Jan 2017 10:09:39 -0500 MY-PROJECT
Using locally configured password for connection to :pserver:MY-SERVICE-ACCOUNT#MY-CVS-SERVER:D:/DATA/REPOSITORIES/MY-CVS-REPO
cvs rlog -S -d25 Jan 2017 00:08:54 -0500<26 Jan 2017 10:09:39 -0500 MY-PROJECT
[MY-PROJECT] $ cmd.exe /C "D:\APPS\jenkins-2.19.4\ant\bin\ant.bat -file build.xml compile && exit %%ERRORLEVEL%%"
Buildfile: build.xml
[echo] ========================================================================
[echo] *** Starting MY-PROJECT build
[echo] ========================================================================
print-version:
[echo] Java/JVM version: 1.6
[echo] Java/JVM detailed version: 1.7.0_25
create-directories:
compile:
[javac] Compiling 41 source files to D:\APPS\jenkins-2.19.4\workspace\MY-PROJECT\target\classes
[javac] Note: Hibernate JPA 2 Static-Metamodel Generator 5.1.0.Final
[javac] D:\APPS\jenkins-2.19.4\workspace\MY-PROJECT\src\path\to\my\class\MyClass.java:28: error: cannot find symbol
[javac] import path.to.my.class.MyClass_;
[javac] ^
[javac] symbol: class MyClass_
[javac] location: package path.to.my.class
The problem is that my JPA metamodels are not being found during the compilation. However, we can see the following line that confirms the generation of JPA Metamodels:
Note: Hibernate JPA 2 Static-Metamodel Generator 5.1.0.Final
The only difference I can see is in the print-version Ant target. When executed locally, I got this:
print-version:
[echo] Java/JVM version: 1.7
[echo] Java/JVM detailed version: 1.7.0_25
I also have several other projects with the exact same setup, and I don't have that issue.
build.xml
<?xml version="1.0" encoding="utf-8"?>
<project name="MY-PROJECT" default="create-war-and-deploy-to-jboss" basedir="." xmlns:sonar="antlib:org.sonar.ant" xmlns:jacoco="antlib:org.jacoco.ant">
<property name="verbose" value="false" />
<property name="javaVersion" value="1.7" />
<!-- Provides access to OS environment variables (eg: env.USERNAME) -->
<property environment="env" />
<!-- Application's directories -->
<property name="dir.build" value="${basedir}/build" />
<property name="dir.src" value="${basedir}/src" />
<property name="dir.dist" value="${basedir}/dist" />
<property name="dir.lib" location="${basedir}/lib" />
<property name="dir.target" value="${basedir}/target" />
<property name="dir.build.classes" value="${dir.target}/classes" />
<property name="dir.test" value="${basedir}/test" />
<property name="dir.web" value="${basedir}/WebContent" />
<property name="dir.webinf.lib" value="${dir.web}/WEB-INF/lib" />
<!-- Loading the application.properties file -->
<property file="${dir.src}/resources/application.properties" prefix="app." />
<!-- Loading the build.properties file -->
<property file="build.properties" prefix="buildProp." />
<tstamp>
<format property="build.time" pattern="yyyy-MM-dd HH:mm" />
</tstamp>
<echo message="========================================================================" />
<echo message="*** Starting ${app.name} build " />
<echo message="========================================================================" />
<!-- JBoss directories -->
<property name="dir.jboss" value="${buildProp.jboss.home}" />
<property name="dir.jboss.domain" value="${dir.jboss}/${buildProp.jboss.domain}" />
<property name="dir.jboss.libs" value="${dir.jboss}/${buildProp.jboss.libs}" />
<property name="dir.jboss.deploy" value="${dir.jboss.domain}/${buildProp.jboss.deploy}" />
<!-- Path for unit tests -->
<path id="run.classpath.tests">
<pathelement path="${dir.build.classes}" />
<pathelement path="${dir.test}/build/classes" />
<path refid="compile.classpath.tests" />
</path>
<!-- Provided dependencies from the container -->
<path id="compile.classpath.server">
<fileset dir="${dir.lib}">
<include name="**/*.jar" />
</fileset>
</path>
<!-- Dependencies specific to this application -->
<path id="compile.classpath.web">
<fileset dir="${dir.web}/WEB-INF/lib">
<include name="**/*.jar" />
</fileset>
</path>
<path id="compile.classpath.target">
<fileset dir="${dir.lib}">
<include name="hibernate-jpamodelgen-5.1.0.Final.jar" />
</fileset>
<fileset dir="${dir.target}">
<include name="**/*.java" />
</fileset>
</path>
<!-- Dependencies specifically for unit testing purposes -->
<path id="compile.classpath.tests">
<fileset dir="${dir.test}/lib/">
<include name="junit-4.6.jar" />
<include name="mockito-all-1.9.5.jar" />
</fileset>
<fileset dir="${dir.webinf.lib}" />
<pathelement location="${dir.build.classes}" />
<path refid="compile.classpath.server" />
</path>
<!-- PRINT-VERSION -->
<target name="print-version">
<echo>Java/JVM version: ${ant.java.version}</echo>
<echo>Java/JVM detailed version: ${java.version}</echo>
</target>
<!-- BUILD-DISTRIBUTION -->
<target
name="build-distribution"
description="Generate Deployment unit and stage to appserver directory."
depends="
clean,
run-unit-tests,
create-war,
run-sonarqube-analysis">
<manifest file="${dir.web}/META-INF/MANIFEST.MF">
<attribute name="Manifest-Version" value="1.0" />
<attribute name="Ant-Version" value="1.7.0" />
<attribute name="Created-By" value="${env.USERNAME}" />
<attribute name="Implementation-Title" value="${app.name}" />
<attribute name="Implementation-Version" value="${app.version}" />
<attribute name="Implementation-Vendor" value="Canada Revenue Agency" />
<attribute name="Implementation-Date" value="${build.time}" />
<attribute name="Built-By" value="${env.USERNAME}" />
<attribute name="Dependencies" value="org.hibernate" />
</manifest>
<propertyfile file="${dir.src}/resources/application.properties">
<entry key="lastUpdate" value="${build.time}" />
</propertyfile>
</target>
<!-- CLEAN -->
<target name="clean"
description="Cleans up build-related temporary directories."
depends="delete-directories" />
<!-- CREATE-DIRECTORIES -->
<target name="create-directories" >
<mkdir dir="${dir.build}" />
<mkdir dir="${dir.dist}" />
<mkdir dir="${dir.build.classes}" />
<mkdir dir="${dir.web}/WEB-INF/classes" />
<mkdir dir="${dir.test}/build" />
<mkdir dir="${dir.test}/build/classes" />
<mkdir dir="${dir.test}/reports" />
</target>
<!-- DELETES-DIRECTORIES -->
<target name="delete-directories">
<delete dir=".sonar" />
<!-- /build -->
<delete includeEmptyDirs="true" failonerror="false">
<fileset dir="${dir.build}" includes="**/*" />
<exclude name=".cvsignore" />
</delete>
<!-- /dist -->
<delete includeEmptyDirs="true" failonerror="false">
<fileset dir="${dir.dist}" includes="**/*" />
<exclude name=".cvsignore" />
</delete>
<!-- /WEB-INF/classes -->
<delete includeEmptyDirs="true" failonerror="false">
<fileset dir="${dir.web}/WEB-INF/classes">
<include name="**/*" />
</fileset>
<fileset dir="${dir.web}/WEB-INF/src">
<include name="**/*" />
</fileset>
</delete>
<!-- /target -->
<delete includeEmptyDirs="true" failonerror="false">
<fileset dir="${dir.target}" includes="**/*" />
<exclude name=".cvsignore" />
</delete>
<!-- /test/build -->
<delete dir="${dir.test}/build" />
<!-- /test/reports -->
<delete includeEmptyDirs="true" failonerror="false">
<fileset dir="${dir.test}/reports" />
</delete>
<!-- Existing WAR file and JBoss marker files -->
<delete dir="${dir.jboss.deploy}/${app.name}.war" />
<delete file="${dir.jboss.deploy}/${app.name}.war" />
<delete file="${dir.jboss.deploy}/${app.name}.war.deployed" />
</target>
<!-- COMPILE -->
<target name="compile" depends="print-version,create-directories">
<javac destdir="${dir.build.classes}" verbose="${verbose}" debug="on" fork="true" failonerror="true" source="${javaVersion}" target="${javaVersion}" includeantruntime="false">
<classpath>
<path refid="compile.classpath.server" />
<path refid="compile.classpath.target" />
<path refid="compile.classpath.web" />
</classpath>
<src path="${dir.src}" />
<compilerarg value="-Xlint:deprecation" />
<compilerarg value="-Xlint:unchecked" />
</javac>
<!--copy property files to classes directory -->
<copy todir="${dir.build.classes}">
<fileset dir="${dir.src}">
<include name="**/*.properties" />
<include name="META-INF/**/*" />
</fileset>
</copy>
</target>
<!-- COMPILE-TESTS -->
<target name="compile-tests" depends="create-directories,compile">
<javac srcdir="${dir.test}/src" destdir="${dir.test}/build/classes" verbose="${verbose}" debug="on" fork="true" source="${javaVersion}" target="${javaVersion}" includeantruntime="false">
<classpath refid="compile.classpath.tests" />
<compilerarg value="-Xlint:deprecation" />
<compilerarg value="-Xlint:unchecked" />
</javac>
</target>
<!-- RUN-UNIT-TESTS -->
<target name="run-unit-tests" depends="compile-tests" description="Runs all unit test classes under the test package.">
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath path="test/lib/org.jacoco.ant-0.7.2.201409121644.jar"/>
<classpath path="test/lib/org.jacoco.agent_0.7.2.201409121644.jar"/>
<classpath path="test/lib/org.jacoco.core_0.7.2.201409121644.jar"/>
<classpath path="test/lib/org.jacoco.report_0.7.2.201409121644.jar"/>
</taskdef>
<jacoco:coverage destfile="${dir.target}/jacoco.exec">
<junit fork="true" printsummary="yes" haltonfailure="yes" showoutput="no">
<classpath refid="run.classpath.tests" />
<formatter type="xml" />
<batchtest fork="yes" todir="${dir.test}/reports">
<fileset dir="${dir.test}/src">
<include name="**/*Test.java" />
</fileset>
</batchtest>
</junit>
</jacoco:coverage>
</target>
<!-- CREATE-WAR -->
<target name="create-war" description="Creates a WAR file including all compiled classes and resources." depends="compile">
<war destfile="dist/${app.name}.war" webxml="WebContent/WEB-INF/web.xml">
<fileset dir="WebContent" />
<classes dir="${dir.build.classes}" />
</war>
</target>
<!-- CREATE-WAR-AND-DEPLOY -->
<target name="create-war-and-deploy-to-jboss" description="Creates a WAR file including all compiled classes and resources, and deploys the app to jboss" depends="create-war,deploy-to-jboss" />
<!-- RUN-SONARQUBE -->
<target name="run-sonarqube-analysis" description="Runs the SonarQube analysis and updates statistics on the server." depends="create-war">
<property file="sonar.properties" prefix="sonar." />
<!-- SonarQube properties -->
<property name="sonar.exclusions" value="${sonar.exclusions}" />
<property name="sonar.host.url" value="${sonar.host.url}" />
<property name="sonar.jacoco.reportPath" value="${sonar.jacoco.reportPath}" />
<property name="sonar.java.binaries" value="${sonar.java.binaries}" />
<property name="sonar.java.coveragePlugin" value="${sonar.java.coveragePlugin}" />
<property name="sonar.java.libraries" value="${sonar.java.libraries}" />
<property name="sonar.java.source" value="${javaVersion}" />
<property name="sonar.junit.reportsPath" value="${sonar.junit.reportsPath}" />
<property name="sonar.projectKey" value="${sonar.projectKey}" />
<property name="sonar.projectVersion" value="${app.version}" />
<property name="sonar.projectName" value="${sonar.projectName}" />
<property name="sonar.sources" value="${sonar.sources}" />
<property name="sonar.sourceEncoding" value="${sonar.sources.sourceEncoding}" />
<property name="sonar.tests" value="${sonar.tests}" />
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="${dir.lib}/sonarqube-ant-task-2.4.1.jar" />
</taskdef>
<sonar:sonar/>
</target>
<!-- DEPLOY-TO-JBOSS -->
<target name="deploy-to-jboss" depends="create-war">
<!--delete war file if it already exists and was copied over, we need a directory now-->
<delete file="${dir.jboss.deploy}/${app.name}.war" />
<delete file="${dir.jboss.deploy}/${app.name}.war.deployed" />
<!-- copy all of the files except one that will trigger the deployment -->
<copy todir="${dir.jboss.deploy}">
<fileset file="${dir.dist}/${app.name}.war" />
</copy>
</target>
</project>
Thank you
From Manage Jenkins-->Configure System--JDK section, add both JDKs to JDK Installation. From your Job Configuration, in the JDK section, explicitly choose the 1.7 JDK.

Ant task "javac" was successful but doesn't produce any output

I'm trying to compile a project using ant. I guess the "javac" task was successful as i can see the following message on the console:
[javac] Compiling 151 source file to /var/lib/jenkins/jobs/project1/workspace/build
Here is the complete output of the command "ant compile"
clean:
[delete] Deleting directory /var/lib/jenkins/jobs/project1/workspace/build
[delete] Deleting directory /var/lib/jenkins/jobs/project1/workspace/docs
[delete] Deleting directory /var/lib/jenkins/jobs/project1/workspace/dist
makedir:
[mkdir] Created dir: /var/lib/jenkins/jobs/project1/workspace/build
[mkdir] Created dir: /var/lib/jenkins/jobs/project1/workspace/docs
[mkdir] Created dir: /var/lib/jenkins/jobs/project1/workspace/dist
compile:
[javac] /var/lib/jenkins/jobs/project1/workspace/build.xml:43: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 151 source file to /var/lib/jenkins/jobs/project1/workspace/build
BUILD SUCCESSFUL
Total time: 1 second
The problem is there is nothing produced in the folder "build" !
I'm using the same build.xml template file in other projects and it works very well, but here i can't understand why i can't find the compiled sources in the build folder.
Please help.
Here is my build.xml file:
<?xml version="1.0"?>
<project name="project1" default="main" basedir=".">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="src.dir" location="src" />
<property name="lib.dir" location="WebContent/WEB-INF/lib" />
<property name="server.common.lib.dir" location="/home/ghali/jboss-5.1.0.GA/common/lib" />
<property name="server.lib.dir" location="/home/ghali/jboss-5.1.0.GA/lib" />
<property name="build.dir" location="build" />
<property name="dist.dir" location="dist" />
<property name="docs.dir" location="docs" />
<!--
Create a classpath container which can be later used in the ant task
-->
<path id="build.classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${server.common.lib.dir}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${server.lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<!-- Deletes the existing build, docs and dist directory-->
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${docs.dir}" />
<delete dir="${dist.dir}" />
</target>
<!-- Creates the build, docs and dist directory-->
<target name="makedir">
<mkdir dir="${build.dir}" />
<mkdir dir="${docs.dir}" />
<mkdir dir="${dist.dir}" />
</target>
<!-- Compiles the java code -->
<target name="compile" depends="clean, makedir">
<javac destdir="${build.dir}" classpathref="build.classpath" debug="true">
<src path="${src.dir}" />
</javac>
</target>
<!-- Creates Javadoc -->
<target name="docs" depends="compile">
<javadoc sourcepath="${src.dir}" destdir="${docs.dir}">
<!-- Define which files / directory should get included, we include all -->
<fileset dir="${src.dir}">
<include name="**" />
</fileset>
</javadoc>
</target>
<!--Creates the deployable jar file -->
<target name="package" depends="compile">
<war destfile="${dist.dir}/project1.war" webxml="WebContent/WEB-INF/web.xml">
<fileset dir="WebContent" />
<lib dir="WebContent/WEB-INF/lib" />
<classes dir="${build.dir}" />
</war>
</target>
<target name="main" depends="compile, package, docs">
<description>Main target</description>
</target>
</project>

TestNg/Selenium call by ant always return Cannot find class in the classpath

I'm pretty new with this setup. And having issue to call my project with TestNG by ant.
I can run the testng.xml without any problem in Eclipse but I alway receive Cannot find class in classpath by ant.
Build.xml
<project basedir="." default="runTest" name="Ant file for TestNG">
<property name="src" location="src" />
<property name="bin" location="bin" />
<property name="telus" location="C:\ESP_Testware\ESP_Projects\Selenium\telus-pharma-integration-tests\src\test\resources\suite\local" />
<property name="libs" location="lib" />
<path id="class.path">
<pathelement location="${libs}/testng-6.4.jar" />
<pathelement location="${libs}/selenium-java-client-driver.jar" />
<pathelement location="${libs}/selenium-server-standalone-2.39.0.jar" />
<pathelement location="${bin}"/>
<pathelement location="${telus}"/>
</path>
<taskdef name="testng" classname="org.testng.TestNGAntTask">
<classpath>
<pathelement location="${libs}/testng-6.4.jar"/>
</classpath>
</taskdef>
<target name="runTest">
<echo message="mkdir"/>
<mkdir dir="testng_output"/><!-- Create the output directory. -->
<echo message= "TestNg Start"/>
<testng outputdir="testng_output" classpathref="class.path">
<xmlfileset dir="${telus}" includes="testng.xml"/>
<!-- <xmlfileset dir="." includes="TestNG2.xml"/> -->
</testng>
</target>
</project>
Testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Bolt harness QA" verbose="1">
<parameter name="test.env" value="qa" />
<parameter name="selenium.url" value="https://www.google.com" />
<!-- Valid values for browser: FF, IE, Chrome -->
<parameter name="selenium.browser" value="Chrome" />
<listeners>
<listener class-name="com.gdo.test.integration.listener.SoftAssertTestListener" />
</listeners>
<test name="Test_MS_Website" preserve-order="true">
<classes>
<class name="com.gdo.telus.SC006">
<methods>
<include name="Web_InvalidPassword" />
<exclude name="Web_LockedAccount" />
</methods>
</class>
</classes>
</test>
</suite>
My Class are at this path :
C:\ESP_Testware\ESP_Projects\Selenium\telus-pharma-integration-tests\src\test\java\com\gdo\telus
Thanks for your help.
Try my build.xml file, I did add the ReportNG plugin into this build.xml file to generate better looking reports instead of the default TestNG reports. You can just download the jar file for ReportNG and place it into your lib folder and it should still work fine:
<project name="Some Bullshit Goes Here" default="clean" basedir=".">
<!-- Initilization properties -->
<!-- <property name="lib.dir" value="${basedir}/lib"/> -->
<!-- using the ${basedir} allows you to use relative paths. It will use the working directory and add folders that you specify -->
<property name="build.dir" value="${basedir}/build"/>
<property name="lib.dir" value="hardcoded value can go here"/>
<property name="src.dir" value="${basedir}/src"/>
<property name="bin.dir" value="${basedir}/bin"/>
<property name="output.dir" value="${basedir}/output"/>
<!-- I chose to hardcode the location where my jar library files will be, it will be used for compilation. Again you can set relative path if you wish.-->
<path id="assloadoflibs">
<fileset dir="/automated/tests/library">
<include name="*.jar"/>
</fileset>
<pathelement path="${basedir}/bin"/>
</path>
<!-- setting libraries -->
<target name="setClassPath">
<path id="classpath_jars">
<pathelement path="${basedir}/"/>
<fileset dir="/automated/tests/library" includes="*.jar"/>
</path>
<!-- Convert jar collection from a given reference into one list, storing the result into a given property, separated by colon -->
<pathconvert pathsep=":" property="test.classpath" refid="classpath_jars"/>
</target>
<target name="loadTestNG" depends="setClassPath">
<!-- Creating task definition for TestNG task -->
<taskdef resource="testngtasks" classpath="${test.classpath}"/>
</target>
<target name="init">
<!-- Creating build directory structure used by compile -->
<mkdir dir="${build.dir}"/>
</target>
<target name="clean">
<echo message="deleting existing build directory"/>
<delete dir="${build.dir}"/>
</target>
<!-- In compile target dependency is given over clean target followed by init,
this order makes sure that build directory gets created before compile takes place
This is how a clean complile is achieved.
-->
<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="run" depends="compile">
<!-- testng classpath has been provided reference of jar files and compiled classes
this will generate report NG report.
-->
<testng classpath="${test.classpath}:${build.dir}" outputdir="${basedir}/output" haltonfailure="false" useDefaultListeners="true" listeners="org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter" classpathref="reportnglibs">
<xmlfileset dir="${basedir}" includes="testng.xml"/>
<!-- This value here will show the title of the report -->
<sysproperty key="org.uncommons.reportng.title" value="Example Test Report"/>
</testng>
</target>
</project>
Here is my TestNG.xml file:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Example Test Suite">
<test name ="Example TestCase Name">
<classes>
<class name="packageName.JavaFilename"></class>
</classes>
</test>
</suite>
I've found my answer on this site. I need to use maven to call my solution.
http://rationaleemotions.wordpress.com/2012/05/14/continuous-integration-with-selenium/
but thanx anyway for your help

Javadoc errors when building Ant project

I am trying to write a build.xml file for my project. When I run build.xml as an Ant project, I get the following error:
D:\workspace\LogAlerter\src\com\j32bit\alerter\launcher\LogAlerter.java:9:
error: package org.apache.log4j does not exist
[javadoc] import org.apache.log4j.Logger;
I have imported log4j in LogAlerter.Java. Here is my build.xml file:
<?xml version="1.0"?>
<project name="LogAlerter" default="main" basedir=".">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="src.dir" location="src" />
<property name="build.dir" location="build" />
<property name="dist.dir" location="dist" />
<property name="docs.dir" location="docs" />
<property name="libs.dir" location="lib" />
<!--
Create a classpath container which can be later used in the ant task
-->
<path id="build.classpath">
<fileset dir="${libs.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<!-- Deletes the existing build, docs and dist directory-->
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${docs.dir}" />
<delete dir="${dist.dir}" />
</target>
<!-- Creates the build, docs and dist directory-->
<target name="makedir">
<mkdir dir="${build.dir}" />
<mkdir dir="${docs.dir}" />
<mkdir dir="${dist.dir}" />
</target>
<!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir" >
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath" includeantruntime="false">
</javac>
</target>
<!-- Creates Javadoc -->
<target name="docs" depends="compile">
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
<!-- Define which files / directory should get included, we include all -->
<packageset dir="${src.dir}" defaultexcludes="yes">
<include name="**" />
</packageset>
</javadoc>
</target>
<!--Creates the deployable jar file -->
<target name="jar" depends="compile">
<jar destfile="${dist.dir}\LogAlerter.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="LogAlerter.Main" />
</manifest>
</jar>
</target>
<target name="main" depends="compile, jar, docs">
<description>Main target</description>
</target>
</project>
Try adding a classpath ref to your javadoc task:
<javadoc packagenames="src"
sourcepath="${src.dir}"
destdir="${docs.dir}"
classpathref="build.classpath">
What the warning is telling you is that you've not provided the full classpath to the javadoc task. Try adding a similar classpath ref to that in your compile task and see where that leads.
Importing is fine but make sure it is available at run time for the JavaDoc tool. log4j.jar should be present in your build.classpath.
Make use of the classpathref inside the docs target like so:
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}" classpathref="build.classpath">

How do I generate Emma code coverage reports using Ant?

How do I setup an Ant task to generate Emma code coverage reports?
To answer questions about where the source and instrumented directories are (these can be switched to whatever your standard directory structure is):
<property file="build.properties" />
<property name="source" location="src/main/java" />
<property name="test.source" location="src/test/java" />
<property name="target.dir" location="target" />
<property name="target" location="${target.dir}/classes" />
<property name="test.target" location="${target.dir}/test-classes" />
<property name="instr.target" location="${target.dir}/instr-classes" />
Classpaths:
<path id="compile.classpath">
<fileset dir="lib/main">
<include name="*.jar" />
</fileset>
</path>
<path id="test.compile.classpath">
<path refid="compile.classpath" />
<pathelement location="lib/test/junit-4.6.jar" />
<pathelement location="${target}" />
</path>
<path id="junit.classpath">
<path refid="test.compile.classpath" />
<pathelement location="${test.target}" />
</path>
First you need to setup where Ant can find the Emma libraries:
<path id="emma.lib" >
<pathelement location="${emma.dir}/emma.jar" />
<pathelement location="${emma.dir}/emma_ant.jar" />
</path>
Then import the task:
<taskdef resource="emma_ant.properties" classpathref="emma.lib" />
Then instrument the code:
<target name="coverage.instrumentation">
<mkdir dir="${instr.target}"/>
<mkdir dir="${coverage}"/>
<emma>
<instr instrpath="${target}" destdir="${instr.target}" metadatafile="${coverage}/metadata.emma" mode="copy">
<filter excludes="*Test*"/>
</instr>
</emma>
<!-- Update the that will run the instrumented code -->
<path id="test.classpath">
<pathelement location="${instr.target}"/>
<path refid="junit.classpath"/>
<pathelement location="${emma.dir}/emma.jar"/>
</path>
</target>
Then run a target with the proper VM arguments like:
<jvmarg value="-Demma.coverage.out.file=${coverage}/coverage.emma" />
<jvmarg value="-Demma.coverage.out.merge=true" />
Finally generate your report:
<target name="coverage.report" depends="coverage.instrumentation">
<emma>
<report sourcepath="${source}" depth="method">
<fileset dir="${coverage}" >
<include name="*.emma" />
</fileset>
<html outfile="${coverage}/coverage.html" />
</report>
</emma>
</target>
The User Guide has a good example of how to set up your build script so that you not only seperate the instrumented code from the execution, but it's also all contained in the same <target> so that you don't have to run a series of different targets, but instead you can just do something like ant emma tests (if ant tests was how you normally ran your unit tests, for example).
Here's their example:
<target name="emma" description="turns on EMMA instrumentation/reporting" >
<property name="emma.enabled" value="true" />
<!-- EMMA instr class output directory: -->
<property name="out.instr.dir" value="${basedir}/outinstr" />
<mkdir dir="${out.instr.dir}" />
</target>
<target name="run" depends="init, compile" description="runs the examples" >
<emma enabled="${emma.enabled}" >
<instr instrpathref="run.classpath"
destdir="${out.instr.dir}"
metadatafile="${coverage.dir}/metadata.emma"
merge="true"
/>
</emma>
<!-- note from matt b: you could just as easily have a <junit> task here! -->
<java classname="Main" fork="true" >
<classpath>
<pathelement location="${out.instr.dir}" />
<path refid="run.classpath" />
<path refid="emma.lib" />
</classpath>
<jvmarg value="-Demma.coverage.out.file=${coverage.dir}/coverage.emma" />
<jvmarg value="-Demma.coverage.out.merge=true" />
</java>
<emma enabled="${emma.enabled}" >
<report sourcepath="${src.dir}" >
<fileset dir="${coverage.dir}" >
<include name="*.emma" />
</fileset>
<txt outfile="${coverage.dir}/coverage.txt" />
<html outfile="${coverage.dir}/coverage.html" />
</report>
</emma>
</target>
Emma 2.1 introduces another way of obtaining runtime coverage information (.ec file). One can remotely request the data from the given port of the computer where an instrumented application is runnig. So there's no need to stop VM.
To get the file with runtime coverage data you need to insert the following snippet in your Ant script between running of your tests and generating coverage report:
<emma>
<ctl connect="${emma.rt.host}:${emma.rt.port}" >
<command name="coverage.get" args="${emma.ec.file}" />
<command name="coverage.reset" />
</ctl>
</emma>
Other steps are similar to Emma 2.0. They are perfectly described in previous post
More information on Emma 2.1 features: http://sourceforge.net/project/shownotes.php?group_id=108932&release_id=336859

Resources