Skip to content

Commit 99528f7

Browse files
committed
Trim the last EOL in AppendingTransformer processing
This is useful for supporting the custom line separator in the future. In that case we can handle `resources/application.yml` files, that are separated by `---`. I did the same patch for Gradle Shadow plugin in GradleUp/shadow@8300f91.
1 parent 3a844d0 commit 99528f7

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/main/java/org/apache/maven/plugins/shade/resource/AppendingTransformer.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ public boolean canTransformResource(String r) {
4646
@Override
4747
public void processResource(String resource, InputStream is, List<Relocator> relocators, long time)
4848
throws IOException {
49+
if (data.size() > 0) {
50+
// Append the EOL before the new content to ensure the EOL is not at the end of the file.
51+
data.write('\n');
52+
}
4953
IOUtil.copy(is, data);
50-
data.write('\n');
5154
if (time > this.time) {
5255
this.time = time;
5356
}

src/test/java/org/apache/maven/plugins/shade/resource/AppendingTransformerTest.java

+41
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@
1818
*/
1919
package org.apache.maven.plugins.shade.resource;
2020

21+
import java.io.ByteArrayInputStream;
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.util.Collections;
2126
import java.util.Locale;
27+
import java.util.jar.JarInputStream;
28+
import java.util.jar.JarOutputStream;
2229

2330
import org.junit.Before;
2431
import org.junit.Test;
2532

33+
import static org.junit.Assert.assertEquals;
2634
import static org.junit.Assert.assertFalse;
2735
import static org.junit.Assert.assertTrue;
2836

@@ -56,4 +64,37 @@ public void testCanTransformResource() {
5664
assertTrue(transformer.canTransformResource("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
5765
assertFalse(transformer.canTransformResource("META-INF/MANIFEST.MF"));
5866
}
67+
68+
@Test
69+
public void testProcessResource() throws IOException {
70+
transformer.resource = "test-resource";
71+
String firstLine = "first line";
72+
String secondLine = "second line";
73+
InputStream firstIs = new ByteArrayInputStream(firstLine.getBytes());
74+
InputStream secondIs = new ByteArrayInputStream(secondLine.getBytes());
75+
76+
transformer.processResource("", firstIs, Collections.emptyList());
77+
transformer.processResource("", secondIs, Collections.emptyList());
78+
79+
final ByteArrayOutputStream out = new ByteArrayOutputStream();
80+
try (final JarOutputStream jarOutputStream = new JarOutputStream(out)) {
81+
transformer.modifyOutputStream(jarOutputStream);
82+
}
83+
84+
try (final JarInputStream jis = new JarInputStream(new ByteArrayInputStream(out.toByteArray()))) {
85+
assertEquals("test-resource", jis.getNextJarEntry().getName());
86+
String result = read(jis);
87+
assertEquals(firstLine + "\n" + secondLine, result);
88+
}
89+
}
90+
91+
private String read(final JarInputStream jar) throws IOException {
92+
final StringBuilder builder = new StringBuilder();
93+
final byte[] buffer = new byte[512];
94+
int read;
95+
while ((read = jar.read(buffer)) >= 0) {
96+
builder.append(new String(buffer, 0, read));
97+
}
98+
return builder.toString();
99+
}
59100
}

0 commit comments

Comments
 (0)