Publishing a checkstyle report in Jenkins - ant

I have the following problem - I'm running a checkstyle ant task when building with Jenkins and the respective report DOES get produced, but the thing is that when the build finishes I get a message that there has been an error during the parsing because the error report has not been found. I verified that I have set the path to the report that is to be published to the right thing (when I change it to something slightly different I get a message that xxx does not exist but the path from the previous version does exist). Any idea what might be wrong? What format is Jenkins expecting in order to publish the checkstyle report? I'm using the following build.xml
<taskdef
classpath="libs/checkstyle-5.6-all.jar"
resource="checkstyletask.properties" />
<target name="checkstyle" >
<checkstyle
config="checkstyle.xml"
failOnViolation="false" >
<fileset
dir="src"
includes="**/*.java" />
<formatter type="plain" />
<formatter type="xml" />
<formatter
toFile="checkstyle-result.xml"
type="xml" />
</checkstyle>
<style
style="checkstyle-noframes.xsl"
in="checkstyle-result.xml"
out="checkstyle-result.html" />
</target>
the config is the sun-checkstyle config.
Thanks for any help.

Try specifying **/checkstyle-result.xml as the result file location in your Jenkins job configuration; that will look through your entire build workspace for the result file. You can tighten up the file glob once you have things working.
If the above doesn't work, post the error message you're getting from Jenkins and the location of checkstyle-results.xml relative to your job's workspace directory.

From the checkstyle plugin website:
The Checkstyle plug-in scans for checkstyle-result.xml files in the
build workspace and reports the number of warnings found.
The plug-in should be looking for the checkstyle-result.xml XML formatted file. You don't mention in your question how you've configured the plugin, but if you are looking for checkstyle-result.html that may be the issue.

Related

org.sonar.runner.impl.RunnerException: Unable to execute Sonar

I am trying to integrate Sonar task in Ant build in Eclipse. SonarQube server is running successfully at the default port on localhost. On opening "http://localhost:9000/" in browser, SonarQube web interface is successfully opening.
Problem is when I am running Sonar task from Ant build file it is giving error "org.sonar.runner.impl.RunnerException: Unable to execute Sonar". That's it. No stacktrace is logged on console.
The sonar task in ANt build is defined as below:
<target name="sonar" depends="jcompile">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="<PathToAntPlugin>/lib/sonar-ant-task-2.2.jar" />
</taskdef>
<property name="sonar.projectDescription" value="Example application using Ant and Jacoco" />
<property name="sonar.sources" value="${basedir}/src" />
<property name="sonar.host.url" value="http://localhost:9000"/>
<property name="sonar.surefire.reportsPath" value="${reports.junit.xml.dir}" />
<property name="sonar.core.codeCoveragePlugin" value="jacoco" />
<property name="sonar.jacoco.antTargets" value="run-tests" />
<property name="sonar.projectKey" value="<PathToMyProject>" />
<property name="sonar.projectVersion" value="1.0" />
<sonar:sonar xmlns:sonar="antlib:org.sonar.ant"/>
</target>
Other details:
SonarQube Server 4.5,
sonar-ant-task-2.2.jar,
JDK 1.6.0_21,
Eclipse Kepler
Is there any comptability issue between the jdk, eclipse, sonar server or sonar ant jar? Is there any way to find the detail logs in order to debug the issue?
Apart from above issue, I am having other issue. When I am installing SonarCube plugin in eclipse, in Windows ->Preferences SonarCube is not coming. However, in installed software list in Eclipse it is listed. I have tried to run eclipse with -clean option but no success. Please let me know what could be the problem?
When we are running the build file from eclipse it is not giving full stack trace on console in some cases. Try to run the build file from the command prompt.
For example:
Open command prompt window and go to eclipse ant bin directory path(In my system it is C:\eclipse\plugins\org.apache.ant_1.9.6.v201510161327\bin) and from here run your build file using ant command.
C:\eclipse\plugins\org.apache.ant_1.9.6.v201510161327\bin>ant -buildfile [path_of_build_file]\build-test.xml
I know I am late, but this is to help who visits this with same problem
For the missing preferences, the Installation FAQ of Sonar says to do a restart with -clean argument.

How to display the classpath build in an Ant Junit task

