Meet the Sonargraph Gradle Plugin – and Say Goodbye to JDepend

With the release of Sonargraph 8.8.0 today we also released the first version of our brand new Gradle plugin. It allows you to create reports for any Java project, even those that do not have a Sonargraph system specification. This is a quick and easy way to get some metrics and other findings like circular dependencies about your project. In this article I am going to show you, how you can use our Gradle plugin to make your build fail if your system contains cyclic package dependencies. Believe it or not, there are still people out there that use JDepend for this very reason (I did too, but that was more than a decade ago). Since you can do everything I am describing now with our free Sonargraph-Explorer license including the interactive visualization of cycles, I think you won’t regret saying “Good Bye” to JDepend and “Hello” to Sonargraph.

The first step is always to add the plugin to your build.gradle file of the root project and add some configuration:

// Root build.gradle file
apply plugin: 'com.hello2morrow.sonargraph'
 
buildscript
{
    // Define the hello2morrow maven repository and the dependency to the plugin
    repositories
    {
        mavenLocal()
        mavenCentral()
        maven { url 'http://maven.hello2morrow.com/repository' }
    }
 
    dependencies
    {
        classpath('com.hello2morrow:sonargraph-gradle-plugin:8.9.1') 
    }
}
 
// Configure the plugin
sonargraphDynamicReport
{
    // Freely usable activation code for Sonargraph-Explorer Build
    activationCode = "36E2-0F3E-643F-B4F2"
    qualityModelFile = "Sonargraph:Java.sgqm" // default Java quality model, optional
    failSet 
    {
        failOnEmptyWorkspace = true
        // The issue type for package cycle groups, fail the build if there are any
        include(issueType: "NamespaceCycleGroup")
    }
}

Now you can run gradle and create your report:

gradle clean build sonargraphDynamicReport

That was easy, wasn’t it? The build will fail if no classes can be found or if your project has package cycles. You will find the generated HTML report in the sub-directory “sonargraph” or your projects build directory. It will look similar to this:

Screen Shot 2016-03-22 at 5.35.54 PM

If the build fails due to package cycles you just use the standalone version of Sonargraph-Explorer to visualize and investigate them. The Sonargraph system that you need to open is generated into the build directory of Gradle. Don’t forget to refresh the system once you opened it.

Screen Shot 2016-03-22 at 6.31.49 PM

Now if you wanted to do something fancy with all those metrics you could decide to also create an XML file with all the metrics and findings and then use our open source integration-access project to read the data from the XML file. You could then feed some dashboard or trend charts with the data you retrieved from the XML report.

To add an XML report you would add a line to the plugin configuration:

reportFormat = "xml, html"

The full documentation for the plugin can be found here.

Keeping a project free of package cycle will make it much easier to maintain it for the long term (see my post “Love your Architecture”). It is an important first step for having a solid architectural structure. If you also want to enforce architectural rules have a look at Sonargraph-Architect, the big brother of Sonargraph-Explorer.

Please feel free to add your comments below. Feedback is always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *