buildscript { dependencies.classpath 'org.ow2.asm:asm-tree:9.8' } plugins { id 'java-library' id 'maven-publish' id 'eclipse' id 'signing' } group = 'org.stianloader' def archivesBaseName = 'micromixin-transformer' version = '0.8.0' repositories { mavenLocal() mavenCentral() } dependencies { api 'org.ow2.asm:asm:9.8' api 'org.ow2.asm:asm-commons:9.8' api 'org.ow2.asm:asm-tree:9.8' api 'org.ow2.asm:asm-util:9.8' api 'org.json:json:20230618' // Note: It is likely that one cannot update this dependency without breaking compilation. Handle with care. compileOnlyApi 'org.jetbrains:annotations-java5:24.1.0' // junit 4 is the last version of junit that I am aware of that still works with Java 6 testImplementation 'junit:junit:4.13.2' } sourceSets { java9 { java { compileClasspath += main.compileClasspath compileClasspath += main.output java.setSrcDirs(new java.util.ArrayList()) java.srcDir(getProjectDir().toPath().resolve('src/main/java9')) java.filter.exclude("module-info.java") } } java9module { java { compileClasspath += main.compileClasspath java.setSrcDirs(new java.util.ArrayList()) var rootSourceDir = getProjectDir().toPath().resolve('src/main/java9'); java.srcDir(rootSourceDir) java.srcDirs(main.java.srcDirs()) main.java.srcDirs().each { dir -> { // For whatever reason 'dir' represents source files and not source directories at this point // Can I complain? No, not really... String path = dir.toString().substring(dir.toString().indexOf("java/") + 5) if (java.nio.file.Files.exists(rootSourceDir.resolve(path))) { java.filter.exclude(path) } } } java.filter.include("module-info.java") } } } java { toolchain { languageVersion = JavaLanguageVersion.of(6) } toolchain { languageVersion = JavaLanguageVersion.of(11) } } compileJava { javaCompiler = javaToolchains.compilerFor { languageVersion = JavaLanguageVersion.of(6) } sourceCompatibility = '1.6' targetCompatibility = '1.6' } compileJava9Java { javaCompiler = javaToolchains.compilerFor { languageVersion = JavaLanguageVersion.of(11) } dependsOn compileJava sourceCompatibility = '9' targetCompatibility = '9' modularity.inferModulePath.set(false) } compileJava9moduleJava { javaCompiler = javaToolchains.compilerFor { languageVersion = JavaLanguageVersion.of(11) } dependsOn compileJava sourceCompatibility = '9' targetCompatibility = '9' modularity.inferModulePath.set(true) } // Eclipse will very frequently use micromixin-transformer's Objects class instead // of the Objects class provided by the JDK. By deprecating the micromixin-transformer // class before publication, we can ensure that internals are less likely to be used // by dependent artifacts. // As we assume that JPMS is used for J9+ environments, this is not performed for // the Java 9 MRJ variant, although in theory it's still an environment where it is an issue. task virtualDeprecationJ6(type: Copy) { dependsOn compileJava from sourceSets.main.output into 'build/classes/java/virtualdepr6' eachFile { if (it.getPath().equals("org/stianloader/micromixin/transform/internal/util/Objects.class")) { it.exclude() ByteArrayOutputStream baos = new ByteArrayOutputStream(); it.copyTo(baos); org.objectweb.asm.ClassReader reader = new org.objectweb.asm.ClassReader(baos.toByteArray()); org.objectweb.asm.tree.ClassNode node = new org.objectweb.asm.tree.ClassNode(); reader.accept(node, 0); node.access |= org.objectweb.asm.Opcodes.ACC_DEPRECATED; org.objectweb.asm.ClassWriter writer = new org.objectweb.asm.ClassWriter(0); node.accept(writer); java.nio.file.Files.write(destinationDir.toPath().resolve(it.path), writer.toByteArray()); } } } jar { dependsOn compileJava9Java dependsOn compileJava9moduleJava dependsOn test dependsOn virtualDeprecationJ6 // Remove original jar sources - yeah, this is getting a bit cursed and there is probably // a better way out, but I don't know that way; Or do I? eachFile { if (compileJava.destinationDirectory.asFileTree.contains(it.getFile())) { it.exclude() } } from tasks.virtualDeprecationJ6.destinationDir into('META-INF/versions/9') { from sourceSets.java9.output } into('META-INF/versions/9') { from sourceSets.java9module.output include 'module-info.class' } into('META-INF/LICENSES/' + archivesBaseName) { from project.rootProject.file("LICENSE") } manifest { attributes 'Multi-Release': true } } task sourcesJar(type: Jar, dependsOn: classes) { archiveClassifier = 'sources' from sourceSets.main.allSource into('META-INF/versions/9') { from sourceSets.java9.allSource } into('META-INF/LICENSES/' + archivesBaseName) { from project.rootProject.file("LICENSE") } } task javadocJar(type: Jar, dependsOn: javadoc) { archiveClassifier = 'javadoc' from javadoc.destinationDir } publishing { publications { maven(MavenPublication) { groupId project.group artifactId project.archivesBaseName from components['java'] artifact sourcesJar artifact javadocJar pom { name = 'Micromixin Transformer' description = "Lightweight reimplementation of sponge's mixin framework." url = 'https://github.com/stianloader/Micromixin' inceptionYear = '2023' licenses { license { name = 'BSD-2-Clause' url = 'https://opensource.org/license/bsd-2-clause/' distribution = 'repo' } } developers { developer { id = 'geolykt' name = 'Emeric "Geolykt" Werner' email = 'mail@geolykt.de' timezone = 'Europe/Berlin' url = 'https://geolykt.de/' } } scm { connection = 'scm:git:git://github.com/stianloader/Micromixin.git' developerConnection = 'scm:git:ssh://github.com:stianloader/Micromixin.git' url = 'https://github.com/stianloader/Micromixin/tree/main' } issueManagement { system = 'Github' url = 'https://github.com/stianloader/Micromixin/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 } } eclipse { classpath { containers 'org.eclipse.buildship.core.gradleclasspathcontainer' file { whenMerged { entries.each { if (it.kind == 'con' || it.kind == 'lib') { it.entryAttributes['module'] = 'true' } else if (it.kind == 'src' && it.path == 'src/main/java9') { it.setIncludes(java.util.Arrays.asList('module-info.java')) } } } } } }