plugins { id 'java-library' id 'maven-publish' id 'eclipse' id 'signing' } group = 'org.stianloader' version = '0.0.1' base { archivesName = 'mrj-mania' } java { sourceCompatibility = targetCompatibility = '1.8' } repositories { mavenLocal() mavenCentral() } dependencies { // Source: https://mvnrepository.com/artifact/org.jetbrains/annotations compileOnlyApi 'org.jetbrains:annotations:26.1.0' } task sourcesJar(type: Jar, dependsOn: classes) { archiveClassifier = 'sources' group = 'build' description = 'Assemble sources jar.' into("META-INF/LICENSES/${base.archivesName.get()}") { from project.rootProject.file('LICENSE') } } eclipse { classpath { containers 'org.eclipse.buildship.core.gradleclasspathcontainer' file.whenMerged { entries.each { if (it.kind == 'con' || it.kind == 'lib') { it.entryAttributes['module'] = 'true' } } } } } sourceSets.removeAll() // remove default sourceset def int[] releaseVersions = new int[] {8, 9, 11, 16, 21} // Important: Must be sorted // Introducing: warcrimes against gradle. for (int sourceSetVersionUnsafe : releaseVersions) { def sourceSetVersion = sourceSetVersionUnsafe // Clone variable because otherwise strange things happen with closures sourceSets.register("java${sourceSetVersion}") { java { for (int otherRelease : releaseVersions) { if (otherRelease < sourceSetVersion) { compileClasspath += sourceSets["java${otherRelease}"].output // TODO somehow handle dependencies as well } } java.setSrcDirs(new java.util.ArrayList()) java.srcDir(getProjectDir().toPath().resolve("src/main/java${sourceSetVersion}")) java.filter.exclude('module-info.java') } } if (sourceSetVersion >= 9) { sourceSets.register("java${sourceSetVersion}module") { java { java.srcDirs = new java.util.ArrayList() java.filter.include('module-info.java') // not exactly right, but probably the mest thing we have at this point } } tasks.register("flattenJ${sourceSetVersion}jpms", ProcessResources.class) { destinationDir = layout.buildDirectory.dir("generated/jpms/flattened${sourceSetVersion}java").get().asFile sourceSets["java${sourceSetVersion}module"].java.srcDir(outputs) into(destinationDir) for (int i = releaseVersions.length - 1; i >= 0; i--) { if (releaseVersions[i] <= sourceSetVersion) { from(getProjectDir().toPath().resolve("src/main/java${releaseVersions[i]}")) } } duplicatesStrategy = DuplicatesStrategy.EXCLUDE } } java { toolchain { languageVersion = JavaLanguageVersion.of(sourceSetVersion) } } tasks[sourceSets["java${sourceSetVersion}"].getCompileJavaTaskName()].configure { javaCompiler = javaToolchains.compilerFor { languageVersion = JavaLanguageVersion.of(sourceSetVersion) } if (sourceSetVersion <= 8) { targetCompatibility = sourceCompatibility = "1.${sourceSetVersion}" } else { // https://github.com/gradle/gradle/issues/15267 - WONTFIX issue if (sourceSetVersion == 9) { targetCompatibility = sourceCompatibility = "${sourceSetVersion}" } else { options.release = sourceSetVersion } modularity.inferModulePath.set(false) } } if (sourceSetVersion >= 9) { tasks[sourceSets["java${sourceSetVersion}module"].getCompileJavaTaskName()].configure { javaCompiler = javaToolchains.compilerFor { languageVersion = JavaLanguageVersion.of(sourceSetVersion) } // https://github.com/gradle/gradle/issues/15267 - WONTFIX issue if (sourceSetVersion == 9) { targetCompatibility = sourceCompatibility = "${sourceSetVersion}" } else { options.release = sourceSetVersion } modularity.inferModulePath.set(true) dependsOn(tasks["flattenJ${sourceSetVersion}jpms"]) } } jar { dependsOn tasks[sourceSets["java${sourceSetVersion}"].getCompileJavaTaskName()] if (sourceSetVersion >= 9) { into("META-INF/versions/${sourceSetVersion}") { from sourceSets["java${sourceSetVersion}"].output } dependsOn tasks[sourceSets["java${sourceSetVersion}module"].getCompileJavaTaskName()] into("META-INF/versions/${sourceSetVersion}") { from sourceSets["java${sourceSetVersion}module"].output include 'module-info.class' } } else { from sourceSets["java${sourceSetVersion}"].output } } sourcesJar { if (sourceSetVersion >= 9) { into("META-INF/versions/${sourceSetVersion}") { from sourceSets["java${sourceSetVersion}"].allSource } } else { from sourceSets["java${sourceSetVersion}"].allSource } } eclipse.classpath.file.whenMerged { entries.each { if (it.kind == 'src' && it.path == "src/main/java${sourceSetVersion}") { it.entryAttributes.put("release", sourceSetVersion) } } entries.removeIf { it.path.contains('generated/jpms/flattened') } } configurations[sourceSets["java${sourceSetVersion}"].compileClasspathConfigurationName].extendsFrom(configurations['compileOnlyApi']) if (sourceSetVersion >= 9) { configurations[sourceSets["java${sourceSetVersion}module"].compileClasspathConfigurationName].extendsFrom(configurations['compileOnlyApi']) } } jar { into("META-INF/LICENSES/${base.archivesName.get()}") { from project.rootProject.file('LICENSE') } manifest { attributes 'Multi-Release': true } } tasks.register(sourceSets['java8'].javadocTaskName, Javadoc) { source = sourceSets['java8'].allJava classpath = sourceSets['java8'].compileClasspath } tasks.register(sourceSets['java8'].javadocJarTaskName, Jar) { archiveClassifier = 'javadoc' from tasks[sourceSets['java8'].javadocTaskName].destinationDir dependsOn tasks[sourceSets['java8'].javadocTaskName] } publishing { publications { maven(MavenPublication) { groupId = project.group artifactId = base.archivesName.get() from components['java'] artifact sourcesJar artifact tasks[sourceSets['java8'].javadocJarTaskName] pom { name = 'MRJ Mania' description = "Java polyfills powered by MRJ (multi-release-jars)." url = 'https://codeberg.org/stianloader/mrj-mania' inceptionYear = '2026' licenses { license { name = 'BSD-3-Clause' url = 'https://opensource.org/license/bsd-3-clause/' distribution = 'repo' } } developers { developer { id = 'geolykt' name = 'Emeric "Geolykt" Werner' email = 'emeric.werner@geolykt.de' timezone = 'Europe/Berlin' url = 'https://geolykt.de/' } } scm { connection = 'scm:git:git://codeberg.org/stianloader/mrj-mania.git' developerConnection = 'scm:git:ssh://git@codeberg.org/stianloader/mrj-mania.git' url = 'https://codeberg.org/stianloader/mrj-mania' } issueManagement { system = 'Codeberg' url = 'https://codeberg.org/stianloader/mrj-mania/issues' } } } } repositories { if (providers.gradleProperty('publishRepo').isPresent()) { System.out.println("Publishing to " + providers.gradleProperty('publishRepo').get()) maven { url providers.gradleProperty('publishRepo').get() allowInsecureProtocol = true } } else { System.out.println("Publishing to the local maven repository") mavenLocal() } } } if (providers.gradleProperty('signedPublish').isPresent() && Boolean.parseBoolean(providers.gradleProperty('signedPublish').get())) { signing { useGpgCmd() sign publishing.publications.maven } }