Using the JDepend plugin

To get quality metrics for our code base, we can use JDepend. JDepend traverses the generated class files in our project and generates design quality metrics. To use JDepend, we simply have to apply the JDepend plugin in our project. This will add a jdependMain and jdependTest task. For each extra source set in our project, a jdepend<SourceSet> task is added. These tasks are all dependency tasks of the check task.

We must configure a repository so Gradle can fetch the JDepend dependencies. Gradle doesn't provide the JDepend libraries in the Gradle distribution. This means that we can easily use another version of JDepend, independent of the Gradle version we are using. We see this behavior in the other code quality plugins as well. To change a version number, we simply have to set the toolVersion property of the JDepend plugin.

In the following example build file, we apply the JDepend plugin and create an extra source set:

apply plugin: 'java'
apply plugin: 'jdepend'

// Repository definition to get JDepend libraries.
repositories {
  mavenCentral()
}

// We can change the version of JDepend to be used.
jdepend.toolVersion = '2.9.1'

// Custom source set so jdependRestApi task is created.
sourceSets {
  restApi
}

When we invoke the tasks task, we will see that three jdepend tasks are created as a dependency for the check task:

$ gradle tasks --all
...

Verification tasks
------------------
check - Runs all checks. [classes, restApiClasses, test, testClasses]
  jdependMain - Run JDepend analysis for main classes
  jdependRestApi - Run JDepend analysis for restApi classes
  jdependTest - Run JDepend analysis for test classes
test - Runs the unit tests. [classes, testClasses]

...

The jdepend tasks create statistics about our code. The results are stored in an XML file in the build/reports/jdepend directory. We can configure the JDepend plugin so the directory that we store the reports in is different. For each jdepend task, we can also alter the output format. Instead of XML, we can generate a text file with the statistics about our code. We have to choose between XML and text; we cannot choose both report outputs for a single jdepend task.

The following sample build file shows several options on how we can change the reports with information about our source code:

apply plugin: 'java'
apply plugin: 'jdepend'

// Repository definition to get JDepend libraries.
repositories {
  mavenCentral()
}

jdepend.reportsDir = file("${reporting.baseDir}/jdepend-output")

jdependMain {
  reports {
    text { 
      enabled = 'true'
      destination = file("${jdepend.reportsDir}/jdepend.txt")
    }
    xml {
      enabled = !text.enabled
    }
  }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.254.151