In the previous articles, Previous whilst working on various projects at IDR Solutions I went through the process of how to build your own Java Application with Apache Ant and also how to set up Jenkins. In this tutorial i will show you how to perform automated static analysis test on your source code.
I will suggest you see the previous article before proceeding if you haven’t. This will make it easier to follow.
What is PMD
It is a static rule-set based Java source code analyser that identifies potential problems in your source code. There are other static analysers like FindBugs. You can get PMD from here
From our previous AntProject, open the jbuild.xml file. you can get the code here if you do not have it. Now in the jbuild file, copy and paste this code before the closing project tag. Also change the pmd.dir property value to where u installed you pmd
<!-- ================== PMD : static analysis checks on code =================== -->
Now if running on MAC/LINUX, create a bash script called testSA.bash, copy the script below, change the ANT_HOME to where you installed ant paste.
#!/bin/bash #1.4 export ANT_HOME=/PDFdata/library/apache_ant/ export PATH=$ANT_HOME/bin:$PATH echo "Build PMD" ant -buildfile jbuild.xml pmd if [ $? -eq 1 ]; then echo "Failed on testSA" exit 1 fi
On Windows, make sure your ANT_HONE Environment variable is set, create a batch file the copy and paste the code below
@ECHO OFF echo "Build PMD" ant -buildfile jbuild.xml pmd IF ERRORLEVEL 1 ( echo "Failed on testSA" exit 1 )
Now we need to create the a file that will have all the pmd rules we want o check. Create a file called pmdRules.xml in the AntProject and paste this code
<!--?xml version="1.0" encoding="UTF-8"?--> This ruleset checks my code for bad stuff <!-- Checking for unused imports --> <!-- Check for Empty Catch blocks --> <!-- Check for Empty try blocks -->
In the xml file above, we are only checking for unused import and empty try and catch blocks.You can find more rules here. Your project should now be looking like this.
Refer to previous article to set up Jenkins task.
To do that I am going to push my go to GitHub and manually run it for the first time.
As you can see it pulled from my GitHub repo and was started by me.
Now i will push a code that will break the test.
As you can see this one was performed by SCM change. so each time you push to GitHub the test automatically runs and sends you an email if it failed.
IDRsolutions develop a Java PDF library, a PDF forms to HTML5 converter, a PDF to HTML5 or SVG converter and a Java Image Library that doubles as an ImageIO replacement. On the blog our team post about anything interesting they learn about.