An Ant junit task that has worked for months is suddenly failing with a NoClassDefFoundError for classes that used to be found. Is there a way to display the classpath that is built in the junit task?
<target name="basic-junit-test" description="Run a single JUnit test. ">
<junit printsummary="yes" fork="no" haltonfailure="yes">
<classpath>
<pathelement location="target/WEB-INF/lib/log4j-1.2.16.jar"/>
.
. many other pathelements
.
</classpath>
<test name="com.mycompany.command.TestUNLOCKACCOUNTCommand" outfile="${report.dir}/junit_test_results" />
</junit>
</target>
I'm a big fan of declaring my Ant paths at the top of my build and using classpath references in the various tasks.
To pathconvert task can be used to print the classpath contents as a property:
<path id="test.path">
<pathelement location="target/WEB-INF/lib/log4j-1.2.16.jar"/>
.
. many other pathelements
.
</path>
<target name="echo-path" description="Echo test path">
<pathconvert targetos="unix" property="test.path.unix" refid="test.path">
<echo message="Test path: ${test.path.unix}"/>
</target>
<target name="basic-junit-test" depends="echo-path" description="Run a single JUnit test. ">
<junit printsummary="yes" fork="no" haltonfailure="yes">
<classpath>
<path refid="test.path"/>
</classpath>
<test name="com.mycompany.command.TestUNLOCKACCOUNTCommand" outfile="${report.dir}/junit_test_results" />
</junit>
</target>
Update
Just occurred to me: an even simpler solution might be to run Ant in debug mode.
This is not really the answer to my question, Mark O'Connor and Rebse gave excellent answers, instead this is a more thorough explanation of what happened to cause me to ask the question in the first place. I had an ANT Junit task that I had used to develop about 100 controller classes. I used it for the first time in several months and every test failed with a classNotFound exception. The class that was not found was one that I was sure should be on the classpath, it was a locally created jar file that is automatically picked up for the build. I thought that somehow the classpath was at fault so I wanted to display it when a test was run.
After many attempts to figure out what was going on I put a try block around the code that was producing the classNotFound exception and I saw that the local class was not the class that was not being found. This lead me to a search of the lib directory and eventually (after about six hours) I realized that the problem was that I had replaced an older version of slf4j-api with a newer version. There was a dependency on a method that was in the older version but not in the newer.
Mark's answer just helped me, but just a quick note as I needed to close the pathconvert xml element when copying the example in the answer.
<pathconvert targetos="unix" property="test.path.unix" refid="test.path" />

Jenkins Phing Plugin - Resource Temporarily Unavailable

I have just started getting Jenkins setup with Phing as the build tool. Although I have used Jenkins before, I'm new to Phing.
I have a project setup in Jenkins that has a Mercurial Repository setup and a Phing Build step.
The build.xml file simply archives the existing file, deletes them and copies the new files from the repository.
I have run phing from the terminal and everything works as planned. However, when running from within Jenkins, I'm getting the following in the Console Output:
[workspace] $ /usr/local/pear/bin/phing -buildfile /Users/Shared/Jenkins/Home/jobs/Project/workspace/build.xml "-Dwebroot=/Volumes/Websites/Project/ -Dcheckoutroot=/Users/Shared/Jenkins/Home/jobs/Project/workspace -Drevision=5" -logger phing.listener.DefaultLogger
/usr/local/pear/bin/phing: fork: Resource temporarily unavailable
Build step 'Invoke Phing targets' marked build as failure
My first thought was that it was permission related, but I've changed Jenkins to run as the same user that I ran Phing manually as and it still got the same issue.
Does anybody have any thoughts as to what might be causing the problem?
I can't find anything related to this error anywhere that isn't related to Cygwin...
The system is running on OS X 10.7.5 with Jenkins 1.518 and Phing 2.5.1
The build.xml file looks like:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Project" default="dist">
<property name="revision" value="old" />
<property name="webroot" value="/Volumes/Websites/${phing.project.name}" />
<property name="checkoutroot" value="./" />
<target name="clean">
<echo msg="Backing up old site to ${phing.project.name}-${revision}..." />
<tar destfile="${webroot}/../${phing.project.name}-${revision}.tar.gz" basedir="${webroot}" compression="gzip" />
<echo msg="Deleting site from ${webroot}..." />
<delete>
<fileset dir="${webroot}" />
</delete>
</target>
<target name="dist" depends="clean">
<echo msg="Copying files to website at ${webroot}..." />
<copy todir="${webroot}">
<fileset dir="${checkoutroot}/Website">
<exclude name="**/.hg/**" />
</fileset>
</copy>
</target>
</project>
I've managed to resolve this issue by removing all of the path details in the Phing configuration section within Jenkins.
This makes absolutely no sense to me as to why it's working without these details as I definitely only have one Phing install, so it's not as if it was picking up the wrong one or something.
However, by not specifying anything in the Phing section of the Jenkins project config so it picks up the default path, default build target etc. and this is working flawlessly now!

