[ https://issues.apache.org/jira/browse/MSHADE-345?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17684326#comment-17684326 ]
Javier Neira Sanchez edited comment on MSHADE-345 at 2/6/23 8:22 AM: --------------------------------------------------------------------- Hi! i am trying to reproduce the issue but i am not able to do it. >From my tests i would say that the plugin is relocating scala code correctly. {{package}} sentences are not included int he bytecode when compiling any jvm lang to bytecode. So i think that maybe the ide is interfering in the result and it is showing a wrong package, cause it still think the class is the original and not the shaded one. In fact my ide is showing me a similar decompiled class file with the wrong package. My tests so far (i've upload them here: [https://github.com/jneira/maven-shade-plugin/tree/SHADE-345_scala-package-relocation/src/it/projects/MSHADE-345_scala-package-relocation] ) * I have this pom.xml: {noformat} <project> <modelVersion>4.0.0</modelVersion> <groupId>org.apache.maven.its.shade.rie</groupId> <artifactId>test</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>MSHADE-345</name> <description> Test handling scala package relocation. </description> <dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api-scala_2.11</artifactId> <version>12.0</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api-scala_2.12</artifactId> <version>12.0</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api-scala_2.13</artifactId> <version>12.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <id>attach-shade-211</id> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <shadedArtifactAttached>false</shadedArtifactAttached> <artifactSet> <includes> <include>org.apache.logging.log4j:log4j-api-scala_2.11</include> <include>org.scala-lang:scala-library</include> </includes> </artifactSet> <relocations> <relocation> <pattern>org.apache.logging.log4j.scala</pattern> <shadedPattern>relocated.scala211</shadedPattern> </relocation> <relocation> <pattern>scala</pattern> <shadedPattern>relocated.scala211</shadedPattern> </relocation> </relocations> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> {noformat} * And i have a Main class using the classes being relocated: {noformat} import org.apache.logging.log4j.scala.Logger; import org.apache.logging.log4j.scala.Logging; import org.apache.logging.log4j.spi.ExtendedLogger; public class Main { public static void main(String[] args) { System.out.println("Test" + Logger.FQCN()); Test t = new Test(); System.out.println("Logger: " + t.logger()); } public static class Test implements Logging{ public ExtendedLogger logger() { return null; } public void org$apache$logging$log4j$scala$Logging$_setter_$logger_$eq(ExtendedLogger arg0) { return; } } } {noformat} * The bytecode genetated for Main.class inside the shaded jar is: {noformat} Compiled from "Main.java" public class Main { public Main(); Code: 0: aload_0 1: invokespecial #12 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: getstatic #22 // Field java/lang/System.out:Ljava/io/PrintStream; 3: new #24 // class java/lang/StringBuilder 6: dup 7: invokespecial #25 // Method java/lang/StringBuilder."<init>":()V 10: ldc #26 // String Test 12: invokevirtual #30 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 15: invokestatic #36 // Method relocated/scala211/Logger.FQCN:()Ljava/lang/String; 18: invokevirtual #30 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 21: invokevirtual #39 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; 24: invokevirtual #45 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 27: new #7 // class Main$Test 30: dup 31: invokespecial #46 // Method Main$Test."<init>":()V 34: astore_1 35: getstatic #22 // Field java/lang/System.out:Ljava/io/PrintStream; 38: new #24 // class java/lang/StringBuilder 41: dup 42: invokespecial #25 // Method java/lang/StringBuilder."<init>":()V 45: ldc #48 // String Logger: 47: invokevirtual #30 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 50: aload_1 51: invokevirtual #52 // Method Main$Test.logger:()Lorg/apache/logging/log4j/spi/ExtendedLogger; 54: invokevirtual #55 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder; 57: invokevirtual #39 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; 60: invokevirtual #45 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 63: return }{noformat} As you can check in the bytecode {{org.apache.logging.log4j.scala.Logger.FQCN()}} has been relocated to {{Method relocated/scala211/Logger.FQCN)Ljava/lang/String;}} Moreover the execution of the main class is succesful and the output is: {noformat} Test: relocated.scala211.Logger$ Logger: null{noformat} As {{org.apache.logging.log4j.scala.Logger.FQCN() }}stands for{{ this.getClass().getName()}} [~programmatix] to try to reproduce the issue, could you extend on the experiments which confirm the relocotaion was wrong, apart the ide output? thanks! was (Author: jneira): Hi! i am trying to reproduce the issue but i am not able to do it. >From my tests i would say that the plugin is relocating scala code correctly. {{package}} sentences are not included int he bytecode when compiling any jvm lang to bytecode. So i think that maybe the ide is interfering in the result and it is showing a wrong package, cause it still think the class is the original and not the shaded one. In fact my ide is showing me a similar decompiled class file with the wrong package. My tests so far (i've upload them here: [https://github.com/jneira/maven-shade-plugin/tree/SHADE-345_scala-package-relocation/src/it/projects/MSHADE-345_scala-package-relocation] ) * I have this pom.xml: {noformat} <project> <modelVersion>4.0.0</modelVersion> <groupId>org.apache.maven.its.shade.rie</groupId> <artifactId>test</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>MSHADE-345</name> <description> Test handling scala package relocation. </description> <dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api-scala_2.11</artifactId> <version>12.0</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api-scala_2.12</artifactId> <version>12.0</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api-scala_2.13</artifactId> <version>12.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <id>attach-shade-211</id> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <shadedArtifactAttached>false</shadedArtifactAttached> <artifactSet> <includes> <include>org.apache.logging.log4j:log4j-api-scala_2.11</include> <include>org.scala-lang:scala-library</include> </includes> </artifactSet> <relocations> <relocation> <pattern>org.apache.logging.log4j.scala</pattern> <shadedPattern>relocated.scala211</shadedPattern> </relocation> <relocation> <pattern>scala</pattern> <shadedPattern>relocated.scala211</shadedPattern> </relocation> </relocations> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> {noformat} * And i have a Main class using the classes being relocated: {noformat} import org.apache.logging.log4j.scala.Logger; import org.apache.logging.log4j.scala.Logging; import org.apache.logging.log4j.spi.ExtendedLogger; public class Main { public static void main(String[] args) { System.out.println("Test" + Logger.FQCN()); Test t = new Test(); System.out.println("Logger: " + t.logger()); } public static class Test implements Logging{ public ExtendedLogger logger() { return null; } public void org$apache$logging$log4j$scala$Logging$_setter_$logger_$eq(ExtendedLogger arg0) { return; } } } {noformat} * The bytecode genetated for Main.class inside the shaded jar is: {noformat} Compiled from "Main.java" public class Main { public Main(); Code: 0: aload_0 1: invokespecial #12 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: getstatic #22 // Field java/lang/System.out:Ljava/io/PrintStream; 3: new #24 // class java/lang/StringBuilder 6: dup 7: invokespecial #25 // Method java/lang/StringBuilder."<init>":()V 10: ldc #26 // String Test 12: invokevirtual #30 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 15: invokestatic #36 // Method relocated/scala211/Logger.FQCN:()Ljava/lang/String; 18: invokevirtual #30 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 21: invokevirtual #39 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; 24: invokevirtual #45 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 27: new #7 // class Main$Test 30: dup 31: invokespecial #46 // Method Main$Test."<init>":()V 34: astore_1 35: getstatic #22 // Field java/lang/System.out:Ljava/io/PrintStream; 38: new #24 // class java/lang/StringBuilder 41: dup 42: invokespecial #25 // Method java/lang/StringBuilder."<init>":()V 45: ldc #48 // String Logger: 47: invokevirtual #30 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 50: aload_1 51: invokevirtual #52 // Method Main$Test.logger:()Lorg/apache/logging/log4j/spi/ExtendedLogger; 54: invokevirtual #55 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder; 57: invokevirtual #39 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; 60: invokevirtual #45 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 63: return }{noformat} As you can check in the bytecode {{org.apache.logging.log4j.scala.Logger.FQCN()}} has been relocated to {{Method relocated/scala211/Logger.FQCN)Ljava/lang/String;}} Moreover the execution of the main class is succesful and the output is: {noformat} Test: relocated.scala211.Logger$ Logger: null{noformat} As {{org.apache.logging.log4j.scala.Logger.FQCN() }}stands for this.getClass().getName()}} [~programmatix] to try to reproduce the issue, could you extend on the experiments which confirm the relocotaion was wrong, apart the ide output? thanks! > Package relocation appears not to work with Scala > ------------------------------------------------- > > Key: MSHADE-345 > URL: https://issues.apache.org/jira/browse/MSHADE-345 > Project: Maven Shade Plugin > Issue Type: Bug > Reporter: Graham Pople > Priority: Major > Attachments: image (4).png > > > (Apologies if this is a known issue, I searched but couldn't find it.) > I have a small pom.xml that's simply pulling in two dependent libraries from > Maven, and shading them: > {code:java} > <?xml version="1.0" encoding="UTF-8"?> > <project xmlns="http://maven.apache.org/POM/4.0.0" > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 > http://maven.apache.org/maven-v4_0_0.xsd"> > <modelVersion>4.0.0</modelVersion> > <groupId>com.couchbase.client</groupId> > <artifactId>scala-deps</artifactId> > <version>1.1.1-SNAPSHOT</version> > <packaging>jar</packaging> > <name>Couchbase Scala Dependencies</name> > <description>Shadowing dependencies for Scala prokect</description> > <dependencies> > <dependency> > <groupId>com.github.plokhotnyuk.jsoniter-scala</groupId> > <artifactId>jsoniter-scala-core_2.12</artifactId> > <version>1.0.0</version> > </dependency> > <dependency> > <groupId>com.github.plokhotnyuk.jsoniter-scala</groupId> > <artifactId>jsoniter-scala-macros_2.12</artifactId> > <version>1.0.0</version> > </dependency> > </dependencies> > <build> > <plugins> > <plugin> > <groupId>org.apache.maven.plugins</groupId> > <artifactId>maven-shade-plugin</artifactId> > <version>3.2.1</version> > <configuration> > <relocations> > <relocation> > <pattern>com.github.plokhotnyuk</pattern> > > <shadedPattern>com.couchbase.client.scala.deps.plokhotnyuk</shadedPattern> > </relocation> > </relocations> > <createSourcesJar>true</createSourcesJar> > <shadeSourcesContent>true</shadeSourcesContent> > <shadedArtifactAttached>true</shadedArtifactAttached> > </configuration> > <executions> > <execution> > <phase>package</phase> > <goals> > <goal>shade</goal> > </goals> > </execution> > </executions> > </plugin> > </plugins> > </build> > </project> {code} > From this I expect to get an uber-jar containing those dependencies, with > classfiles inside > com/couchbase/client/scala/deps/plokhotnyuk/jsoniter_scala/..., and with > those files starting "package com.couchbase.client.scala.deps.plokhotnyuk...". > However, while the files are moved to the correct file structure, the package > names aren't adjusted. Here's a screenshot from the sources jar: > !image (4).png! > > And my experiments with the binary jar itself seem to indicate that the same > issue exists at the bytecode level in the regular jar too, e.g. the > classfiles are in the right place but the package hasn't been changed. > -- This message was sent by Atlassian Jira (v8.20.10#820010)