/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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
 *
 *    https://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.
 */

compileJava.options.release = javaVersion.toInteger()

extensions.configure(JavaPluginExtension) {
    // Explicit `it` is required here
    it.withJavadocJar()
    it.withSourcesJar()
}

tasks.withType(Javadoc).configureEach { Javadoc it ->
    it.options.noTimestamp true // prevent the file header with the date
    it.options.bottom "Generated ${formattedBuildDate} (UTC)"
}

// JavaCompile is not configured because we put java files inside of the groovy source sets

tasks.withType(GroovyCompile).configureEach {
    groovyOptions.encoding = 'UTF-8' // encoding needs to be the same since it's different across platforms
    // Preserve method parameter names in Groovy/Java classes for IDE parameter hints & bean reflection metadata.
    groovyOptions.parameters = true
    options.encoding = 'UTF-8' // encoding needs to be the same since it's different across platforms
    options.fork = true
    options.forkOptions.jvmArgs = ['-Xms128M', '-Xmx2G']
}

// Grails determines the grails version via the META-INF/MANIFEST.MF file
// Note: we exclude attributes such as Built-By, Build-Jdk, Created-By to ensure the build is reproducible.
tasks.withType(Jar).configureEach {
    if (project.findProperty('skipJavaComponent')) {
        it.enabled = false
        return
    }

    manifest.attributes(
            'Implementation-Title': 'Apache Grails',
            'Implementation-Version': grailsVersion,
            'Implementation-Vendor': 'grails.apache.org'
    )
}

// Any jar, zip, or archive should be reproducible
// No longer needed after https://github.com/gradle/gradle/issues/30871
tasks.withType(AbstractArchiveTask).configureEach {
    preserveFileTimestamps = false // to prevent timestamp mismatches
    reproducibleFileOrder = true // to keep the same ordering
    // to avoid platform specific defaults, set the permissions consistently
    filePermissions { permissions ->
        permissions.unix(0644)
    }
    dirPermissions { permissions ->
        permissions.unix(0755)
    }
}

