From 42c8d9f44f5ba19701c553b9357ef2582e8dee20 Mon Sep 17 00:00:00 2001 From: ishland Date: Sat, 11 Nov 2023 12:28:00 +0800 Subject: [PATCH 1/2] fix: composite builds --- .../net/fabricmc/loom/task/RemapJarTask.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/fabricmc/loom/task/RemapJarTask.java b/src/main/java/net/fabricmc/loom/task/RemapJarTask.java index fbc6e720d..5ee9e7b9c 100644 --- a/src/main/java/net/fabricmc/loom/task/RemapJarTask.java +++ b/src/main/java/net/fabricmc/loom/task/RemapJarTask.java @@ -80,9 +80,13 @@ import net.fabricmc.tinyremapper.TinyRemapper; public abstract class RemapJarTask extends AbstractRemapJarTask { + @Deprecated @InputFiles public abstract ConfigurableFileCollection getNestedJars(); + @Internal + public abstract ListProperty getNestedConfigurations(); + @Input public abstract Property getAddNestedDependencies(); @@ -106,7 +110,7 @@ public RemapJarTask() { getAddNestedDependencies().convention(true).finalizeValueOnRead(); Configuration includeConfiguration = getProject().getConfigurations().getByName(Constants.Configurations.INCLUDE); - getNestedJars().from(new IncludedJarFactory(getProject()).getNestedJars(includeConfiguration)); + getNestedConfigurations().add(includeConfiguration); getUseMixinAP().set(LoomGradleExtension.get(getProject()).getMixin().getUseLegacyMixinAp()); @@ -121,6 +125,18 @@ public RemapJarTask() { getJarType().set("classes"); } + @InputFiles + public FileCollection getNestedDependenciesResolved() { // for task up-to-date checking + final ConfigurableFileCollection files = getProject().files(); + + if (getAddNestedDependencies().get()) { + getNestedConfigurations().get().forEach(files::from); + files.from(getNestedJars()); + } + + return files; + } + private void setupPreparationTask() { PrepareJarRemapTask prepareJarTask = getProject().getTasks().create("prepare" + getName().substring(0, 1).toUpperCase() + getName().substring(1), PrepareJarRemapTask.class, this); @@ -141,6 +157,9 @@ private void setupPreparationTask() { public void run() { submitWork(RemapAction.class, params -> { if (getAddNestedDependencies().get()) { + getNestedConfigurations().get().forEach(configuration -> { + params.getNestedJars().from(new IncludedJarFactory(getProject()).getNestedJars(configuration)); + }); params.getNestedJars().from(getNestedJars()); } From 220229a62b2a27ea37fd3937666ac935062ab75a Mon Sep 17 00:00:00 2001 From: ishland Date: Mon, 4 Dec 2023 22:23:14 +0800 Subject: [PATCH 2/2] new: add tests for composite builds Based on `simple` test --- .../integration/CompositeBuildTest.groovy | 49 +++++++++++ .../projects/composite-build/build.gradle | 84 +++++++++++++++++++ .../composite-build/external/build.gradle | 22 +++++ .../src/java/external/SomeExternalClass.java | 4 + .../composite-build/gradle.properties | 16 ++++ .../projects/composite-build/settings.gradle | 7 ++ .../java/net/fabricmc/example/ExampleMod.java | 17 ++++ .../fabricmc/example/mixin/ExampleMixin.java | 15 ++++ .../src/main/resources/fabric.mod.json | 36 ++++++++ .../src/main/resources/modid.mixins.json | 14 ++++ 10 files changed, 264 insertions(+) create mode 100644 src/test/groovy/net/fabricmc/loom/test/integration/CompositeBuildTest.groovy create mode 100644 src/test/resources/projects/composite-build/build.gradle create mode 100644 src/test/resources/projects/composite-build/external/build.gradle create mode 100644 src/test/resources/projects/composite-build/external/src/java/external/SomeExternalClass.java create mode 100644 src/test/resources/projects/composite-build/gradle.properties create mode 100644 src/test/resources/projects/composite-build/settings.gradle create mode 100644 src/test/resources/projects/composite-build/src/main/java/net/fabricmc/example/ExampleMod.java create mode 100644 src/test/resources/projects/composite-build/src/main/java/net/fabricmc/example/mixin/ExampleMixin.java create mode 100644 src/test/resources/projects/composite-build/src/main/resources/fabric.mod.json create mode 100644 src/test/resources/projects/composite-build/src/main/resources/modid.mixins.json diff --git a/src/test/groovy/net/fabricmc/loom/test/integration/CompositeBuildTest.groovy b/src/test/groovy/net/fabricmc/loom/test/integration/CompositeBuildTest.groovy new file mode 100644 index 000000000..c1f626e65 --- /dev/null +++ b/src/test/groovy/net/fabricmc/loom/test/integration/CompositeBuildTest.groovy @@ -0,0 +1,49 @@ +/* + * This file is part of fabric-loom, licensed under the MIT License (MIT). + * + * Copyright (c) 2018-2021 FabricMC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.fabricmc.loom.test.integration + +import net.fabricmc.loom.test.util.GradleProjectTestTrait +import spock.lang.Specification +import spock.lang.Unroll + +import static net.fabricmc.loom.test.LoomTestConstants.STANDARD_TEST_VERSIONS +import static org.gradle.testkit.runner.TaskOutcome.SUCCESS + +class CompositeBuildTest extends Specification implements GradleProjectTestTrait { + @Unroll + def "build (gradle #version)"() { + setup: + def gradle = gradleProject(project: "composite-build", version: version) + + when: + def result = gradle.run(task: "build") + + then: + result.task(":build").outcome == SUCCESS + + where: + version << STANDARD_TEST_VERSIONS + } +} diff --git a/src/test/resources/projects/composite-build/build.gradle b/src/test/resources/projects/composite-build/build.gradle new file mode 100644 index 000000000..cfe1c65d1 --- /dev/null +++ b/src/test/resources/projects/composite-build/build.gradle @@ -0,0 +1,84 @@ +plugins { + id 'fabric-loom' + id 'maven-publish' +} + +version = loom.modVersion +group = project.maven_group + +repositories { + // Add repositories to retrieve artifacts from in here. + // You should only use this when depending on other mods because + // Loom adds the essential maven repositories to download Minecraft and libraries from automatically. + // See https://docs.gradle.org/current/userguide/declaring_repositories.html + // for more information about repositories. +} + +dependencies { + // To change the versions see the gradle.properties file + minecraft "com.mojang:minecraft:${project.minecraft_version}" + mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" + modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" + + // Fabric API. This is technically optional, but you probably want it anyway. + modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + + // PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs. + // You may need to force-disable transitiveness on them. + + // Includes included build + include "external:external" +} + +base { + archivesName = project.archives_base_name +} + +tasks.withType(JavaCompile).configureEach { + // ensure that the encoding is set to UTF-8, no matter what the system default is + // this fixes some edge cases with special characters not displaying correctly + // see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html + // If Javadoc is generated, this must be specified in that task too. + it.options.encoding = "UTF-8" + + // The Minecraft launcher currently installs Java 8 for users, so your mod probably wants to target Java 8 too + // JDK 9 introduced a new way of specifying this that will make sure no newer classes or methods are used. + // We'll use that if it's available, but otherwise we'll use the older option. + def targetVersion = 8 + if (JavaVersion.current().isJava9Compatible()) { + it.options.release = targetVersion + } +} + +java { + // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task + // if it is present. + // If you remove this line, sources will not be generated. + withSourcesJar() + + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} + +jar { + from("LICENSE") { + rename { "${it}_${base.archivesName.get()}"} + } +} + +// configure the maven publication +publishing { + publications { + mavenJava(MavenPublication) { + from components.java + } + } + + // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. + repositories { + // Add repositories to publish to here. + // Notice: This block does NOT have the same function as the block in the top level. + // The repositories here will be used for publishing your artifact, not for + // retrieving dependencies. + } +} diff --git a/src/test/resources/projects/composite-build/external/build.gradle b/src/test/resources/projects/composite-build/external/build.gradle new file mode 100644 index 000000000..71a304183 --- /dev/null +++ b/src/test/resources/projects/composite-build/external/build.gradle @@ -0,0 +1,22 @@ +plugins { + id 'java' +} + +group = 'external' +version = '0.0.1-SNAPSHOT' + +repositories { + mavenCentral() +} + +tasks.withType(JavaCompile).configureEach { + it.options.encoding = "UTF-8" + + def targetVersion = 8 + if (JavaVersion.current().isJava9Compatible()) { + it.options.release = targetVersion + } +} + +dependencies { +} diff --git a/src/test/resources/projects/composite-build/external/src/java/external/SomeExternalClass.java b/src/test/resources/projects/composite-build/external/src/java/external/SomeExternalClass.java new file mode 100644 index 000000000..648c800c0 --- /dev/null +++ b/src/test/resources/projects/composite-build/external/src/java/external/SomeExternalClass.java @@ -0,0 +1,4 @@ +package external; + +public class SomeExternalClass { +} diff --git a/src/test/resources/projects/composite-build/gradle.properties b/src/test/resources/projects/composite-build/gradle.properties new file mode 100644 index 000000000..845c35607 --- /dev/null +++ b/src/test/resources/projects/composite-build/gradle.properties @@ -0,0 +1,16 @@ +# Done to increase the memory available to gradle. +org.gradle.jvmargs=-Xmx1G + +# Fabric Properties + # check these on https://fabricmc.net/use + minecraft_version=1.16.5 + yarn_mappings=1.16.5+build.5 + loader_version=0.11.2 + +# Mod Properties + maven_group = com.example + archives_base_name = fabric-example-mod + +# Dependencies + # currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api + fabric_version=0.31.0+1.16 diff --git a/src/test/resources/projects/composite-build/settings.gradle b/src/test/resources/projects/composite-build/settings.gradle new file mode 100644 index 000000000..d7f98d345 --- /dev/null +++ b/src/test/resources/projects/composite-build/settings.gradle @@ -0,0 +1,7 @@ +rootProject.name = "fabric-example-mod" + +includeBuild('external') { + dependencySubstitution { + substitute module('external:external') using project(':') + } +} diff --git a/src/test/resources/projects/composite-build/src/main/java/net/fabricmc/example/ExampleMod.java b/src/test/resources/projects/composite-build/src/main/java/net/fabricmc/example/ExampleMod.java new file mode 100644 index 000000000..5330d1193 --- /dev/null +++ b/src/test/resources/projects/composite-build/src/main/java/net/fabricmc/example/ExampleMod.java @@ -0,0 +1,17 @@ +package net.fabricmc.example; + +import net.fabricmc.api.ModInitializer; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class ExampleMod implements ModInitializer { + public static final Logger LOGGER = LogManager.getLogger("modid"); + + /** + * @see net.fabricmc.example + */ + @Override + public void onInitialize() { + LOGGER.info("Hello simple Fabric mod!"); + } +} \ No newline at end of file diff --git a/src/test/resources/projects/composite-build/src/main/java/net/fabricmc/example/mixin/ExampleMixin.java b/src/test/resources/projects/composite-build/src/main/java/net/fabricmc/example/mixin/ExampleMixin.java new file mode 100644 index 000000000..83ee1a89d --- /dev/null +++ b/src/test/resources/projects/composite-build/src/main/java/net/fabricmc/example/mixin/ExampleMixin.java @@ -0,0 +1,15 @@ +package net.fabricmc.example.mixin; + +import net.minecraft.client.gui.screen.TitleScreen; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(TitleScreen.class) +public class ExampleMixin { + @Inject(at = @At("HEAD"), method = "init()V") + private void init(CallbackInfo info) { + System.out.println("This line is printed by an example mod mixin!"); + } +} diff --git a/src/test/resources/projects/composite-build/src/main/resources/fabric.mod.json b/src/test/resources/projects/composite-build/src/main/resources/fabric.mod.json new file mode 100644 index 000000000..14ba01fdd --- /dev/null +++ b/src/test/resources/projects/composite-build/src/main/resources/fabric.mod.json @@ -0,0 +1,36 @@ +{ + "schemaVersion": 1, + "id": "modid", + "version": "1.0.0", + + "name": "Example Mod", + "description": "This is an example description! Tell everyone what your mod is about!", + "authors": [ + "Me!" + ], + "contact": { + "homepage": "https://fabricmc.net/", + "sources": "https://github.com/FabricMC/fabric-example-mod" + }, + + "license": "CC0-1.0", + + "environment": "*", + "entrypoints": { + "main": [ + "net.fabricmc.example.ExampleMod" + ] + }, + "mixins": [ + "modid.mixins.json" + ], + + "depends": { + "fabricloader": ">=0.7.4", + "fabric": "*", + "minecraft": "1.16.x" + }, + "suggests": { + "another-mod": "*" + } +} diff --git a/src/test/resources/projects/composite-build/src/main/resources/modid.mixins.json b/src/test/resources/projects/composite-build/src/main/resources/modid.mixins.json new file mode 100644 index 000000000..21fe73a49 --- /dev/null +++ b/src/test/resources/projects/composite-build/src/main/resources/modid.mixins.json @@ -0,0 +1,14 @@ +{ + "required": true, + "minVersion": "0.8", + "package": "net.fabricmc.example.mixin", + "compatibilityLevel": "JAVA_8", + "mixins": [ + ], + "client": [ + "ExampleMixin" + ], + "injectors": { + "defaultRequire": 1 + } +}