forked from DataDog/dd-trace-java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjava.gradle
More file actions
377 lines (326 loc) · 11.9 KB
/
Copy pathjava.gradle
File metadata and controls
377 lines (326 loc) · 11.9 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import java.time.Duration
apply plugin: 'java'
apply plugin: 'groovy'
apply from: "$rootDir/gradle/codenarc.gradle"
apply from: "$rootDir/gradle/forbiddenapis.gradle"
apply from: "$rootDir/gradle/spotless.gradle"
apply from: "$rootDir/gradle/spotbugs.gradle"
apply from: "$rootDir/gradle/repositories.gradle"
def forkedTestLimit = gradle.sharedServices.registerIfAbsent("forkedTestLimit", BuildService) {
maxParallelUsages = 2
}
// Task for tests that want to run forked in their own separate JVM
tasks.register('forkedTest', Test) {
setIncludes(["**/*ForkedTest*"])
jvmArgs += ["-Xms256M", "-Xmx256M"]
forkEvery 1
// Limit the number of concurrent forked tests
usesService(forkedTestLimit)
}
test {
exclude("**/*ForkedTest*")
dependsOn "forkedTest"
}
def applyCodeCoverage = !(
project.path.startsWith(":dd-smoke-tests") ||
project.path == ":dd-java-agent" ||
project.path == ":dd-java-agent:load-generator" ||
project.path.startsWith(":dd-java-agent:benchmark") ||
project.path.startsWith(":dd-java-agent:instrumentation"))
if (applyCodeCoverage) {
apply from: "$rootDir/gradle/jacoco.gradle"
}
sourceCompatibility = 1.7
targetCompatibility = 1.7
// when building with Java 9+, lazily set compiler --release flag to match target
def skipSettingCompilerRelease = project.findProperty('skipSettingCompilerRelease')
if (!skipSettingCompilerRelease && JavaVersion.current().isJava9Compatible()) {
compileJava.options.release = project.provider {
JavaVersion.toVersion(targetCompatibility).majorVersion as Integer
}
}
if (project.hasProperty('minJavaVersionForTests') && project.getProperty('minJavaVersionForTests') != JavaVersion.VERSION_1_7) {
def version = JavaVersion.toVersion(project.getProperty('minJavaVersionForTests'))
def name = "java$version.majorVersion"
sourceSets {
"main_$name" {
java.srcDirs "${project.projectDir}/src/main/$name"
}
}
"compileMain_${name}Java" {
sourceCompatibility = version
targetCompatibility = version
}
dependencies {
compileOnly sourceSets."main_$name".compileClasspath
compile sourceSets."main_$name".output
}
jar {
from sourceSets."main_$name".output
}
// In some cases we would like to avoid setting java version to `minJavaVersionForTests`.
// For example we would like to be able to run profiling tests with ZULU8, but we cannot run it with other JDK8 implementations at the moment
def skipSettingTestJavaVersion = project.hasProperty('skipSettingTestJavaVersion') && project.getProperty('skipSettingTestJavaVersion')
if (!skipSettingTestJavaVersion) {
tasks.withType(JavaCompile).configureEach {
if (it.name.toLowerCase().contains("test")) {
sourceCompatibility = version
targetCompatibility = version
}
}
}
}
java {
// See https://docs.gradle.org/current/userguide/upgrading_version_5.html, Automatic target JVM version
disableAutoTargetJvm()
withJavadocJar()
withSourcesJar()
}
[JavaCompile, ScalaCompile].each { type ->
tasks.withType(type).configureEach {
doFirst {
// We do this specifically for Java7 bytecode generation because we would like to be able to compile
// with Java8+ compiler. This likely would require some modifications when we switch to java11 compiler.
// Using proper Java7 bootstrap and extensions allows to be sure our code will run on real Java7.
if (JavaVersion.toVersion(sourceCompatibility) == JavaVersion.VERSION_1_7
&& JavaVersion.current() != JavaVersion.VERSION_1_7
&& System.env.JAVA_7_HOME != null) {
options.fork = true
options.bootstrapClasspath = fileTree(include: ['*.jar'], dir: "${System.env.JAVA_7_HOME}/jre/lib/")
options.extensionDirs = "${System.env.JAVA_7_HOME}/jre/lib/ext/"
}
}
}
}
jar {
/**
Make Jar build fail on duplicate files
By default Gradle Jar task can put multiple files with the same name
into a Jar. This may lead to confusion. For example if auto-service
annotation processing creates files with same name in `scala` and
`java` directory this would result in Jar having two files with the
same name in it. Which in turn would result in only one of those
files being actually considered when that Jar is used leading to very
confusing failures.
Instead we should 'fail early' and avoid building such Jars.
*/
duplicatesStrategy = 'fail'
}
tasks.register("packageSources", Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts.archives packageSources
dependencies {
testCompile deps.spock
testCompile deps.groovy
testCompile deps.testLogging
testCompile group: 'info.solidsoft.spock', name: 'spock-global-unroll', version: '0.5.1'
}
jar {
manifest {
attributes(
"Implementation-Title": project.name,
"Implementation-Version": project.version,
"Implementation-Vendor": "Datadog",
"Implementation-URL": "https://github.com/datadog/dd-trace-java",
)
}
}
tasks.withType(Javadoc).configureEach {
options.encoding = "utf-8"
options.docEncoding = "utf-8"
options.charSet = "utf-8"
options.addStringOption('Xdoclint:none', '-quiet')
doFirst {
if (project.ext.has("apiLinks")) {
options.links(*project.apiLinks)
}
}
}
javadoc {
source = sourceSets.main.java.srcDirs
classpath = configurations.compileClasspath
options {
setMemberLevel JavadocMemberLevel.PUBLIC
setAuthor true
links "https://docs.oracle.com/javase/8/docs/api/"
source = 8
}
}
project.afterEvaluate {
if (project.plugins.hasPlugin('org.unbroken-dome.test-sets') && configurations.hasProperty("latestDepTestRuntime")) {
tasks.withType(Test).configureEach {
doFirst {
def testArtifacts = configurations.testRuntime.resolvedConfiguration.resolvedArtifacts
def latestTestArtifacts = configurations.latestDepTestRuntime.resolvedConfiguration.resolvedArtifacts
assert testArtifacts != latestTestArtifacts: "latestDepTest dependencies are identical to test"
}
}
}
}
if (project.plugins.hasPlugin('com.github.johnrengelman.shadow')) {
// Remove the no-deps jar from the archives to prevent publication
configurations.archives.with {
artifacts.remove artifacts.find {
if (it.hasProperty("delegate")) {
it.delegate.archiveTask.is jar
} else {
it.archiveTask.is jar
}
}
}
artifacts {
archives shadowJar
}
}
if (project.hasProperty("removeJarVersionNumbers") && removeJarVersionNumbers) {
tasks.withType(AbstractArchiveTask).configureEach {
version = null
}
}
if (project.parent && project.parent.hasProperty("javaExecutableVersionCache")) {
project.ext.javaExecutableVersionCache = project.parent.ext.javaExecutableVersionCache
} else {
project.ext.javaExecutableVersionCache = [:]
}
JavaVersion getJavaExecutableVersion(String path) {
def cache = project.ext.javaExecutableVersionCache
if (cache.containsKey(path)) {
return cache.get(path)
}
new ByteArrayOutputStream().withStream { stream ->
exec {
commandLine = [path, "-version"]
errorOutput = stream
}
def matcher = stream.toString() =~ /^(?:java|openjdk) version "([^"]+)"/
if (matcher) {
def version = JavaVersion.toVersion(matcher.group(1))
cache.put(path, version)
return version
} else {
throw new GradleScriptException("Cannot determine java version: ${stream.toString}")
}
}
}
ext {
getJavaExecutableVersion = this.&getJavaExecutableVersion
}
def isJavaVersionAllowed(JavaVersion version) {
if (project.hasProperty('minJavaVersionForTests') && project.getProperty('minJavaVersionForTests').compareTo(version) > 0) {
return false
}
if (project.hasProperty('maxJavaVersionForTests') && project.getProperty('maxJavaVersionForTests').compareTo(version) < 0) {
return false
}
return true
}
def isJdkForced(String javaName) {
return (project.hasProperty('forceJdk') && project.getProperty('forceJdk').contains(javaName))
}
def isTestingInstrumentation(Project project) {
return ["junit-4.10", "testng-6.4", "junit-5.3"].contains(project.name)
}
// This is slightly complicated because we need to dereference symlinks to make sure
// we are considering same JVM implementation
def currentJavaHome = new File(System.getProperty("java.home")).toPath().toRealPath()
if (currentJavaHome.endsWith("jre")) {
currentJavaHome = currentJavaHome.parent
}
// Generate tests tasks for all provided JVMs
for (def env : System.getenv().entrySet()) {
def matcher = env.key =~ /JAVA_([^_]+)_HOME/
if (!matcher) {
continue
}
def javaName = matcher.group(1)
def javaHome = env.value
def javaPath = "$javaHome/bin/java"
def javaVersion = getJavaExecutableVersion(javaPath)
if (currentJavaHome == new File(javaHome).toPath().toRealPath()) {
// Skip JVM implementation we are running gradle on
continue
}
def parentTask = tasks.register("testJava${javaName}") {
group = 'Verification'
description = "Run tests for Java ${javaName}"
}
tasks.named('check').configure {
dependsOn parentTask
}
tasks.withType(Test).all {
//if (name.endsWith("Generated")) {
if (!name.equals("test") && !name.equals("forkedTest")) {
// The way we're copying the test doesn't currently work with "test-sets" generated tests.
return
}
def clonedTask = it
def newTask = tasks.register("${clonedTask.name}Java${javaName}Generated", clonedTask.class) {
description "Runs $clonedTask.name under java ${javaName}"
executable = javaPath
if (javaName == "7") {
// Disable JIT for this method. Sometimes Java7 JVM crashes trying to compile it.
jvmArgs '-XX:CompileCommand=exclude,net.bytebuddy.description.type.TypeDescription$Generic$Visitor$Substitutor::onParameterizedType'
}
onlyIf { isJavaVersionAllowed(javaVersion) || isJdkForced(javaName) }
if (applyCodeCoverage) {
jacoco {
// Disable jacoco for additional JVM tests to speed things up a bit
enabled = false
}
}
}
parentTask.configure {
dependsOn newTask
}
}
}
// Go through the Test tasks and configure them
tasks.withType(Test).configureEach {
if (project.findProperty("enableJunitPlatform") == true) {
useJUnitPlatform()
}
// All tests must complete within 15 minutes.
// This value is quite big because with lower values (3 mins) we were experiencing large number of false positives
timeout = Duration.ofMinutes(15)
// Disable all tests if skipTests property was specified
onlyIf { !project.rootProject.hasProperty("skipTests") }
// Enable force rerun of tests with -Prerun.tests.${project.name}
outputs.upToDateWhen {
!project.rootProject.hasProperty("rerun.tests.${project.name}")
}
//Avoid executing classes used to test testing frameworks instrumentation
if (isTestingInstrumentation(project)) {
exclude "**/TestSucceed*"
exclude "**/TestFailed*"
exclude "**/TestFailedWithSuccessPercentage*"
exclude "**/TestError*"
exclude "**/TestSkipped*"
exclude "**/TestSkippedClass*"
exclude "**/TestInheritance*", "**/BaseTestInheritance*"
exclude "**/TestFactory*"
exclude "**/TestParameterized*"
exclude "**/TestRepeated*"
exclude "**/TestTemplate*"
exclude "**/TestDisableTestTrace*"
exclude "**/TestAssumption*"
exclude "**/TestParameterized*"
}
// Split up tests that want to run forked in their own separate JVM for generated tasks
if (name.startsWith("forkedTest") || name.endsWith("ForkedTest")) {
setIncludes(["**/*ForkedTest*"])
jvmArgs += ["-Xms256M", "-Xmx256M"]
forkEvery 1
// Limit the number of concurrent forked tests
usesService(forkedTestLimit)
} else {
exclude("**/*ForkedTest*")
}
// Disable default test tasks if the current JVM doesn't match version requirements
if (!name.endsWith("Generated")) {
// Always run all tests that are runnable on JVM used for compilation
onlyIf { isJavaVersionAllowed(JavaVersion.current()) }
it.executable = "$currentJavaHome/bin/java"
}
}