Browse Source

SONAR-20258 Generate package-info files automatically

tags/10.2.0.77647
Aurelien Poscia 8 months ago
parent
commit
ac02a47ce7
3 changed files with 37 additions and 0 deletions
  1. 35
    0
      build.gradle
  2. 1
    0
      plugins/sonar-education-plugin/build.gradle
  3. 1
    0
      ut-monitoring/build.gradle

+ 35
- 0
build.gradle View File

@@ -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

+ 1
- 0
plugins/sonar-education-plugin/build.gradle View File

@@ -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'

+ 1
- 0
ut-monitoring/build.gradle View File

@@ -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) {

Loading…
Cancel
Save