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:9.10.0') } } // 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:
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.
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.
Followed the same instructions but its failing with the below error
> Task :sonargraphReport FAILED
Using SonargraphBuild installation: SonargraphBuild-9.0.0.396_2016-08-26
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ‘:sonargraphReport’.
> org/gradle/internal/classloader/MutableURLClassLoader
Thanks for your comment. Please send technical questions to [email protected]. In your example try updating our gradle plugin to version 9.10.0. I updated the article with the new version.