Remove log4j from compile classpath starting with Minecraft 22w03a.

This is done to force modders to use SLF4J as Mojang have the ability to remove it at anytime. An option is provided to disable this.
dev/0.11
modmuss50 2022-01-19 20:04:10 +00:00
parent 54bf5480b7
commit b31ce4e525
8 changed files with 45 additions and 9 deletions

View File

@ -152,4 +152,6 @@ public interface LoomGradleExtensionAPI {
default void splitMinecraftJar() { default void splitMinecraftJar() {
getMinecraftJarConfiguration().set(MinecraftJarConfiguration.SPLIT); getMinecraftJarConfiguration().set(MinecraftJarConfiguration.SPLIT);
} }
Property<Boolean> getRuntimeOnlyLog4j();
} }

View File

@ -63,6 +63,7 @@ public final class CompileConfiguration {
extension.createLazyConfiguration(Constants.Configurations.MOD_COMPILE_CLASSPATH_MAPPED, configuration -> configuration.setTransitive(false)); extension.createLazyConfiguration(Constants.Configurations.MOD_COMPILE_CLASSPATH_MAPPED, configuration -> configuration.setTransitive(false));
extension.createLazyConfiguration(Constants.Configurations.MINECRAFT_NAMED, configuration -> configuration.setTransitive(false)); // The launchers do not recurse dependencies extension.createLazyConfiguration(Constants.Configurations.MINECRAFT_NAMED, configuration -> configuration.setTransitive(false)); // The launchers do not recurse dependencies
NamedDomainObjectProvider<Configuration> serverDeps = extension.createLazyConfiguration(Constants.Configurations.MINECRAFT_SERVER_DEPENDENCIES, configuration -> configuration.setTransitive(false)); NamedDomainObjectProvider<Configuration> serverDeps = extension.createLazyConfiguration(Constants.Configurations.MINECRAFT_SERVER_DEPENDENCIES, configuration -> configuration.setTransitive(false));
extension.createLazyConfiguration(Constants.Configurations.MINECRAFT_RUNTIME_DEPENDENCIES, configuration -> configuration.setTransitive(false));
extension.createLazyConfiguration(Constants.Configurations.MINECRAFT_DEPENDENCIES, configuration -> { extension.createLazyConfiguration(Constants.Configurations.MINECRAFT_DEPENDENCIES, configuration -> {
configuration.extendsFrom(serverDeps.get()); configuration.extendsFrom(serverDeps.get());
configuration.setTransitive(false); configuration.setTransitive(false);
@ -126,6 +127,8 @@ public final class CompileConfiguration {
extendsFrom(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME, Constants.Configurations.LOOM_DEVELOPMENT_DEPENDENCIES, project); extendsFrom(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME, Constants.Configurations.LOOM_DEVELOPMENT_DEPENDENCIES, project);
extendsFrom(JavaPlugin.TEST_RUNTIME_CLASSPATH_CONFIGURATION_NAME, Constants.Configurations.LOOM_DEVELOPMENT_DEPENDENCIES, project); extendsFrom(JavaPlugin.TEST_RUNTIME_CLASSPATH_CONFIGURATION_NAME, Constants.Configurations.LOOM_DEVELOPMENT_DEPENDENCIES, project);
extendsFrom(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME, Constants.Configurations.MINECRAFT_RUNTIME_DEPENDENCIES, project);
// Add the dev time dependencies // Add the dev time dependencies
project.getDependencies().add(Constants.Configurations.LOOM_DEVELOPMENT_DEPENDENCIES, Constants.Dependencies.DEV_LAUNCH_INJECTOR + Constants.Dependencies.Versions.DEV_LAUNCH_INJECTOR); project.getDependencies().add(Constants.Configurations.LOOM_DEVELOPMENT_DEPENDENCIES, Constants.Dependencies.DEV_LAUNCH_INJECTOR + Constants.Dependencies.Versions.DEV_LAUNCH_INJECTOR);
project.getDependencies().add(Constants.Configurations.LOOM_DEVELOPMENT_DEPENDENCIES, Constants.Dependencies.TERMINAL_CONSOLE_APPENDER + Constants.Dependencies.Versions.TERMINAL_CONSOLE_APPENDER); project.getDependencies().add(Constants.Configurations.LOOM_DEVELOPMENT_DEPENDENCIES, Constants.Dependencies.TERMINAL_CONSOLE_APPENDER + Constants.Dependencies.Versions.TERMINAL_CONSOLE_APPENDER);

View File

@ -38,9 +38,11 @@ public class MinecraftLibraryProvider {
private static final Pattern NATIVES_PATTERN = Pattern.compile("^(?<group>.*)/(.*?)/(?<version>.*)/((?<name>.*?)-([0-9].*?)-)(?<classifier>.*).jar$"); private static final Pattern NATIVES_PATTERN = Pattern.compile("^(?<group>.*)/(.*?)/(?<version>.*)/((?<name>.*?)-([0-9].*?)-)(?<classifier>.*).jar$");
public void provide(MinecraftProvider minecraftProvider, Project project) { public void provide(MinecraftProvider minecraftProvider, Project project) {
final MinecraftJarConfiguration jarConfiguration = LoomGradleExtension.get(project).getMinecraftJarConfiguration().get(); final LoomGradleExtension extension = LoomGradleExtension.get(project);
final MinecraftJarConfiguration jarConfiguration = extension.getMinecraftJarConfiguration().get();
final MinecraftVersionMeta versionInfo = minecraftProvider.getVersionInfo(); final MinecraftVersionMeta versionInfo = minecraftProvider.getVersionInfo();
final BundleMetadata serverBundleMetadata = minecraftProvider.getServerBundleMetadata(); final BundleMetadata serverBundleMetadata = minecraftProvider.getServerBundleMetadata();
final boolean runtimeOnlyLog4j = versionInfo.isVersionOrNewer(Constants.MinecraftReleaseTimes.MC_20W03A) && extension.getRuntimeOnlyLog4j().get();
final boolean overrideLWJGL = LWJGLVersionOverride.overrideByDefault() || LWJGLVersionOverride.forceOverride(project) || Boolean.getBoolean("loom.test.lwjgloverride"); final boolean overrideLWJGL = LWJGLVersionOverride.overrideByDefault() || LWJGLVersionOverride.forceOverride(project) || Boolean.getBoolean("loom.test.lwjgloverride");
@ -54,11 +56,16 @@ public class MinecraftLibraryProvider {
} }
if (library.isValidForOS() && !library.hasNatives() && library.artifact() != null) { if (library.isValidForOS() && !library.hasNatives() && library.artifact() != null) {
if (serverBundleMetadata != null && isLibraryInBundle(serverBundleMetadata, library)) { if (runtimeOnlyLog4j && library.name().startsWith("org.apache.logging.log4j")) {
// Make log4j a runtime only dep in 20w03a or later. Modders should use SLF4J.
project.getDependencies().add(Constants.Configurations.MINECRAFT_RUNTIME_DEPENDENCIES, library.name());
} else if (serverBundleMetadata != null && isLibraryInBundle(serverBundleMetadata, library)) {
project.getDependencies().add(Constants.Configurations.MINECRAFT_SERVER_DEPENDENCIES, library.name()); project.getDependencies().add(Constants.Configurations.MINECRAFT_SERVER_DEPENDENCIES, library.name());
} else if (jarConfiguration.getSupportedEnvironments().contains("client")) { } else if (jarConfiguration.getSupportedEnvironments().contains("client")) {
// Client only library, or legacy version // Client only library, or legacy version
project.getDependencies().add(Constants.Configurations.MINECRAFT_DEPENDENCIES, library.name()); project.getDependencies().add(Constants.Configurations.MINECRAFT_DEPENDENCIES, library.name());
} else {
throw new RuntimeException("Library %s was not added to a configuration".formatted(library.name()));
} }
} }

View File

@ -51,6 +51,10 @@ public record MinecraftVersionMeta(
return downloads().get(key); return downloads().get(key);
} }
public boolean isVersionOrNewer(String releaseTime) {
return this.releaseTime().compareTo(releaseTime) >= 0;
}
public record AssetIndex(String id, long totalSize, String path, String sha1, long size, String url) { public record AssetIndex(String id, long totalSize, String path, String sha1, long size, String url) {
public String fabricId(String version) { public String fabricId(String version) {
return id.equals(version) ? version : version + "-" + id; return id.equals(version) ? version : version + "-" + id;

View File

@ -63,6 +63,7 @@ public abstract class LoomGradleExtensionApiImpl implements LoomGradleExtensionA
protected final Property<Boolean> transitiveAccessWideners; protected final Property<Boolean> transitiveAccessWideners;
protected final Property<String> intermediary; protected final Property<String> intermediary;
protected final Property<Boolean> enableInterfaceInjection; protected final Property<Boolean> enableInterfaceInjection;
private final Property<Boolean> runtimeOnlyLog4j;
private final Property<MinecraftJarConfiguration> minecraftJarConfiguration; private final Property<MinecraftJarConfiguration> minecraftJarConfiguration;
private final ModVersionParser versionParser; private final ModVersionParser versionParser;
@ -104,6 +105,9 @@ public abstract class LoomGradleExtensionApiImpl implements LoomGradleExtensionA
this.accessWidener.finalizeValueOnRead(); this.accessWidener.finalizeValueOnRead();
this.getGameJarProcessors().finalizeValueOnRead(); this.getGameJarProcessors().finalizeValueOnRead();
this.runtimeOnlyLog4j = project.getObjects().property(Boolean.class).convention(true);
this.runtimeOnlyLog4j.finalizeValueOnRead();
} }
@Override @Override
@ -213,6 +217,11 @@ public abstract class LoomGradleExtensionApiImpl implements LoomGradleExtensionA
return minecraftJarConfiguration; return minecraftJarConfiguration;
} }
@Override
public Property<Boolean> getRuntimeOnlyLog4j() {
return runtimeOnlyLog4j;
}
// This is here to ensure that LoomGradleExtensionApiImpl compiles without any unimplemented methods // This is here to ensure that LoomGradleExtensionApiImpl compiles without any unimplemented methods
private final class EnsureCompile extends LoomGradleExtensionApiImpl { private final class EnsureCompile extends LoomGradleExtensionApiImpl {
private EnsureCompile() { private EnsureCompile() {

View File

@ -68,6 +68,7 @@ public class Constants {
*/ */
public static final String MINECRAFT_SERVER_DEPENDENCIES = "minecraftServerLibraries"; public static final String MINECRAFT_SERVER_DEPENDENCIES = "minecraftServerLibraries";
public static final String MINECRAFT_DEPENDENCIES = "minecraftLibraries"; public static final String MINECRAFT_DEPENDENCIES = "minecraftLibraries";
public static final String MINECRAFT_RUNTIME_DEPENDENCIES = "minecraftRuntimeOnlyLibraries";
public static final String MINECRAFT_NATIVES = "minecraftNatives"; public static final String MINECRAFT_NATIVES = "minecraftNatives";
public static final String MINECRAFT_NAMED = "minecraftNamed"; public static final String MINECRAFT_NAMED = "minecraftNamed";
public static final String MAPPINGS = "mappings"; public static final String MAPPINGS = "mappings";
@ -141,4 +142,8 @@ public class Constants {
private TaskGroup() { private TaskGroup() {
} }
} }
public static final class MinecraftReleaseTimes {
public static final String MC_20W03A = "2022-01-19T16:04:59+00:00";
}
} }

View File

@ -107,10 +107,12 @@ trait GradleProjectTestTrait {
throw new FileNotFoundException("Failed to find project directory at: $projectSourceDir.absolutePath") throw new FileNotFoundException("Failed to find project directory at: $projectSourceDir.absolutePath")
} }
def settingsGradle = new File(projectDir, "settings.gradle")
// Cleanup some basic things if they already exists // Cleanup some basic things if they already exists
new File(projectDir, "src").deleteDir() new File(projectDir, "src").deleteDir()
new File(projectDir, "build.gradle").delete() new File(projectDir, "build.gradle").delete()
new File(projectDir, "settings.gradle").delete() settingsGradle.delete()
projectSourceDir.eachFileRecurse { file -> projectSourceDir.eachFileRecurse { file ->
if (file.isDirectory()) { if (file.isDirectory()) {
@ -128,6 +130,10 @@ trait GradleProjectTestTrait {
tempFile.parentFile.mkdirs() tempFile.parentFile.mkdirs()
tempFile.bytes = file.bytes tempFile.bytes = file.bytes
} }
if (!settingsGradle.exists()) {
settingsGradle.createNewFile()
}
} }
@Immutable @Immutable

View File

@ -1,14 +1,14 @@
package net.fabricmc.example; package net.fabricmc.example;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ExampleMod implements ModInitializer { public class ExampleMod implements ModInitializer {
public static final Logger LOGGER = LogManager.getLogger("modid");
@Override @Override
public void onInitialize() { public void onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state. LOGGER.info("Hello Fabric world!");
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.
System.out.println("Hello simple Fabric mod");
} }
} }