forked from gradle/gradle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
137 lines (123 loc) · 4.63 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
import java.util.jar.JarOutputStream
import java.util.zip.ZipEntry
import java.util.zip.ZipFile
plugins {
id('java-library')
}
// tag::artifact-transform-minify[]
abstract class Minify implements TransformAction<Parameters> { // <1>
interface Parameters extends TransformParameters { // <2>
@Input
Map<String, Set<String>> getKeepClassesByArtifact()
void setKeepClassesByArtifact(Map<String, Set<String>> keepClasses)
// end::artifact-transform-minify[]
@Input
Long getTimestamp()
void setTimestamp(Long timestamp)
// tag::artifact-transform-minify[]
}
@PathSensitive(PathSensitivity.NAME_ONLY)
@InputArtifact
abstract Provider<FileSystemLocation> getInputArtifact()
@Override
void transform(TransformOutputs outputs) {
def fileName = inputArtifact.get().asFile.name
for (entry in parameters.keepClassesByArtifact) { // <3>
if (fileName.startsWith(entry.key)) {
def nameWithoutExtension = fileName.substring(0, fileName.length() - 4)
minify(inputArtifact.get().asFile, entry.value, outputs.file("${nameWithoutExtension}-min.jar"))
return
}
}
println "Nothing to minify - using ${fileName} unchanged"
outputs.file(inputArtifact) // <4>
}
private void minify(File artifact, Set<String> keepClasses, File jarFile) {
println "Minifying ${artifact.name}"
// Implementation ...
// end::artifact-transform-minify[]
new JarOutputStream (new BufferedOutputStream (new FileOutputStream (jarFile))).withCloseable { jarOutputStream ->
new ZipFile (artifact).withCloseable { zip ->
for (entry in zip.entries()) {
if (entry.directory) {
jarOutputStream.putNextEntry(new ZipEntry (entry.name))
jarOutputStream.closeEntry()
} else if (entry.name.endsWith(".class")) {
def className = entry . name . replace ("/", ".").substring(0, entry.name.length()-6)
if (keepClasses.contains(className)) {
addEntry(jarOutputStream, entry, zip)
}
} else {
addEntry(jarOutputStream, entry, zip)
}
}
}
}
}
protected void addEntry(JarOutputStream jarOutputStream, ZipEntry entry, ZipFile zip) {
jarOutputStream.putNextEntry(new ZipEntry (entry.name))
zip.getInputStream(entry).withCloseable { jarOutputStream << it }
jarOutputStream.closeEntry()
// tag::artifact-transform-minify[]
}
}
// end::artifact-transform-minify[]
// tag::artifact-transform-registration[]
// tag::artifact-transform-attribute-setup[]
def artifactType = Attribute.of('artifactType', String)
def minified = Attribute.of('minified', Boolean)
// end::artifact-transform-attribute-setup[]
def keepPatterns = [
"guava": [
"com.google.common.base.Optional",
"com.google.common.base.AbstractIterator"
] as Set
]
// end::artifact-transform-registration[]
// tag::artifact-transform-attribute-setup[]
dependencies {
attributesSchema {
attribute(minified) // <1>
}
artifactTypes.getByName("jar") {
attributes.attribute(minified, false) // <2>
}
}
configurations.all {
afterEvaluate {
if (canBeResolved) {
attributes.attribute(minified, true) // <3>
}
}
}
// tag::artifact-transform-registration[]
dependencies {
registerTransform(Minify) {
from.attribute(minified, false).attribute(artifactType, "jar")
to.attribute(minified, true).attribute(artifactType, "jar")
// end::artifact-transform-attribute-setup[]
parameters {
keepClassesByArtifact = keepPatterns
// end::artifact-transform-registration[]
// Make sure the transform executes each time
timestamp = System.nanoTime()
// tag::artifact-transform-registration[]
}
// tag::artifact-transform-attribute-setup[]
}
}
// end::artifact-transform-attribute-setup[]
// end::artifact-transform-registration[]
repositories {
mavenCentral()
}
// tag::artifact-transform-attribute-setup[]
dependencies { // <4>
implementation('com.google.guava:guava:27.1-jre')
implementation(project(':producer'))
}
// end::artifact-transform-attribute-setup[]
tasks.register("resolveRuntimeClasspath", Copy) {
from(configurations.runtimeClasspath)
into(layout.buildDirectory.dir("runtimeClasspath"))
}