Skip to content
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
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugins</artifactId>
<version>42</version>
<version>45</version>
<relativePath />
</parent>

Expand Down Expand Up @@ -116,6 +116,7 @@
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.15.1</version>
<scope>provided</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ private void shadeJars(
}
}

@SuppressWarnings("checkstyle:ParameterNumber")
private void shadeDir(
ShadeRequest shadeRequest,
Set<String> resources,
Expand Down Expand Up @@ -320,6 +321,7 @@ public InputStream call() throws Exception {
}
}

@SuppressWarnings("checkstyle:ParameterNumber")
private void shadeJar(
ShadeRequest shadeRequest,
Set<String> resources,
Expand Down Expand Up @@ -381,6 +383,7 @@ private boolean isExcludedEntry(final String name) {
return false;
}

@SuppressWarnings("checkstyle:ParameterNumber")
private void shadeJarEntry(
ShadeRequest shadeRequest,
Set<String> resources,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ public class ShadeMojo extends AbstractMojo {
/**
* @throws MojoExecutionException in case of an error.
*/
@SuppressWarnings("checkstyle:methodlength")
@Override
public void execute() throws MojoExecutionException {
if (skip) {
Expand Down
91 changes: 46 additions & 45 deletions src/test/java/org/apache/maven/plugins/shade/DefaultShaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
Expand Down Expand Up @@ -93,37 +94,37 @@ public class DefaultShaderTest {
new String[] {"org/codehaus/plexus/util/xml/Xpp3Dom", "org/codehaus/plexus/util/xml/pull.*"};

@ClassRule
public static final TemporaryFolder tmp = new TemporaryFolder();
public static final TemporaryFolder TEMPORARY_FOLDER = new TemporaryFolder();

private final String NEWLINE = "\n";
private static final String NEWLINE = "\n";

@Test
public void testNoopWhenNotRelocated() throws IOException, MojoExecutionException {
final File plexusJar = new File("src/test/jars/plexus-utils-1.4.1.jar");
final File shadedOutput = new File("target/foo-custom_testNoopWhenNotRelocated.jar");
File plexusJar = new File("src/test/jars/plexus-utils-1.4.1.jar");
File shadedOutput = new File("target/foo-custom_testNoopWhenNotRelocated.jar");

final Set<File> jars = new LinkedHashSet<>();
Set<File> jars = new LinkedHashSet<>();
jars.add(new File("src/test/jars/test-project-1.0-SNAPSHOT.jar"));
jars.add(plexusJar);

final Relocator relocator = new SimpleRelocator(
Relocator relocator = new SimpleRelocator(
"org/codehaus/plexus/util/cli",
"relocated/plexus/util/cli",
Collections.<String>emptyList(),
Collections.<String>emptyList());
Collections.emptyList(),
Collections.emptyList());

final ShadeRequest shadeRequest = new ShadeRequest();
ShadeRequest shadeRequest = new ShadeRequest();
shadeRequest.setJars(jars);
shadeRequest.setRelocators(Collections.singletonList(relocator));
shadeRequest.setResourceTransformers(Collections.<ResourceTransformer>emptyList());
shadeRequest.setFilters(Collections.<Filter>emptyList());
shadeRequest.setResourceTransformers(Collections.emptyList());
shadeRequest.setFilters(Collections.emptyList());
shadeRequest.setUberJar(shadedOutput);

final DefaultShader shader = newShader();
DefaultShader shader = newShader();
shader.shade(shadeRequest);

try (final JarFile originalJar = new JarFile(plexusJar);
final JarFile shadedJar = new JarFile(shadedOutput)) {
try (JarFile originalJar = new JarFile(plexusJar);
JarFile shadedJar = new JarFile(shadedOutput)) {
// ASM processes all class files. In doing so, it modifies them, even when not relocating anything.
// Before MSHADE-391, the processed files were written to the uber JAR, which did no harm, but made it
// difficult to find out by simple file comparison, if a file was actually relocated or not. Now, Shade
Expand All @@ -141,7 +142,7 @@ public void testNoopWhenNotRelocated() throws IOException, MojoExecutionExceptio
"relocated/plexus/util/cli/Arg.class"));
}
int result = 0;
for (final String msg : debugMessages.getAllValues()) {
for (String msg : debugMessages.getAllValues()) {
if ("Rewrote class bytecode: org/codehaus/plexus/util/cli/Arg.class".equals(msg)) {
result |= 1;
} else if ("Keeping original class bytecode: org/codehaus/plexus/util/Expand.class".equals(msg)) {
Expand All @@ -153,15 +154,15 @@ public void testNoopWhenNotRelocated() throws IOException, MojoExecutionExceptio

@Test
public void testOverlappingResourcesAreLogged() throws IOException, MojoExecutionException {
final DefaultShader shader = newShader();
DefaultShader shader = newShader();

// we will shade two jars and expect to see META-INF/MANIFEST.MF overlaps, this will always be true
// but this can lead to a broken deployment if intended for OSGi or so, so even this should be logged
final Set<File> set = new LinkedHashSet<>();
Set<File> set = new LinkedHashSet<>();
set.add(new File("src/test/jars/test-project-1.0-SNAPSHOT.jar"));
set.add(new File("src/test/jars/plexus-utils-1.4.1.jar"));

final ShadeRequest shadeRequest = new ShadeRequest();
ShadeRequest shadeRequest = new ShadeRequest();
shadeRequest.setJars(set);
shadeRequest.setRelocators(Collections.<Relocator>emptyList());
shadeRequest.setResourceTransformers(Collections.<ResourceTransformer>emptyList());
Expand Down Expand Up @@ -215,9 +216,9 @@ public void testOverlappingResourcesAreLoggedExceptATransformerHandlesIt() throw

ShadeRequest shadeRequest = new ShadeRequest();
shadeRequest.setJars(set);
shadeRequest.setRelocators(Collections.<Relocator>emptyList());
shadeRequest.setResourceTransformers(Collections.<ResourceTransformer>singletonList(transformer));
shadeRequest.setFilters(Collections.<Filter>emptyList());
shadeRequest.setRelocators(Collections.emptyList());
shadeRequest.setResourceTransformers(Collections.singletonList(transformer));
shadeRequest.setFilters(Collections.emptyList());
shadeRequest.setUberJar(new File("target/foo-custom_testOverlappingResourcesAreLogged.jar"));

DefaultShader shaderWithTransformer = newShader();
Expand All @@ -226,7 +227,7 @@ public void testOverlappingResourcesAreLoggedExceptATransformerHandlesIt() throw
assertThat(warnMessages.getAllValues().size(), is(0));

DefaultShader shaderWithoutTransformer = newShader();
shadeRequest.setResourceTransformers(Collections.<ResourceTransformer>emptyList());
shadeRequest.setResourceTransformers(Collections.emptyList());
shaderWithoutTransformer.shade(shadeRequest);

assertThat(
Expand Down Expand Up @@ -292,33 +293,33 @@ public void testShaderWithoutExcludesShouldRemoveReferencesOfOriginalPattern() t

@Test
public void testHandleDirectory() throws Exception {
final File dir = tmp.getRoot();
final File dir = TEMPORARY_FOLDER.getRoot();
// explode src/test/jars/test-artifact-1.0-SNAPSHOT.jar in this temp dir
try (final JarInputStream in =
new JarInputStream(new FileInputStream("src/test/jars/test-artifact-1.0-SNAPSHOT.jar"))) {
try (JarInputStream in =
new JarInputStream(Files.newInputStream(Paths.get("src/test/jars/test-artifact-1.0-SNAPSHOT.jar")))) {
JarEntry nextJarEntry;
while ((nextJarEntry = in.getNextJarEntry()) != null) {
if (nextJarEntry.isDirectory()) {
continue;
}
final File out = new File(dir, nextJarEntry.getName());
File out = new File(dir, nextJarEntry.getName());
forceMkdir(out.getParentFile());
try (final OutputStream outputStream = new FileOutputStream(out)) {
try (OutputStream outputStream = Files.newOutputStream(out.toPath())) {
IOUtil.copy(in, outputStream, (int) Math.max(nextJarEntry.getSize(), 512));
}
}
}

// do shade
final File shade = new File("target/testHandleDirectory.jar");
File shade = new File("target/testHandleDirectory.jar");
shaderWithPattern("org/shaded/plexus/util", shade, new String[0], singleton(dir));

// ensure directory was shaded properly
try (final JarFile jar = new JarFile(shade)) {
final List<String> entries = new ArrayList<>();
final Enumeration<JarEntry> jarEntryEnumeration = jar.entries();
try (JarFile jar = new JarFile(shade)) {
List<String> entries = new ArrayList<>();
Enumeration<JarEntry> jarEntryEnumeration = jar.entries();
while (jarEntryEnumeration.hasMoreElements()) {
final JarEntry jarEntry = jarEntryEnumeration.nextElement();
JarEntry jarEntry = jarEntryEnumeration.nextElement();
if (jarEntry.isDirectory()) {
continue;
}
Expand Down Expand Up @@ -346,8 +347,8 @@ public void testShaderWithRelocatedClassname() throws Exception {

List<Relocator> relocators = new ArrayList<>();

relocators.add(new SimpleRelocator(
"org/codehaus/plexus/util/", "_plexus/util/__", null, Collections.<String>emptyList()));
relocators.add(
new SimpleRelocator("org/codehaus/plexus/util/", "_plexus/util/__", null, Collections.emptyList()));

List<ResourceTransformer> resourceTransformers = new ArrayList<>();

Expand All @@ -373,8 +374,8 @@ public void testShaderWithRelocatedClassname() throws Exception {
assertEquals("", c.getMethod("clean", String.class).invoke(o, (String) null));

// now, check that its source file was rewritten:
final String[] source = {null};
final ClassReader classReader = new ClassReader(cl.getResourceAsStream("_plexus/util/__StringUtils.class"));
String[] source = {null};
ClassReader classReader = new ClassReader(cl.getResourceAsStream("_plexus/util/__StringUtils.class"));
classReader.accept(
new ClassVisitor(Opcodes.ASM4) {
@Override
Expand Down Expand Up @@ -448,9 +449,9 @@ public void testShaderNoOverwrite() throws Exception {

ShadeRequest shadeRequest = new ShadeRequest();
shadeRequest.setJars(new LinkedHashSet<>(Collections.singleton(outerJar)));
shadeRequest.setFilters(new ArrayList<Filter>());
shadeRequest.setRelocators(new ArrayList<Relocator>());
shadeRequest.setResourceTransformers(new ArrayList<ResourceTransformer>());
shadeRequest.setFilters(new ArrayList<>());
shadeRequest.setRelocators(new ArrayList<>());
shadeRequest.setResourceTransformers(new ArrayList<>());
File shadedFile = temporaryFolder.newFile("shaded.jar");
shadeRequest.setUberJar(shadedFile);

Expand Down Expand Up @@ -522,7 +523,7 @@ public void testShaderWithSmallEntries() throws Exception {

temporaryFolder.create();
File innerJar = temporaryFolder.newFile(innerJarFileName);
try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(innerJar))) {
try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(innerJar.toPath()))) {
jos.putNextEntry(new JarEntry("foo.txt"));
byte[] bytes = "c1".getBytes(StandardCharsets.UTF_8);
len = bytes.length;
Expand All @@ -532,9 +533,9 @@ public void testShaderWithSmallEntries() throws Exception {

ShadeRequest shadeRequest = new ShadeRequest();
shadeRequest.setJars(new LinkedHashSet<>(Collections.singleton(innerJar)));
shadeRequest.setFilters(new ArrayList<Filter>());
shadeRequest.setRelocators(new ArrayList<Relocator>());
shadeRequest.setResourceTransformers(new ArrayList<ResourceTransformer>());
shadeRequest.setFilters(new ArrayList<>());
shadeRequest.setRelocators(new ArrayList<>());
shadeRequest.setResourceTransformers(new ArrayList<>());
File shadedFile = temporaryFolder.newFile("shaded.jar");
shadeRequest.setUberJar(shadedFile);

Expand Down Expand Up @@ -620,9 +621,9 @@ private boolean areEqual(final JarFile jar1, final JarFile jar2, final String en

private boolean areEqual(final JarFile jar1, final JarFile jar2, final String entry1, String entry2)
throws IOException {
try (final InputStream s1 = jar1.getInputStream(
try (InputStream s1 = jar1.getInputStream(
requireNonNull(jar1.getJarEntry(entry1), entry1 + " in " + jar1.getName()));
final InputStream s2 = jar2.getInputStream(
InputStream s2 = jar2.getInputStream(
requireNonNull(jar2.getJarEntry(entry2), entry2 + " in " + jar2.getName()))) {
return Arrays.equals(IOUtil.toByteArray(s1), IOUtil.toByteArray(s2));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void testRelocateMavenFiles() {
assertTrue(relocator.canRelocatePath("META-INF/maven/com-foo-bar/artifactId/pom.xml"));
}

private static final String sourceFile = "package org.apache.maven.hello;\n" + "package org.objectweb.asm;\n"
private static final String SOURCE_FILE = "package org.apache.maven.hello;\n" + "package org.objectweb.asm;\n"
+ "\n"
+ "import foo.bar.Bar;\n"
+ "import zot.baz.Baz;\n"
Expand Down Expand Up @@ -216,7 +216,7 @@ public void testRelocateMavenFiles() {
+ " }\n"
+ "}\n";

private static final String relocatedFile = "package com.acme.maven.hello;\n" + "package aj.org.objectweb.asm;\n"
private static final String RELOCATED_FILE = "package com.acme.maven.hello;\n" + "package aj.org.objectweb.asm;\n"
+ "\n"
+ "import foo.bar.Bar;\n"
+ "import zot.baz.Baz;\n"
Expand Down Expand Up @@ -257,7 +257,7 @@ public void testRelocateSourceWithExcludesRaw() {
Arrays.asList("foo.bar", "zot.baz"),
Arrays.asList("irrelevant.exclude", "org.apache.maven.exclude1", "org.apache.maven.sub.exclude2"),
true);
assertEquals(sourceFile, relocator.applyToSourceContent(sourceFile));
assertEquals(SOURCE_FILE, relocator.applyToSourceContent(SOURCE_FILE));
}

@Test
Expand All @@ -275,8 +275,8 @@ public void testRelocateSourceWithExcludes() {
// Make sure not to replace 'foo' package by path-like 'shaded/foo'
SimpleRelocator fooRelocator = new SimpleRelocator("foo", "shaded.foo", null, Arrays.asList("foo.bar"));
assertEquals(
relocatedFile,
RELOCATED_FILE,
fooRelocator.applyToSourceContent(asmRelocator.applyToSourceContent(
ioRelocator.applyToSourceContent(relocator.applyToSourceContent(sourceFile)))));
ioRelocator.applyToSourceContent(relocator.applyToSourceContent(SOURCE_FILE)))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public void rewriteDefaultAttributes() throws Exception {

final ByteArrayOutputStream out = transform(manifest, relocators);

try (final JarInputStream jis = new JarInputStream(new ByteArrayInputStream(out.toByteArray()))) {
final Attributes attrs = jis.getManifest().getMainAttributes();
try (JarInputStream jis = new JarInputStream(new ByteArrayInputStream(out.toByteArray()))) {
Attributes attrs = jis.getManifest().getMainAttributes();
assertEquals(
"jakarta.decorator;version=\"2.0\";uses:=\"jakarta.enterprise.inject\","
+ "jakarta.enterprise.context;version=\"2.0\";uses:=\"jakarta.enterprise.util,"
Expand Down Expand Up @@ -131,22 +131,22 @@ public void rewriteAdditionalAttributes() throws Exception {
transformer.setAdditionalAttributes(Arrays.asList("description-custom", "attribute-unknown"));
final ByteArrayOutputStream out = transform(manifest, relocators);

try (final JarInputStream jis = new JarInputStream(new ByteArrayInputStream(out.toByteArray()))) {
final Attributes attrs = jis.getManifest().getMainAttributes();
try (JarInputStream jis = new JarInputStream(new ByteArrayInputStream(out.toByteArray()))) {
Attributes attrs = jis.getManifest().getMainAttributes();
assertEquals("This jar uses jakarta packages", attrs.getValue("description-custom"));
}
}

private ByteArrayOutputStream transform(final Manifest manifest, List<Relocator> relocators) throws IOException {
final ByteArrayOutputStream mboas = new ByteArrayOutputStream();
try (final OutputStream mos = mboas) {
try (OutputStream mos = mboas) {
manifest.write(mos);
}
transformer.processResource(
JarFile.MANIFEST_NAME, new ByteArrayInputStream(mboas.toByteArray()), relocators, 0);

final ByteArrayOutputStream out = new ByteArrayOutputStream();
try (final JarOutputStream jarOutputStream = new JarOutputStream(out)) {
try (JarOutputStream jarOutputStream = new JarOutputStream(out)) {
transformer.modifyOutputStream(jarOutputStream);
}
return out;
Expand Down
Loading
Loading