AbstractRemapJarTask: Fix manifests not being inherited (#1421)

* AbstractRemapJarTask: Fix manifests not being inherited

* Copy manifest before applying manifest service

This matches the original ordering of the manifest creation,
and fixes the override functionality in the service.

* Test merging named sections
This commit is contained in:
Juuz
2025-11-03 16:55:07 +02:00
committed by GitHub
parent 94d4eb6e08
commit 5d59759a89
3 changed files with 50 additions and 0 deletions

View File

@@ -34,6 +34,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import javax.inject.Inject;
@@ -74,6 +75,12 @@ import net.fabricmc.loom.util.gradle.SourceSetHelper;
import net.fabricmc.loom.util.service.ScopedServiceFactory;
public abstract class AbstractRemapJarTask extends Jar {
/**
* The main input jar to remap.
* Other contents can be added to this task, but this jar must always be present.
*
* <p>The input file's manifest will be copied into the remapped jar.
*/
@InputFile
public abstract RegularFileProperty getInputFile();
@@ -143,6 +150,7 @@ public abstract class AbstractRemapJarTask extends Jar {
final WorkQueue workQueue = getWorkerExecutor().noIsolation();
workQueue.submit(workAction, params -> {
params.getInputFile().set(getInputFile());
params.getArchiveFile().set(getArchiveFile());
params.getSourceNamespace().set(getSourceNamespace());
@@ -181,6 +189,7 @@ public abstract class AbstractRemapJarTask extends Jar {
protected abstract Provider<? extends ClientEntriesService.Options> getClientOnlyEntriesOptionsProvider(SourceSet clientSourceSet);
public interface AbstractRemapParams extends WorkParameters {
RegularFileProperty getInputFile();
RegularFileProperty getArchiveFile();
Property<String> getSourceNamespace();
@@ -242,11 +251,20 @@ public abstract class AbstractRemapJarTask extends Jar {
}
}
// Note: the inputFile parameter is the remapping input file.
// The main input jar is available in the parameters, but should not be used
// for remapping as it might be missing some files added manually to this task.
protected abstract void execute(Path inputFile) throws IOException;
protected void modifyJarManifest() throws IOException {
int count = ZipUtils.transform(outputFile, Map.of(Constants.Manifest.PATH, bytes -> {
var manifest = new Manifest(new ByteArrayInputStream(bytes));
byte[] sourceManifestBytes = ZipUtils.unpackNullable(getParameters().getInputFile().get().getAsFile().toPath(), Constants.Manifest.PATH);
if (sourceManifestBytes != null) {
var sourceManifest = new Manifest(new ByteArrayInputStream(sourceManifestBytes));
mergeManifests(manifest, sourceManifest);
}
getParameters().getJarManifestService().get().apply(manifest, getParameters().getManifestAttributes().get());
manifest.getMainAttributes().putValue(Constants.Manifest.MAPPING_NAMESPACE, getParameters().getTargetNamespace().get());
@@ -268,6 +286,24 @@ public abstract class AbstractRemapJarTask extends Jar {
ZipReprocessorUtil.reprocessZip(outputFile, isReproducibleFileOrder, isPreserveFileTimestamps, compression);
}
}
private static void mergeManifests(Manifest target, Manifest source) {
mergeAttributes(target.getMainAttributes(), source.getMainAttributes());
source.getEntries().forEach((name, sourceAttributes) -> {
final Attributes targetAttributes = target.getAttributes(name);
if (targetAttributes != null) {
mergeAttributes(targetAttributes, sourceAttributes);
} else {
target.getEntries().put(name, sourceAttributes);
}
});
}
private static void mergeAttributes(Attributes target, Attributes source) {
source.forEach(target::putIfAbsent);
}
}
@Deprecated

View File

@@ -51,6 +51,10 @@ class RemapJarContentsTest extends Specification implements GradleProjectTestTra
ZipUtils.contains(gradle.getOutputFile('fabric-example-mod-1.0.0-sources.jar').toPath(), 'test_src_file.txt')
def manifest = readManifest(gradle.getOutputFile('fabric-example-mod-1.0.0.jar'))
manifest.mainAttributes.getValue('Hello-World') == 'test'
manifest.mainAttributes.getValue('Inherited-In-Remap-Jar') == '1234'
manifest.getAttributes('fabric.mod.json').getValue('Inherited-In-Remap-Jar') == '5678'
manifest.getAttributes('modid.mixins.json').getValue('Hello-World') == 'another test'
manifest.getAttributes('modid.mixins.json').getValue('Inherited-In-Remap-Jar') == '9'
where:
version << STANDARD_TEST_VERSIONS

View File

@@ -78,11 +78,21 @@ publishing {
}
}
jar {
manifest {
attributes 'Inherited-In-Remap-Jar': '1234'
attributes(['Inherited-In-Remap-Jar': '5678'], 'fabric.mod.json') // category not present in remapJar
attributes(['Inherited-In-Remap-Jar': '9'], 'modid.mixins.json') // category present in remapJar
attributes 'Hello-World': 'shadowed in remapJar'
}
}
remapJar {
from 'test_file.txt'
manifest {
attributes 'Hello-World': 'test'
attributes(['Hello-World': 'another test'], 'modid.mixins.json')
}
}