-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathbuild.gradle
182 lines (155 loc) · 6.56 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
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'net.sf.proguard:proguard-gradle:6.2.2'
}
}
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.6.21'
id 'com.github.johnrengelman.shadow' version '7.1.2'
}
group 'tech.httptoolkit'
version '1.3.7'
repositories {
mavenCentral()
maven {
url "https://maven.google.com/"
}
}
configurations {
r8
}
dependencies {
implementation group: 'net.bytebuddy', name: 'byte-buddy-dep', version: '1.14.17'
// byte buddy contains references to jna classes and without them the r8Jar step fails
compileOnly group: 'net.java.dev.jna', name: 'jna', version: '5.8.0'
// Dependencies we load only as part of rewriting them, iff the target app includes them:
compileOnly group: 'commons-httpclient', name: 'commons-httpclient', version: '3.1'
compileOnly group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5'
compileOnly group: 'org.apache.httpcomponents.client5', name: 'httpclient5', version: '5.0.3'
compileOnly group: 'org.eclipse.jetty', name: 'jetty-client', version: '11.0.1'
compileOnly group: 'org.asynchttpclient', name: 'async-http-client', version: '2.12.2'
compileOnly group: 'io.projectreactor.netty', name: 'reactor-netty', version: '1.0.4'
compileOnly group: 'io.ktor', name: 'ktor-client-core', version: '1.5.2'
compileOnly group: 'io.ktor', name: 'ktor-client-cio', version: '1.5.2'
compileOnly group: 'com.typesafe.akka', name: 'akka-http-core_2.13', version: '10.2.4'
compileOnly group: 'com.typesafe.akka', name: 'akka-actor_2.13', version: '2.6.13'
compileOnly group: 'io.vertx', name: 'vertx-core', version: '4.2.2'
// Test deps:
testImplementation group: 'io.kotest', name: 'kotest-runner-junit5-jvm', version: '4.4.0'
testImplementation group: 'io.kotest', name: 'kotest-assertions-core-jvm', version: '4.4.0'
testImplementation "com.github.tomakehurst:wiremock-jre8:2.27.2"
// Only used during the R8 build task
r8 group: 'com.android.tools', name: 'r8', version: '2.1.75'
}
compileJava {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
tasks.withType(Jar) {
manifest {
attributes 'Premain-Class': 'tech.httptoolkit.javaagent.HttpProxyAgent'
attributes 'Agent-Class': 'tech.httptoolkit.javaagent.HttpProxyAgent'
attributes 'Main-Class': 'tech.httptoolkit.javaagent.AttachMain'
attributes 'Can-Redefine-Classes': 'true'
attributes 'Can-Retransform-Classes': 'true'
}
}
// First, we bundle everything into a workable standalone JAR, with all runtime source included plus
// dependencies plus agent metadata:
shadowJar {
minimize()
exclude '**/*.kotlin_metadata'
exclude '**/*.kotlin_module'
exclude '**/*.kotlin_builtins'
exclude '**/module_info.class'
exclude 'META-INF/maven/**'
// We have to specifically exclude our reactor stub code here, because we don't want to the type
// stubs that we've manually defined in our own source included here.
exclude 'reactor/'
}
// As part of bundling the JAR, we relocate all dependencies into our namespace:
import com.github.jengelman.gradle.plugins.shadow.tasks.ConfigureShadowRelocation
task relocateShadowJar(type: ConfigureShadowRelocation) {
target = tasks.shadowJar
prefix = "tech.httptoolkit.relocated"
}
tasks.shadowJar.dependsOn tasks.relocateShadowJar
// Then we take this bundled JAR and optimize it. This shrinks it dramatically, but also breaks it, because
// bytebuddy depends on some of our source being unmodified by R8 (frames in advice classes get messed with).
def r8File = new File("$buildDir/libs/$archivesBaseName-r8.jar")
tasks.register('r8Jar', JavaExec) { task ->
def rules = file('r8-rules.txt')
task.dependsOn(tasks.shadowJar)
task.outputs.file(r8File)
task.inputs.files shadowJar.getArchiveFile()
task.classpath(configurations.r8)
task.main = 'com.android.tools.r8.R8'
task.args = [
'--release',
'--classfile',
'--output', r8File.toString(),
'--pg-conf', rules.toString()
] + (configurations.compileClasspath.filter { path ->
// Include libs for a few runtime-only deps, so R8 can resolve them during optimization:
path.getName().startsWith("jna-") ||
path.getName().startsWith("jetty-util-") ||
path.getName().startsWith("commons-httpclient-") ||
path.getName().startsWith("async-http-client-")
}.collect {path ->
['--lib', path.toString()]
}.flatten())
doFirst {
def java8Home = System.getenv("JAVA_HOME_8_X64")
if (java8Home == null || java8Home.empty) {
throw new GradleException("\$JAVA_HOME_8_X64 must be set to build a minified distributable")
} else {
// AFAICT R8 only supports the Java 8 lib files. We require that to be available, configured by env
task.args += "--lib"
task.args += java8Home
}
task.args += shadowJar.getArchiveFile().get().asFile.toString()
}
}
// Then we fix this, by taking the raw advice classes for our own source from the original bundled JAR (i.e. including
// any relocated references) and combining that with the minified & optimized dependencies from R8, to get a single
// bundled and 99% optimized JAR.
task distJar(type: Jar) {
dependsOn(tasks.shadowJar, tasks.r8Jar)
archiveClassifier = 'dist'
// Pull raw advice classes from the shadow JAR, unminified:
from (zipTree(shadowJar.getArchiveFile())) {
include "tech/httptoolkit/javaagent/advice/**/*"
include "tech/httptoolkit/relocated/net/bytebuddy/agent/builder/**/*"
}
// Pull other source & bundled dependencies in their minified form, from R8:
from (zipTree(r8Jar.outputs.files[0])) {
exclude "tech/httptoolkit/javaagent/advice/**/*"
exclude "tech/httptoolkit/relocated/net/bytebuddy/agent/builder/**/*"
}
}
tasks.withType(Test) {
// We need to build both JARs before the integration tests can run
dependsOn('shadowJar')
dependsOn(':test-app:shadowJar')
useJUnitPlatform()
outputs.upToDateWhen {false}
testLogging {
events "STARTED", "PASSED", "FAILED", "SKIPPED", "STANDARD_OUT", "STANDARD_ERROR"
}
}
task quickTest(type: Test) {
environment 'TEST_JAR', tasks.shadowJar.getArchiveFile().get().asFile.toString()
}
task distTest(type: Test) {
environment 'TEST_JAR', tasks.distJar.getArchiveFile().get().asFile.toString()
dependsOn('distJar')
}