xref: /aosp_15_r20/external/dagger2/tools/shader/build.gradle (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1/*
2 * Copyright (C) 2020 The Dagger Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import java.util.jar.Manifest;
18import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer
19import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext
20import shadow.org.apache.tools.zip.ZipOutputStream
21import shadow.org.apache.tools.zip.ZipEntry
22
23plugins {
24  id 'java-library'
25  // Use shadow version >= 7.1.1 to get log4j vulnerability patches:
26  //   https://github.com/johnrengelman/shadow/releases/tag/7.1.1
27  id 'com.github.johnrengelman.shadow' version '7.1.1'
28}
29
30repositories {
31  mavenCentral()
32  mavenLocal()
33}
34
35java {
36    // This can be Java 8 because this is non-Android and so isn't required to
37    // be Java 11 by AGP.
38    sourceCompatibility = JavaVersion.VERSION_1_8
39}
40
41// This transformation makes sure the input jar's MANIFEST.MF properties,
42// such as "Created-by:" get merged into the final artifact jar (although it's
43// not clear how necessary this is).
44/** A transform that merges in the MANIFEST.MF files from the inputJar.  */
45class ManifestMerger implements Transformer {
46    private static final String MANIFEST_MF = "META-INF/MANIFEST.MF";
47
48    private Manifest manifest;
49
50    @Override
51    boolean canTransformResource(FileTreeElement element) {
52        return MANIFEST_MF.equalsIgnoreCase(element.relativePath.pathString);
53    }
54
55    @Override
56    void transform(TransformerContext context) {
57        if (manifest == null) {
58            manifest = new Manifest(context.is);
59        } else {
60            Manifest toMerge = new Manifest(context.is);
61            manifest.getMainAttributes()
62                .putAll(toMerge.getMainAttributes().entrySet());
63            manifest.getEntries().putAll(toMerge.getEntries().entrySet());
64        }
65    }
66
67    @Override
68    boolean hasTransformedResource() { true }
69
70    @Override
71    void modifyOutputStream(
72            ZipOutputStream os, boolean preserveFileTimestamps) {
73        os.putNextEntry(new ZipEntry(MANIFEST_MF));
74        if (manifest != null) {
75            ByteArrayOutputStream content = new ByteArrayOutputStream();
76            manifest.write(content);
77            os.write(content.toByteArray());
78        }
79    }
80}
81
82configurations {
83    shaded
84}
85
86shadowJar {
87    // This transform is needed so that services get relocated/shaded as well.
88    mergeServiceFiles()
89
90    archiveClassifier = "" // postfix for output jar
91    configurations = [project.configurations.shaded]
92    transform(ManifestMerger.class)
93
94    // Add a 'relocate' declaration for each shaded rule.
95    //   Format: "key1,value1;key2,value2;key3,value3"
96    def rules = project.getProperty("shadedRules").split(";")
97    for (def i = 0; i < rules.size(); i++) {
98        def rule = rules[i].split(",")
99        relocate "${rule[0]}", "${rule[1]}"
100    }
101}
102
103dependencies {
104    shaded files(project.getProperty("inputJar"))
105}
106