Customising Sanity4J analysis

The analysis can be customised using an external configuration file. On start-up, Sanity4J attempts to read external configuration from a standard Java properties file named "sanity4j.properties" located in the working directory or the user's home directory. (The location of the file can be further specificed by using the externalPropertiesPath.)

Customising which tools are run.

The "sanity4j.toolsToRun" property controls which tools will be run by Sanity4J. Multiple tools can be listed here, in a comma-separated list. The Available tools are listed in the table below.

IDTool nameDescriptionSupported versions
checkstyleCheckstyleStatic source code analyser.4.4, 5.6, 7.7
jacoco-mergeJaCoCo MergeMerges multiple datafiles before the coverage analysis.0.7.9
jacocoJaCoCoUnit test coverage analysis.0.7.9
spotbugsSpotBugsStatic byte-code analyser.3.1.6
pmdPMDStatic source code analyser.4.2.1, 4.2.2, 4.2.5, 5.0.4, 5.6.1
pmd-cpdPMD CPDDetects copy & pasted source code.4.2.1, 4.2.2, 4.2.5, 5.0.4, 5.6.1

The default value for this parameter is:

sanity4j.toolsToRun=checkstyle,jacoco-merge,jacoco,spotbugs,pmd,pmd-cpd

Customising tool configuration.

The command line used to run each tool is specified by parameters of the form: sanity4j.tool.tool.version.command.

The default command line used by each tool may not be appropriate to your project, so the tool command lines can be modified as necessary. i.e. you may wish to modify the JDK used, JVM performance options, or the command line options passed to the actual tools themselves. As the most common "configuration" changes are expected to be made to the configuration files passed to the tools themselves (i.e. the set of Checkstyle rules to apply, the set of SpotBugs plugins used, or the set of PMD rulesets used.) special parameters of the below form can be used.

sanity4j.tool.tool.version.config

This can be placed at any point within the command line, and these parameters can then be set from within the Sanity4J invocation shells (ant, maven, eclipse and the GUI.) via more convenient means.

Variable substitution is available for tool locations and command lines. For example, ${java} will be replaced by the path to the JRE executable. The Variables available are detailed in the table below.

VariableValue
javaPath to JRE executable.
javaArgsJRE arguments (memory, classpath, defines).
productsThe location of the tools directory.
sourceThe path to the directory containing the combined sources to be analysed.
classesThe path to the directory containing the combined classes to be analysed.
libsThe path to the directory containing the combined libraries to be analysed.
coverageDataFileThe unit test coverage data file.
coverageMergeDataFilesThe list of unit test coverage data files.
tempDirThe temporary directory location.
File.separatorCharOS-specific File path separator character.
File.pathSeparatorOS-specific path separator character.
outputFileThe location where the tool should place it's output.
toolHomeThe home directory for the tool (not available for ".home" properties).

Here is an example of configuring Checkstyle 4.4 to only use the Sun Code Conventions:

sanity4j.tool.checkstyle.4.4.config=${toolHome}${File.separatorChar}sun_checks.xml
sanity4j.tool.checkstyle.4.4.command="${java}" ${javaArgs} com.puppycrawl.tools.checkstyle.Main -c "${sanity4j.tool.checkstyle.4.4.config}" -f xml -o "${outputFile}" -r ${source} 

Resource Expansion.

The .config parameter also allows for named resources that will be expanded if they are found on the classpath.

Sanity4J will locate the named resource on the classpath and extract it to a temporary location on the filesystem. Then it will modify the .config parameter to the expanded path of the located extracted resource.

So instead, the above parameters can be re-written as below.

sanity4j.tool.checkstyle.4.4.config=sun_checks.xml
sanity4j.tool.checkstyle.4.4.command="${java}" ${javaArgs} com.puppycrawl.tools.checkstyle.Main -c "${sanity4j.tool.checkstyle.4.4.config}" -f xml -o "${outputFile}" -r ${source} 

It should be noted that the Sanity4J jar contains the sun_checks.xml. So it is automatically on the classpath. For other resources that are not on the classpath, an additional clsspath can be specified for the tool.

sanity4j.tool.checkstyle.4.4.config=custom_checks.xml
sanity4j.tool.checkstyle.4.4.classpath=../custom/custom-plugins.jar

