-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathotel.java-conventions.gradle.kts
More file actions
222 lines (186 loc) · 6.86 KB
/
otel.java-conventions.gradle.kts
File metadata and controls
222 lines (186 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import io.opentelemetry.gradle.OtelJavaExtension
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
plugins {
`java-library`
jacoco
id("otel.errorprone-conventions")
id("otel.spotless-conventions")
id("otel.japicmp-conventions")
id("org.owasp.dependencycheck")
}
val otelJava = extensions.create<OtelJavaExtension>("otelJava")
group = "io.opentelemetry.contrib"
base.archivesName.set("opentelemetry-${project.name}")
// Version to use to compile code and run tests.
val DEFAULT_JAVA_VERSION = JavaVersion.VERSION_21
java {
toolchain {
languageVersion.set(
otelJava.minJavaVersionSupported.map { JavaLanguageVersion.of(Math.max(it.majorVersion.toInt(), DEFAULT_JAVA_VERSION.majorVersion.toInt())) }
)
}
withJavadocJar()
withSourcesJar()
}
tasks {
withType<JavaCompile>().configureEach {
with(options) {
release.set(otelJava.minJavaVersionSupported.map { it.majorVersion.toInt() })
if (name!="jmhCompileGeneratedClasses") {
compilerArgs.addAll(
listOf(
"-Xlint:all",
// We suppress the "try" warning because it disallows managing an auto-closeable with
// try-with-resources without referencing the auto-closeable within the try block.
"-Xlint:-try",
// We suppress the "processing" warning as suggested in
// https://groups.google.com/forum/#!topic/bazel-discuss/_R3A9TJSoPM
"-Xlint:-processing",
// We suppress the "options" warning because it prevents compilation on modern JDKs
"-Xlint:-options",
// Fail build on any warning
"-Werror",
),
)
}
encoding = "UTF-8"
if (name.contains("Test")) {
// serialVersionUI is basically guaranteed to be useless in tests
compilerArgs.add("-Xlint:-serial")
compilerArgs.add("-Xlint:-this-escape")
}
}
}
withType<Test>().configureEach {
useJUnitPlatform()
val maxTestRetries = gradle.startParameter.projectProperties["maxTestRetries"]?.toInt() ?: 0
develocity.testRetry {
// You can see tests that were retried by this mechanism in the collected test reports and build scans.
maxRetries.set(maxTestRetries)
}
testLogging {
exceptionFormat = TestExceptionFormat.FULL
showStandardStreams = true
}
configure<JacocoTaskExtension> {
// only care about code coverage for code in this repository
// (in particular avoiding netty classes which sometimes end up
// causing sporadic CI failures)
includes = listOf("io/opentelemetry/contrib/**")
}
}
withType<Javadoc>().configureEach {
exclude("io/opentelemetry/**/internal/**")
with(options as StandardJavadocDocletOptions) {
source = "8"
encoding = "UTF-8"
docEncoding = "UTF-8"
breakIterator(true)
addBooleanOption("html5", true)
addBooleanOption("Xdoclint:all,-missing", true)
}
}
}
// Add version information to published artifacts.
plugins.withId("otel.publish-conventions") {
tasks {
register("generateVersionResource") {
val moduleName = otelJava.moduleName
val propertiesDir = moduleName.map { layout.buildDirectory.file("generated/properties/${it.replace('.', '/')}") }
val projectVersion = project.version.toString()
inputs.property("project.version", projectVersion)
outputs.dir(propertiesDir)
doLast {
File(propertiesDir.get().get().asFile, "version.properties").writeText("contrib.version=${projectVersion}")
}
}
}
sourceSets {
main {
output.dir(layout.buildDirectory.dir("generated/properties"), "builtBy" to "generateVersionResource")
}
}
}
val dependencyManagement by configurations.creating {
isCanBeConsumed = false
isCanBeResolved = false
}
dependencies {
dependencyManagement(platform(project(":dependencyManagement")))
afterEvaluate {
configurations.configureEach {
if (isCanBeResolved && !isCanBeConsumed) {
extendsFrom(dependencyManagement)
}
}
}
compileOnly("com.google.code.findbugs:jsr305")
compileOnly("com.google.errorprone:error_prone_annotations")
}
testing {
suites.withType(JvmTestSuite::class).configureEach {
useJUnitJupiter("5.14.2")
dependencies {
implementation(project())
implementation(enforcedPlatform("org.testcontainers:testcontainers-bom:2.0.2"))
implementation(enforcedPlatform("com.google.guava:guava-bom:33.5.0-jre"))
implementation(enforcedPlatform("com.linecorp.armeria:armeria-bom:1.34.0"))
compileOnly("com.google.auto.value:auto-value-annotations")
compileOnly("com.google.errorprone:error_prone_annotations")
compileOnly("com.google.code.findbugs:jsr305")
implementation("org.mockito:mockito-core")
implementation("org.mockito:mockito-junit-jupiter")
implementation("org.assertj:assertj-core")
implementation("org.awaitility:awaitility")
implementation("io.github.netmikey.logunit:logunit-jul")
}
}
}
tasks {
check {
dependsOn(testing.suites)
}
}
fun isJavaVersionAllowed(version: JavaVersion): Boolean {
if (otelJava.minJavaVersionSupported.get() > version) {
return false
}
if (otelJava.maxJavaVersionForTests.isPresent && otelJava.maxJavaVersionForTests.get().compareTo(version) < 0) {
return false
}
return true
}
afterEvaluate {
val testJavaVersion = gradle.startParameter.projectProperties["testJavaVersion"]?.let(JavaVersion::toVersion)
val useJ9 = gradle.startParameter.projectProperties["testJavaVM"]?.run { this=="openj9" }
?: false
tasks.withType<Test>().configureEach {
if (testJavaVersion!=null) {
javaLauncher.set(
javaToolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(testJavaVersion.majorVersion))
implementation.set(if (useJ9) JvmImplementation.J9 else JvmImplementation.VENDOR_SPECIFIC)
}
)
isEnabled = isEnabled && isJavaVersionAllowed(testJavaVersion)
} else {
// We default to testing with Java 11 for most tests, but some tests don't support it, where we change
// the default test task's version so commands like `./gradlew check` can test all projects regardless
// of Java version.
if (!isJavaVersionAllowed(DEFAULT_JAVA_VERSION) && otelJava.maxJavaVersionForTests.isPresent) {
javaLauncher.set(
javaToolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(otelJava.maxJavaVersionForTests.get().majorVersion))
}
)
}
}
}
}
dependencyCheck {
scanConfigurations = mutableListOf("runtimeClasspath")
suppressionFile = "buildscripts/dependency-check-suppressions.xml"
failBuildOnCVSS = 7.0f // fail on high or critical CVE
nvd.apiKey = System.getenv("NVD_API_KEY")
nvd.delay = 3500 // until next dependency check release (https://github.com/jeremylong/DependencyCheck/pull/6333)
}