le.java-convention-gradle-plugin.0.2.1.source-code.se.bjurr.gradle-convention.gradle Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-convention-gradle-plugin Show documentation
Show all versions of java-convention-gradle-plugin Show documentation
Defines the java convention in my java projects.
def getGivenConfigJavaConventions() {
def givenConfig = [
// ---- default config ----
// mainClass: se.bjurr.violations.main.Main
mainClass: project.getProperties().getOrDefault("mainClass", ""),
sourceCompatibility: project.getProperties().getOrDefault("sourceCompatibility", "17"),
targetCompatibility: project.getProperties().getOrDefault("targetCompatibility", "17"),
maxViolations: Integer.parseInt(project.getProperties().getOrDefault("maxViolations", "0")),
formattingExcludedPatterns: project.getProperties().getOrDefault("formattingExcludedPatterns", "**/gen/**,**/generated/**"),
generatedSourceFolders: project.getProperties().getOrDefault("generatedSourceFolders", "src/gen/java,src/generated/java"),
extraTestSourceFolders: project.getProperties().getOrDefault("extraTestSourceFolders", "src/test/generated"),
jarResourcesFolder: project.getProperties().getOrDefault("jarResourcesFolder", "src/jar/resources"),
useViolations: project.getProperties().getOrDefault("useViolations", "true") == "true",
// ---- default config ----
]
return givenConfig
}
def getEffectiveJavaConventions() {
def givenConfig = getGivenConfigJavaConventions()
return givenConfig + [
formattingExcludedPatterns: givenConfig.formattingExcludedPatterns.tokenize(','),
generatedSourceFolders: givenConfig.generatedSourceFolders.tokenize(','),
extraTestSourceFolders: givenConfig.extraTestSourceFolders.tokenize(','),
]
}
logger.info("Java Conventions: Given config: ${getGivenConfigJavaConventions()}")
logger.info("Java Conventions: Effective config: ${getEffectiveJavaConventions()}")
if (getEffectiveJavaConventions().useViolations) {
project.plugins.apply "se.bjurr.violations.violations-gradle-plugin"
}
project.plugins.apply "com.github.spotbugs"
project.plugins.apply "pmd"
project.plugins.apply 'java-library'
project.plugins.apply 'eclipse'
project.plugins.apply "com.diffplug.spotless"
project.plugins.apply 'groovy'
repositories {
mavenLocal()
mavenCentral()
}
try {
se.bjurr.violations.lib.util.Utils.updateReadmeWithReporters()
logger.info("Updated README with list of reporters")
} catch (e) {
logger.info("Not updating README with list of reporters: "+e.getMessage())
}
sourceCompatibility = getEffectiveJavaConventions().sourceCompatibility
targetCompatibility = getEffectiveJavaConventions().targetCompatibility
getEffectiveJavaConventions().generatedSourceFolders.each {
addSourceFolderIfExists(it)
}
getEffectiveJavaConventions().extraTestSourceFolders.each {
addTestSourceFolderIfExists(it)
}
test {
if (System.getProperty('DEBUG', 'false') == 'true') {
jvmArgs '-Xdebug',
'-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9009',
'-parameters'
}
}
allprojects {
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
}
}
eclipse {
classpath {
downloadSources = true
downloadJavadoc = true
}
}
java {
withSourcesJar()
withJavadocJar()
}
if (getEffectiveJavaConventions().mainClass != '') {
jar {
manifest {
attributes 'Main-Class': getEffectiveJavaConventions().mainClass
}
if (new File(getEffectiveJavaConventions().jarResourcesFolder).exists()) {
from getEffectiveJavaConventions().jarResourcesFolder
}
}
}
dependencies {
spotbugs 'com.github.spotbugs:spotbugs:4.8.3'
spotbugsPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.12.0'
compileOnly "com.github.spotbugs:spotbugs-annotations:${spotbugs.toolVersion.get()}"
}
assemble.doLast {
copyIfFound("spotbugs-exclude.xml")
copyIfFound("pmd.xml")
}
tasks.withType(com.github.spotbugs.snom.SpotBugsTask) {
enabled = false
}
spotbugsMain {
enabled = true
ignoreFailures = true
effort = com.github.spotbugs.snom.Effort.MAX
showProgress = true
showStackTraces = false
excludeFilter = new File("$buildDir/spotbugs-exclude.xml")
reportsDir = new File("$buildDir/reports/spotbugs/")
reports {
xml {
enabled true
}
}
}
pmd {
sourceSets = [sourceSets.main]
toolVersion = '6.55.0'
ruleSets = [] // Gradle has some defaults that needs to be cleared
ruleSetFiles = files("$buildDir/pmd.xml")
}
pmdMain {
source = "src/main/java"
ignoreFailures = true
reports {
xml.required = true
html.required = true
}
}
if (getEffectiveJavaConventions().useViolations) {
task violations(type: se.bjurr.violations.gradle.plugin.ViolationsTask) {
minSeverity = 'INFO'
detailLevel = 'VERBOSE' // PER_FILE_COMPACT, COMPACT or VERBOSE
maxViolations = getEffectiveJavaConventions().maxViolations
printViolations = true
// Many more formats available, see: https://github.com/tomasbjerre/violations-lib
violations = [
[
"FINDBUGS",
".",
".*/build/reports/findbugs/.*\\.xml\$",
"Findbugs"
],
[
"FINDBUGS",
".",
".*/build/reports/spotbugs/.*\\.xml\$",
"Spotbugs"
],
[
"PMD",
".",
".*/build/reports/pmd/.*\\.xml\$",
"Pmd"
]
]
}
check.finalizedBy violations
}
processResources.dependsOn spotlessApply
spotless {
java {
target '**/*.java'
googleJavaFormat()
targetExclude getEffectiveJavaConventions().formattingExcludedPatterns
}
groovyGradle {
target '*.gradle', '**/*.gradle'
greclipse()
}
json {
target '*.json', '**/*.json'
gson()
.indentWithSpaces(2)
.version('2.8.1')
}
}
def findResource(String name, classLoader = project.buildscript.classLoader) {
URL resource = classLoader.getResource(name)
if (resource == null) {
logger.lifecycle("Cannot find resource \"${name}\"")
return null;
}
return resource
}
def copyResourceToFile(URL resource, String copyTo, classLoader = project.buildscript.classLoader) {
logger.lifecycle("Copying ${resource} to ${copyTo}")
def content = resource.text
def parentFile = new File(copyTo).getParentFile()
if (parentFile != null) {
parentFile.mkdirs()
}
FileWriter fw = new FileWriter(copyTo)
BufferedWriter bw = new BufferedWriter(fw)
bw.write(content)
bw.close()
}
def copyIfFound(String name, String target=null, classLoader = project.buildscript.classLoader) {
URL resource = findResource(name, classLoader)
if (resource != null) {
logger.lifecycle("Found resource ${name} as ${resource}")
def newFilename = target == null ? "$buildDir/$name" : target
copyResourceToFile(resource, newFilename)
return new File(newFilename)
}
}
def addSourceFolderIfExists(String folder) {
if (new File(folder).exists()) {
logger.lifecycle("Detected ${folder}, adding it as source folder")
sourceSets.main.java.srcDirs += [folder]
}
}
def addTestSourceFolderIfExists(String folder) {
if (new File(folder).exists()) {
logger.lifecycle("Detected ${folder}, adding it as test source folder")
sourceSets.test.java.srcDirs += [folder]
}
}