There are some special cases for the resource expansion.

  • If the name of the resource begins with a "-" or doesn't contain a ".", then no expansion occurs. This allows for arguments such as -pluginList to be passed through unchanged.
  • If the name of the resource ends with ".jar", then rather than extracting the resource, it searches the classpath for the name of that jar, and then replaces the .config with the matched found location.
  • The name matching for the jar also allows for name parts. This is useful for Maven dependencies where there is a version number associated with the name of the jar.

The resource expansion can also be applied to multiple files. The .config parameter is tokenised for spaces, commas and path seperators, then each token is located and expanded.

Below is another example showing the use of the special case resources. Note the use of the custom-plugins.jar resource which will match the custom-plugins-1.0.2.jar and subsequently be expanded. Note also the use of multiple resources expanded for spotbugs.

sanity4j.tool.pmd.4.2.5.config         =custom_pmd_rules.xml
sanity4j.tool.pmd.4.2.5.classpath      =../custom/custom-plugins-1.0.2.jar

sanity4j.tool.spotbugs.3.1.6.config    =-pluginList custom-plugins.jar -exclude sanity4j_filter_exclude.xml
sanity4j.tool.spotbugs.3.1.6.classpath =../custom/custom-plugins-1.0.2.jar

sanity4j.tool.checkstyle.4.4.config    =custom_checks.xml
sanity4j.tool.checkstyle.4.4.classpath =../custom/custom-plugins-1.0.2.jar

Customising issue categories and severities.

Issue weightings and exclusions can be adjusted in settings files for each individual tool (where supported) or centrally in the external Sanity4J property file. The names for each parameter are of the form <source>.<ruleName>.<option> .

The "source" corresponds to the tool which generates the issue. Possible values are "SpotBugs", "PMD" and "Checkstyle".

The "ruleName" is the tool-specific name of the rule which generates the issue (e.g. NP_LOAD_OF_KNOWN_NULL_VALUE).

The "option" must be one of the following:

  • The "category" option is used to set category paths. A category path is a text string, with path elements delimited by '/'. Multiple paths may be supplied, delimited by commas. For example, the value "Style,Maintenance/Code Size" would place issues under the categories "Style" and "Maintenance" --> "Code Size"
  • The "severity" option is used to set the issue severity. The parameter's value must be 0-4 (0=info, 1=low, 2=moderate, 3=significant, 4=high).
  • The "includes" option controls which Java class names should have the rule applied to them. All files are included by default. The parameter value should be a comma-delimited list of class names to include, using java regular expressions.
  • The "excludes" option controls which Java class names should be excluded from this rule. A class is excluded if it is matched by at least one exclude. The default exclude is nothing. The parameter's value should be a comma-delimited list of class names to exclude.

The following example shows parameters to exclude jUnit 3.x tests from CheckStyle's magic-number check, and set the severity for this issue in other classes to low.

Checkstyle.MagicNumberCheck.severity=1
Checkstyle.MagicNumberCheck.excludes=Test[A-Z].*,.*Test

Customising report output.

The weighting for the quality metric can be modified if necessary. The default weighting is shown below.

sanity4j.report.metric.quality.expression=1.0 - ((#highCount * 100.0 + #significantCount * 20.0 +
#moderateCount * 5.0 + #lowCount * 2.0 + #infoCount * 0.0) / #linesOfCode)

Customising the static content used by tools in the report.

Custom rules can be written for each of the tools used by Sanity4J. When a custom rule is written, it usually is accompanied by a page in the report specific for that custom rule.

Sanity4J uses the properties file "ExtractStaticContent.properties" to incorporate the page in the report.

This properties file can be placed in the working directory, or on the classpath. Therefore, if you write your own custom rule, and package the compiled class in a plugin jar for the tool, then you could additionally place the "ExtractStaticContent.properties" into that same jar as your custom rule.

The format of the file consists of a mapping of the name of the resource to be extracted, to a location of the file for the report.

SQL_TIMESTAMP_COMPARISON.html=rules/spotbugs/SQL_TIMESTAMP_COMPARISON.html

The above is an example for a custom "spotbugs" rule used to check that java.util.Date objects are not being used in comparison operations with java.sql.Timestamp objects.

The above named html file would be a page that describes incorrect and correct code, and is linked to by the report.

As the html file would be placed in the same jar as your plugin class, it needs to be extracted to a location where the report can reference it, thus the mapping in the properties file.