Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid empty lines in the middle of the manifest when shading a multi-release jar #77

Merged
merged 4 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog/@unreleased/pr-77.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type: fix
fix:
description: Previously, projects that shaded multi-release jars may have had an
empty line in the middle of their manifest, which would prevent the manifest from
being parsed. This has been removed.
links:
- https://github.com/palantir/gradle-shadow-jar/pull/77
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Change: Different package, added imports
package com.palantir.gradle.shadowjar

import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer
import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext
import shadow.org.apache.tools.zip.ZipOutputStream
import shadow.org.apache.tools.zip.ZipEntry
import shadow.org.codehaus.plexus.util.IOUtil
import org.gradle.api.file.FileTreeElement

import static java.nio.charset.StandardCharsets.*
import static java.util.jar.JarFile.*

// Originally taken from https://github.com/johnrengelman/shadow/blob/6.1.0/src/main/groovy/com/github/jengelman/
// gradle/plugins/shadow/transformers/ManifestAppenderTransformer.groovy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

surely we should just be PRing this upstream to mr johnrengelman?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to do this if you think that makes sense, though was a bit concerned as to timescales since the last thing was merged in early October!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll PR this upstream, but also think we should get this through here as we need it for atlasdb-proxy's migrations and I don't know how actively the shadow plugin is maintained (as mentioned, the last time anything was merged was in early October, and there is a 26 day old PR that has seen no response)

class ComposableManifestAppenderTransformer implements Transformer {
private static final byte[] EOL = "\r\n".getBytes(UTF_8)
private static final byte[] SEPARATOR = ": ".getBytes(UTF_8)

private byte[] manifestContents = []
private final List<Tuple2<String, ? extends Comparable<?>>> attributes = []

List<Tuple2<String, ? extends Comparable<?>>> getAttributes() { attributes }

ComposableManifestAppenderTransformer append(String name, Comparable<?> value) {
attributes.add(new Tuple2<String, ? extends Comparable<?>>(name, value))
this
}

@Override
boolean canTransformResource(FileTreeElement element) {
MANIFEST_NAME.equalsIgnoreCase(element.relativePath.pathString)
}

@Override
void transform(TransformerContext context) {
if (manifestContents.length == 0) {
manifestContents = IOUtil.toByteArray(context.is)
IOUtil.close(context.is)
}
}

@Override
boolean hasTransformedResource() {
!attributes.isEmpty()
}

@Override
void modifyOutputStream(ZipOutputStream os, boolean preserveFileTimestamps) {
ZipEntry entry = new ZipEntry(MANIFEST_NAME)
entry.time = TransformerContext.getEntryTimestamp(preserveFileTimestamps, entry.time)
os.putNextEntry(entry)
// Change: Trim existing file contents and add a single trailing newline
os.write(trimWhitespace(manifestContents))
os.write(EOL)

if (!attributes.isEmpty()) {
for (attribute in attributes) {
os.write(attribute.first.getBytes(UTF_8))
os.write(SEPARATOR)
os.write(attribute.second.toString().getBytes(UTF_8))
os.write(EOL)
}
os.write(EOL)
attributes.clear()
}
}

// Change: New method
static byte[] trimWhitespace(byte[] contents) {
return new String(contents, UTF_8).trim().getBytes(UTF_8);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.github.jengelman.gradle.plugins.shadow.relocation.RelocatePathContext;
import com.github.jengelman.gradle.plugins.shadow.relocation.SimpleRelocator;
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar;
import com.github.jengelman.gradle.plugins.shadow.transformers.ManifestAppenderTransformer;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
Expand Down Expand Up @@ -124,7 +123,7 @@ public final void run() {

if (!multiReleaseStuff.isEmpty()) {
try {
shadowJarTask.transform(ManifestAppenderTransformer.class, transformer -> {
shadowJarTask.transform(ComposableManifestAppenderTransformer.class, transformer -> {
// JEP 238 requires this manifest entry
transformer.append("Multi-Release", true);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ class ShadowJarPluginIntegrationSpec extends IntegrationSpec {
assert shadowJarFile().isMultiRelease() ?:
"The jar manifest must include 'Multi-Release: true', but was '" +
file("build/extractForAssertions/META-INF/MANIFEST.MF").text + "'"

// What is of interest here is that this does not throw an exception
shadowJarFile().getManifest()
}

def 'should shade known logging implementations iff it is placed in shadeTransitively directly'() {
Expand Down