-
Notifications
You must be signed in to change notification settings - Fork 147
/
build.gradle
355 lines (302 loc) · 13.3 KB
/
build.gradle
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
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer
import com.nr.builder.BuildManifestTask
import com.nr.builder.DependencyPatcher
import com.nr.builder.GitUtil
import com.nr.builder.Log4j2PluginFileMover
import com.nr.builder.publish.PublishConfig
plugins {
id("java-library")
id("maven-publish")
id("signing")
}
apply from: '../gradle/script/spotbugs-config.gradle'
apply from: '../gradle/script/jacoco.gradle'
configurations {
tests
// This configuration is intended for dependencies that will be
// compiled, extracted, and relocated.
shadowIntoJar
// This configuration is intended for dependencies whose jar files
// will be compiled, but included in the final jar compressed
jarIntoJar
// This configuration has the final artifact.
finalArtifact
}
configurations.implementation.extendsFrom(configurations.shadowIntoJar)
configurations.implementation.extendsFrom(configurations.jarIntoJar)
project.ext {
instrumentProjects = {
rootProject.subprojects.findAll { project -> project.path =~ /:instrumentation:/ }
}
}
java {
// by default, gradle will attempt to resolve only files
// that are compiled with a corresponding source version.
// However, module-util is compiled with Java 11 (because it
// has to be >= 9) and that will not work with this.
disableAutoTargetJvm()
withSourcesJar()
withJavadocJar()
}
dependencies {
// remove this stuff after we re-write the instrumentation
implementation("javax.servlet:javax.servlet-api:3.0.1")
// end remove
jarIntoJar project(":newrelic-api")
jarIntoJar project(":agent-bridge")
jarIntoJar project(":agent-bridge-datastore")
jarIntoJar project(":newrelic-weaver-api")
jarIntoJar project(":newrelic-weaver-scala-api")
jarIntoJar("com.newrelic.agent.java.security:newrelic-security-api:${securityAgentVersion}")
jarIntoJar("com.newrelic.agent.java.security:newrelic-security-agent:${securityAgentVersion}")
shadowIntoJar( group: 'commons-codec', name: 'commons-codec') {
version {
strictly "[1.13]"
}
}
shadowIntoJar project(":agent-interfaces")
shadowIntoJar project(":agent-model")
shadowIntoJar(project(":newrelic-weaver"))
shadowIntoJar(project(":infinite-tracing"))
shadowIntoJar(project(":discovery")) {
transitive = false
}
shadowIntoJar("com.newrelic.agent.java:newrelic-module-util-java:2.1")
shadowIntoJar("com.newrelic:jfr-daemon:1.12.0")
shadowIntoJar "org.ow2.asm:asm:$asmVersion"
shadowIntoJar "org.ow2.asm:asm-tree:$asmVersion"
shadowIntoJar "org.ow2.asm:asm-commons:$asmVersion"
shadowIntoJar "org.ow2.asm:asm-util:$asmVersion"
shadowIntoJar "org.ow2.asm:asm-analysis:$asmVersion"
shadowIntoJar 'net.sourceforge.jregex:jregex:1.2_01'
shadowIntoJar 'org.apache.httpcomponents:httpclient:4.5.13'
// We use StringUtils. Since we still support Java 7, we can't go much higher than this.
shadowIntoJar("org.apache.commons:commons-lang3:3.8.1")
// for command line parsing
shadowIntoJar 'commons-cli:commons-cli:1.2'
shadowIntoJar 'org.apache.logging.log4j:log4j-core:2.17.1'
shadowIntoJar 'org.slf4j:slf4j-api:1.7.25'
shadowIntoJar 'com.googlecode.json-simple:json-simple:1.1'
shadowIntoJar('com.google.guava:guava:30.1.1-jre')
shadowIntoJar('com.github.ben-manes.caffeine:caffeine:2.9.3')
shadowIntoJar("org.yaml:snakeyaml:1.33")
implementation("javax.management.j2ee:management-api:1.1-rev-1") {
transitive = false
}
testImplementation("com.google.code.gson:gson:2.8.9")
testImplementation("org.apache.struts:struts-core:1.3.10")
testImplementation("javax.servlet:jsp-api:2.0")
testImplementation("org.eclipse.jetty:jetty-server:8.1.22.v20160922")
testImplementation("org.eclipse.jetty:jetty-servlet:8.1.22.v20160922")
testImplementation(project(path: ':newrelic-weaver', configuration: 'testClasses'))
testImplementation("org.mockito:mockito-inline:4.11.0")
}
/**
* The buildManifest task creates a Properties file using org.reflections
* that contains references to classes that have specific annotations.
* See the source of the task in buildSrc.
*/
task buildManifest(type: BuildManifestTask, dependsOn: 'classes') {
setInputRuntimeClasspath(sourceSets.main.output.classesDirs)
}
/**
* The generateVersionProperties creates a properties file
* containing the built version of the Jar.
*/
task generateVersionProperties() {
def buildProps = new File(sourceSets.main.output.resourcesDir, "com/newrelic/agent/Agent.properties")
outputs.file(buildProps)
doLast {
buildProps.parentFile.mkdirs()
buildProps.text = """version=$version"""
}
}
/**
* The `relocatedShadowJar` task builds the initial shaded jar. It performs the following tasks:
* - Identifies all dependencies in the "shadowIntoJar" configuration
* This should include all third-party (not Java, not New Relic) dependencies
* This should also gather their dependencies (i.e., transitive dependencies)
* - Relocates any non-com.newrelic packages to com.newrelic.agent.deps
* - Rewrites all class files to honor the relocated classes
* - Transforms the Log4j2Plugins cache for the relocated classes
* The output of this task should contain:
* - The classes from the newrelic-agent package
* - The contents of newrelic-agent/src/main/resources
* - The relocated classes from the third-party dependencies
* - The newly-created Agent.properties from generateVersionProperties
*/
task relocatedShadowJar(type: ShadowJar) {
dependsOn("classes", "processResources", "generateVersionProperties")
from sourceSets.main.output.classesDirs
from(sourceSets.main.output.resourcesDir) {
exclude("*.jar")
}
setConfigurations([project.configurations.shadowIntoJar])
dependencies { filter ->
filter.exclude(filter.dependency("com.google.code.findbugs:jsr305"))
filter.exclude(filter.project(":newrelic-api"))
}
[
// stuff we use
"com.google", "org.yaml", "org.slf4j", "org.objectweb", "org.json",
"org.apache.commons", "org.apache.http", "org.apache.logging", "jregex",
"io.grpc", "com.squareup", "okio", "io.perfmark", "android", "com.github.benmanes",
// The following rules are to prevent these transitive dependencies from breaking anything
"org.apache.log4j", "org.apache.log", "org.apache.avalon",
"org.checkerframework", "org.dom4j", "org.zeromq", "org.apache.kafka",
"com.lmax", "com.conversantmedia", "org.jctools",
"com.fasterxml", "org.osgi", "org.codehaus", "org.fusesource", "kotlin", "org.jetbrains",
"okhttp3", "org.bouncycastle", "org.conscrypt", "org.openjsse"
].each {
relocate(it, "com.newrelic.agent.deps.$it")
}
// This has to happen here because it uses the class relocation rules to accomplish its task
transform(Log4j2PluginsCacheFileTransformer)
// For any given shadow task, `exclude` may be called exactly once.
// Repeated calls replace earlier calls.
exclude(
// these are files that come along with protobuf; we don't
// need them at runtime.
"**/*.proto",
// These are unnecessary files from our dependencies.
"META-INF/maven/**",
// Multiple dependencies
"LICENSE",
// log4j2
"META-INF/versions/9/module-info.class",
"META-INF/services/org.apache.logging*",
// asm
"module-info.class",
// httpclient
"META-INF/DEPENDENCIES",
"META-INF/LICENSE",
"META-INF/NOTICE",
// Commons CLI
"META-INF/LICENSE.txt",
"META-INF/NOTICE.txt",
// gRPC
"META-INF/services/javax.annotation.*"
)
mergeServiceFiles()
archiveBaseName.set("relocatedShadowJar")
}
/**
* <p>The `transformedShadowJar` task creates an intermediate shaded jar. The purpose of this
* task is to apply class transformations to our dependencies. These class transformations
* are under {@link com.nr.builder.DependencyPatcher}, and include:</p>
* <ol>
* <li>Altering calls to java.util.logging (within shaded Guava) to get the global logger</li>
* <li>Altering references to Log4j2Plugins.dat to a different file name</li>
* </ol>
* <p>The rationale for two shadow jar tasks is a limitation within shadowJar.
* Any relocations prevent transformers from running on class files. Without relocations,
* transformers will run on class files. Therefore, we have one step that relocates
* and a second step that transforms.
*/
task transformedShadowJar(type: ShadowJar) {
dependsOn 'relocatedShadowJar'
from(zipTree(project.tasks['relocatedShadowJar'].archiveFile.get().asFile.path))
// shadow will only apply the first transform that claims to transform a file,
// so the set of files they could possibly apply to _must_ be disjoint.
// DependencyPatcher applies only to class files.
transform(DependencyPatcher)
// Log4j2PluginFileMover applies only to the Log4j2Plugins.dat resource file.
transform(Log4j2PluginFileMover)
archiveBaseName.set("transformedShadowJar")
}
/**
* The newrelicVersionedAgentJar task builds the final output jar. This jar contains:
* <ul>
* <li>All the files from the transformedShadowJar task</li>
* <li>All the instrumentation jars as jars (not expanded)</li>
* <li>The jars from the other projects that get added to other classloaders, like agent-bridge
* (from the jarIntoJar configuration)</li>
* <li>LICENSE from updateLicenseFile</li>
* <li>PointcutClasses.properties from buildManifest</li>
* </ul>
*/
task newrelicVersionedAgentJar(type: Jar) {
group("build")
dependsOn("transformedShadowJar", "buildManifest")
dependsOn(instrumentProjects().collect { it.tasks["jar"] })
from("$rootDir/LICENSE")
from("$rootDir/THIRD_PARTY_NOTICES.md")
includeEmptyDirs = false
// This captures jars that are added to the bootstrap classpath.
// All projects have the same version, so we can safely remove this version from the name.
from(project.configurations.jarIntoJar) {
rename("-${project.version}", "")
rename("-${securityAgentVersion}", "")
}
from(zipTree(project.tasks['transformedShadowJar'].archiveFile.get().asFile.path))
from(project.tasks["buildManifest"].outputs.getFiles())
into('instrumentation') {
from {
instrumentProjects().collect { it.tasks['jar'].archiveFile.get().asFile.path }
}
}
manifest {
attributes 'Implementation-Title': 'New Relic Java Agent',
'Implementation-Version': project.version,
'Created-By': 'New Relic, Inc',
'Built-By': System.getProperty('user.name'),
'Built-Date': project.version.toString().contains("DEV") ? "DEV" : new Date(),
'Can-Redefine-Classes': true,
'Can-Retransform-Classes': true,
'Specification-Version': project.version,
'Build-Id': System.getProperty('BUILD_ID') ?: "None",
'Multi-Release': true,
'Agent-Class': 'com.newrelic.bootstrap.BootstrapAgent',
'Premain-Class': 'com.newrelic.bootstrap.BootstrapAgent',
'Main-Class': 'com.newrelic.bootstrap.BootstrapAgent',
'X-Git-SHA': System.getenv("GITHUB_SHA") ?: GitUtil.sha(project) ?: "UNKNOWN"
}
}
task newrelicJar(dependsOn: newrelicVersionedAgentJar, type: Copy) {
from((tasks.getByName("newrelicVersionedAgentJar") as Jar).archiveFile)
into("$buildDir/newrelicJar")
rename(".*", "newrelic.jar")
}
PublishConfig.config(
project,
"New Relic Java Agent",
"The New Relic Java agent for full-stack observability") { it ->
it.artifact(tasks.newrelicVersionedAgentJar)
it.artifact(tasks.sourcesJar)
it.artifact(tasks.javadocJar)
}
// because of the complex shadowing requirements, the auto-generated `jar` task is not useful.
jar {
enabled = false
}
test {
forkEvery = 1
maxParallelForks = Runtime.runtime.availableProcessors()
useJUnit {
if (project.hasProperty("forkedTests")) {
includeCategories("com.newrelic.test.marker.RequiresFork")
} else if (project.hasProperty("nonForkedTests")) {
forkEvery = 10
excludeCategories("com.newrelic.test.marker.RequiresFork")
}
}
minHeapSize = "256m"
maxHeapSize = "768m"
if (System.getProperty('DEBUG', 'false') == 'true') {
jvmArgs '-Xdebug',
'-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9999',
'-Dnewrelic.config.file=${projectDir}/src/test/resources/com/newrelic/agent/config/newrelic.yml'
} else {
jvmArgs "-Dnewrelic.config.file=${projectDir}/src/test/resources/com/newrelic/agent/config/newrelic.yml"
}
}
task testJar(type: Jar, dependsOn: testClasses) {
getArchiveBaseName().set("test-${project.archivesBaseName}")
from sourceSets.test.output
}
artifacts {
tests testJar
finalArtifact newrelicVersionedAgentJar
archives newrelicVersionedAgentJar
}