xref: /aosp_15_r20/external/jspecify/gradle/mrjar.gradle (revision 2167191df2fa07300797f1ac5b707370b5f38c48)
1/*
2 * Copyright 2021 The JSpecify 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
17/**
18 * This build configuration configures project to necessary files to support JEP 238: Multi-Release JAR Files.
19 * https://openjdk.java.net/jeps/238
20 *
21 * It puts class files built for Java 9+ into META-INF/versions/9 directory.
22 * It also create a manifest file with Multi-Release attribute.
23 */
24
25sourceSets {
26    // The source set for classes stored in META-INF/versions/9 directory
27    java9 {
28        java {
29            srcDirs = [
30                'src/java9/java',
31                /*
32                 * We compile the main classes again: If we don't, then this
33                 * javac invocation contains only a module-info, so javac fails
34                 * because we're creating an empty module.
35                 *
36                 * But we won't include the main .class files when we place the
37                 * output module-info.class under META-INF/versions/9, thanks
38                 * to some more configuration below.
39                 */
40                'src/main/java'
41            ]
42        }
43    }
44}
45
46tasks.named('compileJava9Java', JavaCompile).configure {
47    options.release = 9
48}
49
50jar {
51    into('META-INF/versions/9') {
52        from(sourceSets.java9.output) {
53            /*
54             * As discussed above, we compiled all the classes again. But all
55             * that we want to include under META-INF/versions/9 is the
56             * module-info.
57             */
58            include 'module-info.class'
59        }
60    }
61    manifest {
62        attributes 'Multi-Release': true
63    }
64}
65