forked from DataDog/dd-trace-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava.gradle
More file actions
348 lines (294 loc) · 10.1 KB
/
java.gradle
File metadata and controls
348 lines (294 loc) · 10.1 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
import java.time.Duration
apply plugin: 'java'
apply plugin: 'groovy'
apply from: "$rootDir/gradle/checkstyle.gradle"
apply from: "$rootDir/gradle/codenarc.gradle"
def applyCodeCoverage = !(project.plugins.hasPlugin('com.github.johnrengelman.shadow')
|| project.path.startsWith(":dd-java-agent:instrumentation:"))
if (applyCodeCoverage) {
apply from: "$rootDir/gradle/jacoco.gradle"
}
sourceCompatibility = 1.7
targetCompatibility = 1.7
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
}
// Note: ideally lombok plugin would do this for us, but currently it doesn't support custom
// source sets. See https://github.com/franzbecker/gradle-lombok/issues/17.
dependencies {
compileOnly sourceSets."main_$name".compileClasspath
compile sourceSets."main_$name".output
"main_${name}CompileOnly" "org.projectlombok:lombok:${project.lombok.version}" transitive false
"main_${name}AnnotationProcessor" "org.projectlombok:lombok:${project.lombok.version}" transitive false
}
jar {
from sourceSets."main_$name".output
}
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()
}
[JavaCompile, ScalaCompile].each { type ->
tasks.withType(type) {
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/"
}
}
}
}
apply plugin: "io.franzbecker.gradle-lombok"
lombok { // optional: values below are the defaults
version = versions.lombok
sha256 = ""
}
apply plugin: "eclipse"
eclipse {
classpath {
downloadSources = true
downloadJavadoc = true
}
}
if (configurations.find { it.name == 'jmh' }) {
eclipse.classpath.plusConfigurations += [configurations.jmh]
}
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
repositories {
mavenLocal()
jcenter()
mavenCentral()
}
dependencies {
testCompile deps.spock
testCompile deps.groovy
testCompile deps.testLogging
testCompile group: 'info.solidsoft.spock', name: 'spock-global-unroll', version: '0.5.1'
testCompile group: 'com.anotherchrisberry', name: 'spock-retry', version: '0.6.4'
testCompile group: 'com.github.stefanbirkner', name: 'system-rules', version: '1.19.0'
}
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.allJava
classpath = configurations.compileClasspath
options {
setMemberLevel JavadocMemberLevel.PUBLIC
setAuthor true
links "https://docs.oracle.com/javase/8/docs/api/"
}
}
tasks.register("sourceJar", Jar) {
from sourceSets.main.allJava
classifier = 'sources'
}
tasks.register("javaDocJar", Jar) {
from javadoc.destinationDir
classifier = 'javadoc'
dependsOn javadoc
}
artifacts {
archives sourceJar
archives javaDocJar
}
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))
}
// Disable default test tasks if current JVM doesn't match version requirements
tasks.withType(Test).configureEach {
if (name.endsWith("Generated")) {
return
}
// Always run all tests that are runnable on JVM used for compilation
onlyIf { isJavaVersionAllowed(JavaVersion.current()) }
}
// 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)
// 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
}
if (currentJavaHome == new File(javaHome).toPath().toRealPath()) {
// Skip JVM implementation we are running gradle on
continue
}
def parentTask = task "testJava${javaName}"() {
group = 'Verification'
description = "Run tests for Java ${javaName}"
}
tasks.check.dependsOn parentTask
tasks.withType(Test).all {
//if (name.endsWith("Generated")) {
if (!name.equals("test")) {
// The way we're copying the test doesn't currently work with "test-sets" generated tests.
return
}
def clonedTask = it
def newTask = task "${clonedTask.name}Java${javaName}Generated"(type: 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.dependsOn newTask
}
}
tasks.withType(Test).configureEach {
// All tests must complete within 2 minutes.
timeout = Duration.ofMinutes(2)
// Disable all tests if skipTests property was specified
onlyIf { !project.rootProject.hasProperty("skipTests") }
}
plugins.withType(BasePlugin) {
project.afterEvaluate {
def deleteTasks = tasks.withType(Delete) + project.tasks.findByPath('clean')
def otherTasks = tasks - deleteTasks
otherTasks*.mustRunAfter deleteTasks
}
}