From ac02a47ce78aa3ec9483a1bb5e10a71f9803d8d2 Mon Sep 17 00:00:00 2001 From: Aurelien Poscia Date: Thu, 24 Aug 2023 17:55:59 +0200 Subject: [PATCH] SONAR-20258 Generate package-info files automatically --- build.gradle | 35 +++++++++++++++++++++ plugins/sonar-education-plugin/build.gradle | 1 + ut-monitoring/build.gradle | 1 + 3 files changed, 37 insertions(+) diff --git a/build.gradle b/build.gradle index ac3521d65be..9d9f525b57e 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,5 @@ import groovy.json.JsonOutput +import groovy.text.SimpleTemplateEngine import org.sonar.build.BlackBoxTest import static org.gradle.api.JavaVersion.VERSION_17 @@ -457,6 +458,30 @@ subprojects { // when needed (see protobuf modules for example) } + task generatePackageInfo { + doLast { + def allPathsContainingJavaFiles = [] as Set + + fileTree('src/main/java/').matching() { + include "*/**/*.java" + }.forEach { + allPathsContainingJavaFiles << it.toPath().toFile().getParent(); + } + + allPathsContainingJavaFiles.each { + String packageInfoPath = it + "/package-info.java" + File packageInfoFile = new File (packageInfoPath) + if (!packageInfoFile.exists()) { + logger.warn("Creating file: " + packageInfoPath) + def packageName = packageInfoFile.getParent().takeAfter("src/main/java/").replaceAll("/", "\\.") + String packageInfoContent = applyPackageInfoTemplate(packageName) + packageInfoFile << packageInfoContent + } + } + } + } + generatePackageInfo.finalizedBy(licenseFormat) + jacocoTestReport { reports { xml.required = true @@ -670,6 +695,16 @@ subprojects { } } +static def applyPackageInfoTemplate(packageName) { + def engine = new SimpleTemplateEngine() + def templateText = "@ParametersAreNonnullByDefault\n" + + "package $packageName;\n" + + "\n" + + "import javax.annotation.ParametersAreNonnullByDefault;\n" + def templateParams = ["packageName": packageName] + engine.createTemplate(templateText).make(templateParams).toString() +} + gradle.projectsEvaluated { gradle -> // yarn_run tasks can't all run in parallel without random issues // this script ensure all yarn_run tasks run sequentially diff --git a/plugins/sonar-education-plugin/build.gradle b/plugins/sonar-education-plugin/build.gradle index 5714f7698d8..7378f93ca26 100644 --- a/plugins/sonar-education-plugin/build.gradle +++ b/plugins/sonar-education-plugin/build.gradle @@ -4,6 +4,7 @@ configurations { dependencies { compileOnlyApi 'org.sonarsource.api.plugin:sonar-plugin-api' + compileOnlyApi 'com.google.code.findbugs:jsr305' implementation 'commons-io:commons-io' testImplementation 'junit:junit' diff --git a/ut-monitoring/build.gradle b/ut-monitoring/build.gradle index 39fe958dac7..b607704e80a 100644 --- a/ut-monitoring/build.gradle +++ b/ut-monitoring/build.gradle @@ -12,6 +12,7 @@ dependencies { api 'org.sonarsource.api.plugin:sonar-plugin-api' compileOnlyApi 'org.aspectj:aspectjtools' + compileOnlyApi 'com.google.code.findbugs:jsr305' } tasks.withType(JavaCompile) { -- 2.39.5