subprojects {
    apply plugin: 'groovy'
    apply plugin: 'java-library'

    dependencies {
        api "org.codehaus.groovy:groovy:${groovyVersion}"
        api "org.codehaus.groovy:groovy-jsr223:${groovyVersion}"
        api "org.codehaus.groovy:groovy-json:${groovyVersion}"
        api "com.h2database:h2:1.4.200"
        api "org.codehaus.groovy:groovy-sql:${groovyVersion}"
    }

    compileGroovy {
        groovyOptions.optimizationOptions.indy = false
        sourceCompatibility = project.sourceCompatibility
        targetCompatibility = project.targetCompatibility
        options.compilerArgs += project.compilerArgs
        options.deprecation = true
        options.encoding = 'UTF-8'
    }

    compileJava {
        sourceCompatibility = project.sourceCompatibility
        targetCompatibility = project.targetCompatibility
        options.compilerArgs += project.compilerArgs
        options.deprecation = true
        options.encoding = 'UTF-8'
    }

    repositories {
        mavenCentral()
    }

    tasks.withType(AbstractArchiveTask) {
        preserveFileTimestamps = false
        reproducibleFileOrder = true
        doLast {
            stripJar(it.archivePath)
        }
    }
    
    tasks.withType(Jar) {
        metadataCharset = "US-ASCII"
    }
}
import java.util.jar.*
import java.nio.file.*
void stripJar(File file) {
    if (file.getName().endsWith('.tar'))
        return
    println "stripping $file"
    File newFile = new File(file.parent, 'tmp-' + file.name)
    newFile.withOutputStream { fout ->
        JarOutputStream out = new JarOutputStream(fout)
        JarFile jf = new JarFile(file)
        jf.entries().unique {it.name}.sort {it.name}.each {
            def copy = new JarEntry(it.name)
            copy.time = 1001
            out.putNextEntry(copy)
            out << jf.getInputStream(it)
        }
        out.finish()
        jf.close()
    }
    Files.copy(newFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING)
    newFile.delete()
}