What is the preferred practice for enabling Checkstyle for Jenkins?

Hi guys : I noticed that there is an ant task for checkstyle
http://checkstyle.sourceforge.net/anttask.html
I want checkstyle to run in my Ant build, which is on jenkins.
Unfortunately, the instructions are somewhat cryptic - with references to enabling project dependencies , modules, and other ant-specific configurations. I have a massive build file and I'm not really a build engineer - so I want to keep it simple without adding too much bload to the script.
Jenkins has a nice little button which supports displaying the checkstyle results, however, jenkins requires that you run the checkstyle and configure it yourself when you run a build.
What is the simplest way to modify my build.xml and ivy.xml ( i assume i will need to add checkstyle to ivy to get the jar remotely) to enable a basic checkstyle analysis of all the code base when builds are run ?
The sample of how to do it with help of just Ant:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Build" default="build" basedir=".">
<property file="props.properties"/>
<taskdef resource="checkstyletask.properties" classpath="${checkstyle.jar.path}"/>
<target name="build" depends="checkstyle">
<echo>Starting build</echo>
<echo>Build finished</echo>
</target>
<target name="checkstyle">
<echo>Starting checkstyle</echo>
<checkstyle config="rules/sun_checks.xml" failOnViolation="false">
<fileset dir="src" includes="**/*.java"/>
<formatter type="plain"/>
<formatter type="xml" toFile="build/checkstyle_errors.xml"/>
</checkstyle>
<echo>Checkstyle finished</echo>
</target>
</project>
The quote from Checkstyle site:
failOnViolation - Specifies whether the build will continue even if
there are violations. Defaults to "true".
You can download checkstyle-5.4-bin.zip from here.
The distribution package contains sun_checks.xml - checkstyle configuration that checks the sun coding conventions and checkstyle-x.x-all.jar library with task engine.

Relative path problem in ANT junit task

I have setup an ant script as eclipse builder to automatically run all my tests, like below:
<project name="auto-test" default="test">
<property name="tst-dir" location="C:\STAF\services\custom\TopCoder\bin" />
<path id="classpath.base" />
<path id="classpath.test">
<pathelement location="D:\eclipse\eclipse\plugins\org.junit4_4.3.1\junit.jar" />
<pathelement location="${tst-dir}" />
<path refid="classpath.base" />
</path>
<target name="test" description="Run the tests">
<junit>
<classpath refid="classpath.test" />
<formatter type="brief" usefile="false" />
<test name="testDataGenerator.test.AllTests" />
</junit>
</target>
</project>
It was all good before I changed a test fixture file from absolute path to relative path:
SAXReader reader = new SAXReader();
Document document = reader.read(new File(".").getCanonicalPath()+"\\conf\\TestData.xml");
The ant task now try to open D:\eclipse\eclipse\conf\TestData.xml, instead of C:\STAF\services\custom\TopCoder\conf\TestData.xml, I've also try to run AllTests manually from Eclipse and it's all good.
Has anyone met similar problem before?
Thanks in advance.
PS. ANT_HOME=D:\eclipse\eclipse\plugins\org.apache.ant_1.7.0.v200706080842
Follow up:
I tried to run the ant script from command line, and find below:
C:\STAF\services\custom\TopCoder>ant -f c:\STAF\services\custom\TopCoder\task\build.xml, the ant script works correctly.
C:>ant -f c:\STAF\services\custom\TopCoder\task\build.xml, the script will claim: [junit] C:\conf\TestData.xml (The system cannot find the path specified)
I've also checked eclipse builder setting, there seems nothing to change the path to D:\eclipse\eclipse.
Java resolves relative paths against the current user directory, which is typically the directory from where the java program was invoked.
One way to overcome this issue is to define an environmental variable for your base path. Then, you could easily use "relative paths" (meaning, create absolute paths by concatenating the base path and the relative path).
Here is the solution I find:
Just as kgiannakakis mentioned, Ant also start executing its task from the location it was invoked, so we just need to change the working directory setting of our custom eclipse builder.
In the JRE tab, choose "Execution Environment".
Change the Working directory to your current workspace.
Looks like I've missed the karma but anyway...
We do this:-
Build.xml
<project name="whatever">
<property file="build.${env.COMPUTERNAME}.properties"/>
<property file="build.properties"/>
build.properties
project.root=..
build.file.dir=${project.root}/buildfiles
deploy.dir=${project.root}/deploy
which of course you can override by creating your OWN build.computername.properties to allow for developer path differences etc

Resources