/*
 * Copyright 2021 The JSpecify 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.
 */

/**
 * This build configuration configures project to necessary files to support JEP 238: Multi-Release JAR Files.
 * https://openjdk.java.net/jeps/238
 *
 * It puts class files built for Java 9+ into META-INF/versions/9 directory.
 * It also create a manifest file with Multi-Release attribute.
 */

sourceSets {
    // The source set for classes stored in META-INF/versions/9 directory
    java9 {
        java {
            srcDirs = [
                'src/java9/java',
                /*
                 * We compile the main classes again: If we don't, then this
                 * javac invocation contains only a module-info, so javac fails
                 * because we're creating an empty module.
                 *
                 * But we won't include the main .class files when we place the
                 * output module-info.class under META-INF/versions/9, thanks
                 * to some more configuration below.
                 */
                'src/main/java'
            ]
        }
    }
}

tasks.named('compileJava9Java', JavaCompile).configure {
    options.release = 9
}

jar {
    into('META-INF/versions/9') {
        from(sourceSets.java9.output) {
            /*
             * As discussed above, we compiled all the classes again. But all
             * that we want to include under META-INF/versions/9 is the
             * module-info.
             */
            include 'module-info.class'
        }
    }
    manifest {
        attributes 'Multi-Release': true
    }
}
