From 4eb2a55a43b5176605162d0397e19e400131178b Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Thu, 11 Jul 2024 02:04:50 +0200 Subject: [PATCH] Added a bit of test --- .../neoforged/moddevgradle/tasks/JarJar.java | 14 ++++++++++++-- testproject/jijtest/build.gradle | 19 +++++++++++++++++++ .../service/java/jijtestservice/Service.java | 5 +++++ .../service/resources/META-INF/MANIFEST.MF | 3 +++ .../src/test/java/jijtest/CoreModTest.java | 5 +++++ 5 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 testproject/jijtest/src/service/java/jijtestservice/Service.java create mode 100644 testproject/jijtest/src/service/resources/META-INF/MANIFEST.MF diff --git a/src/main/java/net/neoforged/moddevgradle/tasks/JarJar.java b/src/main/java/net/neoforged/moddevgradle/tasks/JarJar.java index 02d3dfc7..ee0c91da 100644 --- a/src/main/java/net/neoforged/moddevgradle/tasks/JarJar.java +++ b/src/main/java/net/neoforged/moddevgradle/tasks/JarJar.java @@ -12,6 +12,7 @@ import org.gradle.api.file.FileSystemOperations; import org.gradle.api.model.ObjectFactory; import org.gradle.api.tasks.InputFiles; +import org.gradle.api.tasks.Internal; import org.gradle.api.tasks.Nested; import org.gradle.api.tasks.OutputDirectory; import org.gradle.api.tasks.SkipWhenEmpty; @@ -56,12 +57,16 @@ public abstract class JarJar extends DefaultTask { @OutputDirectory public abstract DirectoryProperty getOutputDirectory(); + @Internal + public abstract DirectoryProperty getBuildDirectory(); + private final FileSystemOperations fileSystemOperations; @Inject public JarJar(FileSystemOperations fileSystemOperations) { this.fileSystemOperations = fileSystemOperations; this.getOutputDirectory().convention(getProject().getLayout().getBuildDirectory().dir("generated/" + getName())); + this.getBuildDirectory().convention(getProject().getLayout().getBuildDirectory()); } @TaskAction @@ -69,14 +74,19 @@ protected void run() { List includedJars = new ArrayList<>(getJarJarArtifacts().getResolvedArtifacts().get()); fileSystemOperations.delete(spec -> spec.delete(getOutputDirectory())); + var buildDir = getBuildDirectory().getAsFile().get().toPath(); + var artifactFiles = new ArrayList<>(includedJars.stream().map(ResolvedJarJarArtifact::getFile).toList()); // Now we have to handle pure file collection dependencies that do not have artifact ids for (var file : getInputFiles()) { if (!artifactFiles.contains(file)) { + // This is only intended for libraries built by this project, so this is a shoddy check + if (!file.toPath().startsWith(buildDir)) { + throw new GradleException("Cannot embed file dependencies in the jar if they aren't in this projects build directory."); + } + // Create a hashcode to use as a version var hashCode = hashFile(file); - - // We only support file dependencies that are likely built by this project includedJars.add(new ResolvedJarJarArtifact( file, file.getName(), diff --git a/testproject/jijtest/build.gradle b/testproject/jijtest/build.gradle index 76f457d9..5c19f694 100644 --- a/testproject/jijtest/build.gradle +++ b/testproject/jijtest/build.gradle @@ -2,10 +2,26 @@ plugins { id 'net.neoforged.moddev' } +sourceSets { + service +} + +def serviceJar = tasks.register("serviceJar", Jar) { + from(sourceSets.service.output) + archiveClassifier = "service" + manifest { + attributes( + 'FMLModType': "LIBRARY" + ) + } +} + dependencies { + implementation files(sourceSets.service.output) testImplementation 'org.junit.jupiter:junit-jupiter:5.10.2' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' jarJar(project(":coremod")) + jarJar(files(serviceJar)) jarJar("org.commonmark:commonmark") { version { @@ -35,6 +51,9 @@ neoForge { coremod { dependency project(":coremod") } + service { + sourceSet sourceSets.service + } } unitTest { diff --git a/testproject/jijtest/src/service/java/jijtestservice/Service.java b/testproject/jijtest/src/service/java/jijtestservice/Service.java new file mode 100644 index 00000000..76b5b240 --- /dev/null +++ b/testproject/jijtest/src/service/java/jijtestservice/Service.java @@ -0,0 +1,5 @@ +package jijtestservice; + +public class Service { + +} diff --git a/testproject/jijtest/src/service/resources/META-INF/MANIFEST.MF b/testproject/jijtest/src/service/resources/META-INF/MANIFEST.MF new file mode 100644 index 00000000..eb6b1cfa --- /dev/null +++ b/testproject/jijtest/src/service/resources/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Note: Dummy Manifest file only for in-dev runtime +FMLModType: LIBRARY diff --git a/testproject/jijtest/src/test/java/jijtest/CoreModTest.java b/testproject/jijtest/src/test/java/jijtest/CoreModTest.java index e22b9bcb..277e0fd5 100644 --- a/testproject/jijtest/src/test/java/jijtest/CoreModTest.java +++ b/testproject/jijtest/src/test/java/jijtest/CoreModTest.java @@ -12,5 +12,10 @@ public class CoreModTest { void testPresenceOfCoreMod() throws Exception { var field = assertDoesNotThrow(() -> ItemStack.class.getField("CORE_MOD_MARKER")); assertTrue(field.getBoolean(null)); + + var obj = new jijtestservice.Service(); + // ensures it is *not* a transforming classloader, meaning it was loaded in our parent layer + assertEquals("cpw.mods.cl.ModuleClassLoader", obj.getClass().getClassLoader().getClass().getName()); + assertEquals("cpw.mods.modlauncher.TransformingClassLoader", getClass().getClassLoader().getClass().getName()); } }