From 81ac90feb4bbe458f6dcb706e0851888f9f762fd Mon Sep 17 00:00:00 2001 From: James Seibel Date: Thu, 29 Jun 2023 20:52:30 -0500 Subject: [PATCH] Add a build.gradle for API building with source files --- api/build.gradle | 79 ++++++++++++++++++++++++++ api/src/main/resources/fabric.mod.json | 9 +++ 2 files changed, 88 insertions(+) create mode 100644 api/build.gradle create mode 100644 api/src/main/resources/fabric.mod.json diff --git a/api/build.gradle b/api/build.gradle new file mode 100644 index 000000000..cb3195792 --- /dev/null +++ b/api/build.gradle @@ -0,0 +1,79 @@ +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar + +plugins { + id "java" + + id "com.github.johnrengelman.shadow" version '7.1.2' apply false +} + + +shadowJar { + // required for basic shadowJar setup + configurations = [project.configurations.shadow] +} + +task addSourcesToCompiledJar(type: ShadowJar) { + + def sourceJarPath = "build/libs/DistantHorizons-api-${rootProject.versionStr}-sources.jar" + def secondJarFile = file(sourceJarPath) + + // doFirst is so these only run when the task is actually executed + doFirst { + System.out.println("Adding source files from: \n" + + "[" + sourceJarPath + "] to compiled API jar: \n" + + "[" + shadowJar.archivePath + "]") + + // Validate the input JAR file + if (!secondJarFile.exists()) { + throw new GradleException("Second JAR file not found: [${secondJarFile}]") + } + } + + // Set the name of the combined JAR file + archiveFileName.set("DistantHorizonsApi-${rootProject.api_version}.jar") + + // Set the destination directory for the combined JAR file + destinationDirectory = file('build/libs/merged/') + + // Set the input JAR files to be combined + from sourceSets.main.allJava + from { + configurations.shadow.collect { it.isDirectory() ? it : zipTree(it) } + } + + // set the jars to merge + from shadowJar.archivePath + from secondJarFile + + // alternative method to Include the source files in the combined JAR + // and/or see which files are being included + eachFile { file -> + // can be set to true for debugging + def useAlternateEmbedMethod = false + def showFileEmbedding = false + + if (showFileEmbedding || useAlternateEmbedMethod) { + System.out.println("attempting to add file: " + file) + + def relativePath = file.relativePath.pathString + if (relativePath.endsWith('.class')) { + System.out.println("class file: " + relativePath) + def sourceFile = file.file.parentFile.resolveSibling('src/main/java/' + relativePath.replace('.class', '.java')) + if (sourceFile.exists()) { + System.out.println("adding source file: " + sourceFile) + if (useAlternateEmbedMethod) { + file.withInputStream { inputStream -> + copy { + from(inputStream) + into(relativePath.replace('.class', '.java')) + } + } + } + } else { + System.out.println('missing source file: ' + sourceFile) + } + } + } + } + +} diff --git a/api/src/main/resources/fabric.mod.json b/api/src/main/resources/fabric.mod.json new file mode 100644 index 000000000..58f9fa39d --- /dev/null +++ b/api/src/main/resources/fabric.mod.json @@ -0,0 +1,9 @@ +{ + "schemaVersion": 1, + "id": "NA", + "version": "", + + "description": "This is just a placeholder to make uploading to Modrinth happy. Don't try running this jar in Minecraft." + +} +