/
MinifyPlugin.kt
86 lines (78 loc) · 3.45 KB
/
MinifyPlugin.kt
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
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.gradlebuild.packaging
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.gradlebuild.packaging.Attributes.artifactType
import org.gradle.gradlebuild.packaging.Attributes.minified
import org.gradle.kotlin.dsl.*
/**
* A map from artifact name to a set of class name prefixes that should be kept.
* Artifacts matched by this map will be minified to only contain the specified
* classes and the classes they depend on. The classes are not relocated, they all
* remain in their original namespace. This reduces the final Gradle distribution
* size and makes us more conscious of which parts of a library we really need.
*/
open class MinifyPlugin : Plugin<Project> {
override fun apply(project: Project): Unit = project.run {
val keepPatterns = mapOf(
"fastutil" to setOf(
"it.unimi.dsi.fastutil.ints.IntOpenHashSet",
"it.unimi.dsi.fastutil.ints.IntRBTreeSet",
"it.unimi.dsi.fastutil.ints.IntSets"
)
)
allprojects {
plugins.withId("java-base") {
dependencies {
attributesSchema {
attribute(minified)
}
artifactTypes.getByName("jar") {
attributes.attribute(minified, java.lang.Boolean.FALSE)
}
registerTransform(Minify::class) {
/*
* TODO Why do I have to add artifactType=jar here? According to
* the declaration above, it's the only artifact type for which
* minified=false anyway. If I don't add this, the transform chain
* in binary-compatibility.gradle no longer works.
*/
from.attribute(minified, false).attribute(artifactType, "jar")
to.attribute(minified, true).attribute(artifactType, "jar")
parameters {
keepClassesByArtifact = keepPatterns
}
}
}
configurations.all {
/*
* Some of our projects still depend on matching the default
* configuration. As soon as any attribute is added, the default
* configuration is no longer a valid match. To work around this
* we only add the "minified" attribute in places where we already
* use other attributes.
*/
afterEvaluate {
if (!attributes.isEmpty) {
attributes.attribute(minified, true)
}
}
}
}
}
}
}