Skip to content

Commit 5d59759

Browse files
authored
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
1 parent 94d4eb6 commit 5d59759

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/main/java/net/fabricmc/loom/task/AbstractRemapJarTask.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.Collections;
3535
import java.util.List;
3636
import java.util.Map;
37+
import java.util.jar.Attributes;
3738
import java.util.jar.Manifest;
3839

3940
import javax.inject.Inject;
@@ -74,6 +75,12 @@
7475
import net.fabricmc.loom.util.service.ScopedServiceFactory;
7576

7677
public abstract class AbstractRemapJarTask extends Jar {
78+
/**
79+
* The main input jar to remap.
80+
* Other contents can be added to this task, but this jar must always be present.
81+
*
82+
* <p>The input file's manifest will be copied into the remapped jar.
83+
*/
7784
@InputFile
7885
public abstract RegularFileProperty getInputFile();
7986

@@ -143,6 +150,7 @@ public final <P extends AbstractRemapParams> void submitWork(Class<? extends Abs
143150
final WorkQueue workQueue = getWorkerExecutor().noIsolation();
144151

145152
workQueue.submit(workAction, params -> {
153+
params.getInputFile().set(getInputFile());
146154
params.getArchiveFile().set(getArchiveFile());
147155

148156
params.getSourceNamespace().set(getSourceNamespace());
@@ -181,6 +189,7 @@ public final <P extends AbstractRemapParams> void submitWork(Class<? extends Abs
181189
protected abstract Provider<? extends ClientEntriesService.Options> getClientOnlyEntriesOptionsProvider(SourceSet clientSourceSet);
182190

183191
public interface AbstractRemapParams extends WorkParameters {
192+
RegularFileProperty getInputFile();
184193
RegularFileProperty getArchiveFile();
185194

186195
Property<String> getSourceNamespace();
@@ -242,11 +251,20 @@ public final void execute() {
242251
}
243252
}
244253

254+
// Note: the inputFile parameter is the remapping input file.
255+
// The main input jar is available in the parameters, but should not be used
256+
// for remapping as it might be missing some files added manually to this task.
245257
protected abstract void execute(Path inputFile) throws IOException;
246258

247259
protected void modifyJarManifest() throws IOException {
248260
int count = ZipUtils.transform(outputFile, Map.of(Constants.Manifest.PATH, bytes -> {
249261
var manifest = new Manifest(new ByteArrayInputStream(bytes));
262+
byte[] sourceManifestBytes = ZipUtils.unpackNullable(getParameters().getInputFile().get().getAsFile().toPath(), Constants.Manifest.PATH);
263+
264+
if (sourceManifestBytes != null) {
265+
var sourceManifest = new Manifest(new ByteArrayInputStream(sourceManifestBytes));
266+
mergeManifests(manifest, sourceManifest);
267+
}
250268

251269
getParameters().getJarManifestService().get().apply(manifest, getParameters().getManifestAttributes().get());
252270
manifest.getMainAttributes().putValue(Constants.Manifest.MAPPING_NAMESPACE, getParameters().getTargetNamespace().get());
@@ -268,6 +286,24 @@ protected void rewriteJar() throws IOException {
268286
ZipReprocessorUtil.reprocessZip(outputFile, isReproducibleFileOrder, isPreserveFileTimestamps, compression);
269287
}
270288
}
289+
290+
private static void mergeManifests(Manifest target, Manifest source) {
291+
mergeAttributes(target.getMainAttributes(), source.getMainAttributes());
292+
293+
source.getEntries().forEach((name, sourceAttributes) -> {
294+
final Attributes targetAttributes = target.getAttributes(name);
295+
296+
if (targetAttributes != null) {
297+
mergeAttributes(targetAttributes, sourceAttributes);
298+
} else {
299+
target.getEntries().put(name, sourceAttributes);
300+
}
301+
});
302+
}
303+
304+
private static void mergeAttributes(Attributes target, Attributes source) {
305+
source.forEach(target::putIfAbsent);
306+
}
271307
}
272308

273309
@Deprecated

src/test/groovy/net/fabricmc/loom/test/integration/RemapJarContentsTest.groovy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ class RemapJarContentsTest extends Specification implements GradleProjectTestTra
5151
ZipUtils.contains(gradle.getOutputFile('fabric-example-mod-1.0.0-sources.jar').toPath(), 'test_src_file.txt')
5252
def manifest = readManifest(gradle.getOutputFile('fabric-example-mod-1.0.0.jar'))
5353
manifest.mainAttributes.getValue('Hello-World') == 'test'
54+
manifest.mainAttributes.getValue('Inherited-In-Remap-Jar') == '1234'
55+
manifest.getAttributes('fabric.mod.json').getValue('Inherited-In-Remap-Jar') == '5678'
56+
manifest.getAttributes('modid.mixins.json').getValue('Hello-World') == 'another test'
57+
manifest.getAttributes('modid.mixins.json').getValue('Inherited-In-Remap-Jar') == '9'
5458
5559
where:
5660
version << STANDARD_TEST_VERSIONS

src/test/resources/projects/remapJarContents/build.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,21 @@ publishing {
7878
}
7979
}
8080

81+
jar {
82+
manifest {
83+
attributes 'Inherited-In-Remap-Jar': '1234'
84+
attributes(['Inherited-In-Remap-Jar': '5678'], 'fabric.mod.json') // category not present in remapJar
85+
attributes(['Inherited-In-Remap-Jar': '9'], 'modid.mixins.json') // category present in remapJar
86+
attributes 'Hello-World': 'shadowed in remapJar'
87+
}
88+
}
89+
8190
remapJar {
8291
from 'test_file.txt'
8392

8493
manifest {
8594
attributes 'Hello-World': 'test'
95+
attributes(['Hello-World': 'another test'], 'modid.mixins.json')
8696
}
8797
}
8898

0 commit comments

Comments
 (0)