Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize file configuration #5399

Merged
merged 12 commits into from
Aug 3, 2023
Prev Previous commit
Next Next commit
Generate builders, ensure @generated annotation is added
  • Loading branch information
jack-berg committed Aug 2, 2023
commit d77b1c1c736a3ab3f30fd6260f5d361e3a9870e6
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencyCheck {
"annotationProcessor",
"animalsniffer",
"spotless-1972451482", // spotless-1972451482 is a weird configuration that's only added in jaeger-proto
"js2p",
"jmhAnnotationProcessor",
"jmhCompileClasspath",
"jmhRuntimeClasspath",
Expand Down
53 changes: 49 additions & 4 deletions sdk-extensions/incubator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ dependencies {
testImplementation("com.google.guava:guava-testlib")
}

// The following tasks download the JSON Schema files from open-telemetry/opentelemetry-configuration and generate classes from the type definitions which are used with jackson-databind to parse JSON / YAML to the configuration schema.
// The sequence of tasks is:
// 1. downloadConfigurationSchema - download configuration schema from open-telemetry/opentelemetry-configuration
// 2. unzipConfigurationSchema - unzip the configuration schema archive contents to $buildDir/configuration/
// 3. generateJsonSchema2Pojo - generate java POJOs from the configuration schema
// 4. replaceGeneratedAnnotation - replace javax.annotation.processing.Generated with javax.annotation.Generated to address issue in org.jsonschema2pojo plugin preventing usage of java 8 @Generated annotation with our gradle build setup. Updated content are placed to tmp directory.
// 5. overwriteJs2p - overwrite original generated classes with versions containing updated @Generated annotation
// 6. deleteJs2pTmp - delete tmp directory
// ... proceed with normal sourcesJar, compileJava, etc

val configurationRef = "2107dbb6f2a6c99fe2f55d550796ee7e2286fd1d"
val configurationRepoZip = "https://github.com/open-telemetry/opentelemetry-configuration/archive/$configurationRef.zip"

Expand All @@ -44,7 +54,7 @@ val downloadConfigurationSchema by tasks.registering(Download::class) {
overwrite(false)
}

val downloadAndUnzipConfigurationSchema by tasks.registering(Copy::class) {
val unzipConfigurationSchema by tasks.registering(Copy::class) {
dependsOn("downloadConfigurationSchema")
from(zipTree(downloadConfigurationSchema.get().dest))
eachFile(closureOf<FileCopyDetails> {
Expand All @@ -59,12 +69,47 @@ jsonSchema2Pojo {
sourceFiles = setOf(file("$buildDir/configuration/schema"))
targetDirectory = file("$buildDir/generated/sources/js2p/java/main")
targetPackage = "io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model"
includeSetters = true

// Clear old source files to avoid contaminated source dir when updating
removeOldOutput = true

// Prefer builders to setters
includeSetters = false
generateBuilders = true
useInnerClassBuilders = true

// Force java 9+ @Generated annotation, since java 8 @Generated annotation isn't detected by
// jsonSchema2Pojo and annotation is skipped altogether
targetVersion = "1.9"
}

val generateJsonSchema2Pojo = tasks.getByName("generateJsonSchema2Pojo")
generateJsonSchema2Pojo.dependsOn(unzipConfigurationSchema)

val replaceGeneratedAnnotation by tasks.registering(Copy::class) {
from("$buildDir/generated/sources/js2p")
into("$buildDir/generated/sources/js2p-tmp")
filter {
it
// Replace java 9+ @Generated annotation with java 8 version
.replace("import javax.annotation.processing.Generated", "import javax.annotation.Generated")
// Add @SuppressWarnings("rawtypes") annotation to address raw types used in jsonschema2pojo builders
.replace("@Generated(\"jsonschema2pojo\")", "@Generated(\"jsonschema2pojo\")\n@SuppressWarnings(\"rawtypes\")")
}
dependsOn(generateJsonSchema2Pojo)
}
val overwriteJs2p by tasks.registering(Copy::class) {
from("$buildDir/generated/sources/js2p-tmp")
into("$buildDir/generated/sources/js2p")
dependsOn(replaceGeneratedAnnotation)
}
val deleteJs2pTmp by tasks.registering(Delete::class) {
delete("$buildDir/generated/sources/js2p-tmp/")
dependsOn(overwriteJs2p)
}

tasks.getByName("generateJsonSchema2Pojo").dependsOn(downloadAndUnzipConfigurationSchema)
tasks.getByName("sourcesJar").dependsOn("generateJsonSchema2Pojo")
tasks.getByName("compileJava").dependsOn(deleteJs2pTmp)
tasks.getByName("sourcesJar").dependsOn(deleteJs2pTmp)

// Exclude jsonschema2pojo generated sources from checkstyle
tasks.named<Checkstyle>("checkstyleMain") {
Expand Down
Loading