More Code Standards
Checkstyle Configuration
Java applications use Checkstyle to help write code that follows coding standards.
The configuration in the Gradle build file is as follows:
plugins {
id 'checkstyle'
}
dependencies {
checkstyle "com.puppycrawl.tools:checkstyle:10.9.3"
}
// ------------ Checkstyle configuration ---------
checkstyle {
configProperties.configFile = file("${project.rootDir}/config/checkstyle/checkstyle.xml")
configProperties.checkstyleSuppressionFile = file("${project.rootDir}/config/checkstyle/suppressions.xml")
ignoreFailures = false
}
tasks.register('checkstyleReport') {
doLast {
if (file("${layout.buildDirectory.asFile.get()}/reports/checkstyle/${project.ext.checkType}.xml").exists()) {
ant.xslt(in: "${layout.buildDirectory.asFile.get()}/reports/checkstyle/${project.ext.checkType}.xml",
style: "${project.rootDir}/config/checkstyle/checkstyle.xsl",
out: "${layout.buildDirectory.asFile.get()}/reports/checkstyle/checkstyle_${project.ext.checkType}.html")
}
}
}
// Tests can be excluded by mask
tasks.withType(Checkstyle).configureEach {
exclude '**/*Test*'
}
Checkstyle uses a configuration file config/checkstyle/checkstyle.xml
and a suppression file
config/checkstyle/suppressions.xml
. These files are kept
synchronized across all the repositories to ensure the same code style.
Optionally, there can be another file with XSL stylesheets config/checkstyle/checkstyle.xsl
to process XML reports generated by checkstyle Gradle task.
Gradle can ignore SpotBugs failures if ignoreFailures
flag is set to true.
Using Lombok
Lombok library is used to avoid boilerplate in code in many cases, presumably for Data Transfer Objects or domain modeling classes.
The configuration in the Gradle build file is as follows:
dependencies {
annotationProcessor 'org.projectlombok:lombok:1.18.34'
compileOnly (
'org.projectlombok:lombok'
)
testAnnotationProcessor 'org.projectlombok:lombok:1.18.34'
testCompileOnly (
'org.projectlombok:lombok'
)
}
SpotBugs Configuration
Using SpotBugs library can help detect suspicious and dodgy code fragments before the code goes to testing. It's encouraged to have this stage configured in the project. The library adds more cases to check with every new version, so it could be a separate development task to keep project code clean after SpotBugs upgrades.
The configuration in the Gradle build file is as follows:
plugins {
id 'com.github.spotbugs' version '6.0.7'
}
dependencies {
compileOnly (
'com.github.spotbugs:spotbugs:4.8.4',
'com.google.code.findbugs:annotations:3.0.1u2',
)
testCompileOnly (
"com.github.spotbugs:spotbugs:4.8.4",
"com.google.code.findbugs:annotations:3.0.1u2",
)
}
// ------------ Spotbugs configuration ---------
spotbugs {
reportsDir = file("${layout.buildDirectory.asFile.get()}/reports/spotbugs/main/spotbugs.html")
ignoreFailures = true
}
Gradle can ignore SpotBugs failures if ignoreFailures
flag is set to true.
Code Smells
Use the following site to surface check the code for most common errors and bad patterns: Code Smells.