-
Notifications
You must be signed in to change notification settings - Fork 3
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
89 changes: 89 additions & 0 deletions
89
src/main/groovy/com/palantir/gradle/shadowjar/ComposableManifestAppenderTransformer.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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)