Gradle 7 (and hopefully 8) support (#332)

* First pass on gradle 7 support

* Fix Gradle 8 deprecation warnings

* Add a deprecated compile constant
dev/0.11
modmuss50 2021-01-17 18:34:22 +00:00 committed by GitHub
parent f0bb05a205
commit 47097c65ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 38 additions and 21 deletions

View File

@ -107,7 +107,7 @@ public final class CompileConfiguration {
extendsFrom(Constants.Configurations.LOADER_DEPENDENCIES, Constants.Configurations.MINECRAFT_DEPENDENCIES, project); extendsFrom(Constants.Configurations.LOADER_DEPENDENCIES, Constants.Configurations.MINECRAFT_DEPENDENCIES, project);
extendsFrom(Constants.Configurations.MINECRAFT_NAMED, Constants.Configurations.LOADER_DEPENDENCIES, project); extendsFrom(Constants.Configurations.MINECRAFT_NAMED, Constants.Configurations.LOADER_DEPENDENCIES, project);
extendsFrom(JavaPlugin.COMPILE_CONFIGURATION_NAME, Constants.Configurations.MAPPINGS_FINAL, project); extendsFrom(JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME, Constants.Configurations.MAPPINGS_FINAL, project);
} }
/** /**

View File

@ -63,10 +63,6 @@ public abstract class DependencyProvider {
public abstract String getTargetConfig(); public abstract String getTargetConfig();
public void addDependency(Object object) {
addDependency(object, "compile");
}
public Dependency addDependency(Object object, String target) { public Dependency addDependency(Object object, String target) {
if (object instanceof File) { if (object instanceof File) {
object = project.files(object); object = project.files(object);

View File

@ -25,6 +25,10 @@
package net.fabricmc.loom.configuration; package net.fabricmc.loom.configuration;
import org.gradle.api.artifacts.ConfigurationContainer; import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.plugins.JavaPlugin;
import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.gradle.GradleSupport;
public class RemappedConfigurationEntry { public class RemappedConfigurationEntry {
private final String sourceConfiguration; private final String sourceConfiguration;
@ -61,7 +65,7 @@ public class RemappedConfigurationEntry {
public String getTargetConfiguration(ConfigurationContainer container) { public String getTargetConfiguration(ConfigurationContainer container) {
if (container.findByName(targetConfiguration) == null) { if (container.findByName(targetConfiguration) == null) {
return "compile"; return GradleSupport.IS_GRADLE_7_OR_NEWER ? JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME : Constants.Configurations.COMPILE;
} }
return targetConfiguration; return targetConfiguration;

View File

@ -27,9 +27,11 @@ package net.fabricmc.loom.util;
import java.util.List; import java.util.List;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import org.gradle.api.plugins.JavaPlugin;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;
import net.fabricmc.loom.configuration.RemappedConfigurationEntry; import net.fabricmc.loom.configuration.RemappedConfigurationEntry;
import net.fabricmc.loom.util.gradle.GradleSupport;
public class Constants { public class Constants {
public static final String LIBRARIES_BASE = "https://libraries.minecraft.net/"; public static final String LIBRARIES_BASE = "https://libraries.minecraft.net/";
@ -40,14 +42,23 @@ public class Constants {
public static final int ASM_VERSION = Opcodes.ASM9; public static final int ASM_VERSION = Opcodes.ASM9;
public static final List<RemappedConfigurationEntry> MOD_COMPILE_ENTRIES = ImmutableList.of( private static final List<RemappedConfigurationEntry> LEGACY_MOD_COMPILE_ENTRIES = ImmutableList.of(
new RemappedConfigurationEntry("modCompile", "compile", true, "compile"), new RemappedConfigurationEntry("modCompile", Configurations.COMPILE, true, "compile"),
new RemappedConfigurationEntry("modApi", "api", true, "compile"), new RemappedConfigurationEntry("modApi", JavaPlugin.API_CONFIGURATION_NAME, true, "compile"),
new RemappedConfigurationEntry("modImplementation", "implementation", true, "runtime"), new RemappedConfigurationEntry("modImplementation", JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME, true, "runtime"),
new RemappedConfigurationEntry("modRuntime", "runtimeOnly", false, ""), new RemappedConfigurationEntry("modRuntime", JavaPlugin.RUNTIME_ONLY_CONFIGURATION_NAME, false, ""),
new RemappedConfigurationEntry("modCompileOnly", "compileOnly", true, "") new RemappedConfigurationEntry("modCompileOnly", JavaPlugin.COMPILE_ONLY_CONFIGURATION_NAME, true, "")
); );
private static final List<RemappedConfigurationEntry> MODERN_MOD_COMPILE_ENTRIES = ImmutableList.of(
new RemappedConfigurationEntry("modApi", JavaPlugin.API_CONFIGURATION_NAME, true, "compile"),
new RemappedConfigurationEntry("modImplementation", JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME, true, "runtime"),
new RemappedConfigurationEntry("modRuntime", JavaPlugin.RUNTIME_ONLY_CONFIGURATION_NAME, false, ""),
new RemappedConfigurationEntry("modCompileOnly", JavaPlugin.COMPILE_ONLY_CONFIGURATION_NAME, true, "")
);
public static final List<RemappedConfigurationEntry> MOD_COMPILE_ENTRIES = GradleSupport.IS_GRADLE_7_OR_NEWER ? MODERN_MOD_COMPILE_ENTRIES : LEGACY_MOD_COMPILE_ENTRIES;
private Constants() { private Constants() {
} }
@ -65,6 +76,8 @@ public class Constants {
public static final String MAPPINGS = "mappings"; public static final String MAPPINGS = "mappings";
public static final String MAPPINGS_FINAL = "mappingsFinal"; public static final String MAPPINGS_FINAL = "mappingsFinal";
public static final String LOADER_DEPENDENCIES = "loaderLibraries"; public static final String LOADER_DEPENDENCIES = "loaderLibraries";
@Deprecated // Not to be used in gradle 7+
public static final String COMPILE = "compile";
private Configurations() { private Configurations() {
} }

View File

@ -38,6 +38,7 @@ import org.cadixdev.mercury.Mercury;
import org.cadixdev.mercury.remapper.MercuryRemapper; import org.cadixdev.mercury.remapper.MercuryRemapper;
import org.gradle.api.Project; import org.gradle.api.Project;
import org.gradle.api.artifacts.Dependency; import org.gradle.api.artifacts.Dependency;
import org.gradle.api.plugins.JavaPlugin;
import org.zeroturnaround.zip.ZipUtil; import org.zeroturnaround.zip.ZipUtil;
import net.fabricmc.loom.LoomGradleExtension; import net.fabricmc.loom.LoomGradleExtension;
@ -192,7 +193,7 @@ public class SourceRemapper {
m.getClassPath().add(extension.getMinecraftMappedProvider().getIntermediaryJar().toPath()); m.getClassPath().add(extension.getMinecraftMappedProvider().getIntermediaryJar().toPath());
Dependency annotationDependency = extension.getDependencyManager().getProvider(LaunchProvider.class).annotationDependency; Dependency annotationDependency = extension.getDependencyManager().getProvider(LaunchProvider.class).annotationDependency;
Set<File> files = project.getConfigurations().getByName("compileOnly") Set<File> files = project.getConfigurations().getByName(JavaPlugin.COMPILE_CLASSPATH_CONFIGURATION_NAME)
.files(annotationDependency); .files(annotationDependency);
for (File file : files) { for (File file : files) {

View File

@ -28,9 +28,12 @@ import java.lang.reflect.Method;
import org.gradle.api.Project; import org.gradle.api.Project;
import org.gradle.api.file.RegularFileProperty; import org.gradle.api.file.RegularFileProperty;
import org.gradle.util.GradleVersion;
// This is used to bridge the gap over large gradle api changes. // This is used to bridge the gap over large gradle api changes.
public class GradleSupport { public class GradleSupport {
public static final boolean IS_GRADLE_7_OR_NEWER = isIsGradle7OrNewer();
public static RegularFileProperty getfileProperty(Project project) { public static RegularFileProperty getfileProperty(Project project) {
try { try {
// First try the new method, if that fails fall back. // First try the new method, if that fails fall back.
@ -59,4 +62,9 @@ public class GradleSupport {
method.setAccessible(true); method.setAccessible(true);
return (RegularFileProperty) method.invoke(object); return (RegularFileProperty) method.invoke(object);
} }
public static boolean isIsGradle7OrNewer() {
String version = GradleVersion.current().getVersion();
return Integer.parseInt(version.substring(0, version.indexOf("."))) >= 7;
}
} }

View File

@ -24,10 +24,10 @@ dependencies {
//to change the versions see the gradle.properties file //to change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:\${project.minecraft_version}" minecraft "com.mojang:minecraft:\${project.minecraft_version}"
mappings ${mappingsDep} mappings ${mappingsDep}
modCompile "net.fabricmc:fabric-loader:\${project.loader_version}" modImplementation "net.fabricmc:fabric-loader:\${project.loader_version}"
// Fabric API. This is technically optional, but you probably want it anyway. // Fabric API. This is technically optional, but you probably want it anyway.
modCompile "net.fabricmc.fabric-api:fabric-api:\${project.fabric_version}" 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. // PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs.
// You may need to force-disable transitiveness on them. // You may need to force-disable transitiveness on them.
@ -36,14 +36,9 @@ dependencies {
processResources { processResources {
inputs.property "version", project.version inputs.property "version", project.version
from(sourceSets.main.resources.srcDirs) { filesMatching("fabric.mod.json") {
include "fabric.mod.json"
expand "version": project.version expand "version": project.version
} }
from(sourceSets.main.resources.srcDirs) {
exclude "fabric.mod.json"
}
} }
// ensure that the encoding is set to UTF-8, no matter what the system default is // ensure that the encoding is set to UTF-8, no matter what the system default is