8

I have a maven project with both Java and Scala components, but when I use maven-shade-plugin, it relocates package names for both Java and Scala files, but ONLY renames packages inside Java files, Scala files still contain the older package names, what am I missing?

               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-shade-plugin</artifactId>
               <version>3.2.1</version>
               <executions>
                   <execution>
                       <phase>package</phase>
                       <goals>
                           <goal>shade</goal>
                       </goals>
                       <configuration>
                           <!--<minimizeJar>true</minimizeJar>-->
                           <artifactSet>
                               <includes>
                                   <include>ml.dmlc:xgboost4j-spark</include>
                                   <include>ml.dmlc:xgboost4j</include>
                               </includes>
                           </artifactSet>
                           <filters>
                               <filter>
                                   <artifact>*:*</artifact>
                                   <excludes>
                                       <exclude>META-INF/*.SF</exclude>
                                       <exclude>META-INF/*.DSA</exclude>
                                       <exclude>META-INF/*.RSA</exclude>
                                   </excludes>
                               </filter>
                           </filters>
                           <relocations>
                               <relocation>
                                   <pattern>ml.dmlc.xgboost4j</pattern>
                                   <shadedPattern>ml.dmlc.xgboost4j.shaded</shadedPattern>
                               </relocation>
                           </relocations>
                           <transformers>
                           </transformers>
                       </configuration>
                   </execution>
               </executions>
           </plugin>```
Jas Bali
  • 241
  • 2
  • 10

2 Answers2

2

Sadly, I believe that Maven is intended to have this functionality but, currently (Dec 2020), it does not.

This can be seen with this bug ticket: https://issues.apache.org/jira/browse/MSHADE-345

workaround

I have personally done a silly workaround for this. I make a new empty mvn project that has the dependency:

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>28.0-jre</version>
</dependency>

And the plugin:

  <plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>3.1.1</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <relocations>
            <relocation>
              <pattern>com.google.</pattern>
              <shadedPattern>shader.com.google.</shadedPattern>
            </relocation>
          </relocations>
        </configuration>
      </execution>
    </executions>
  </plugin>

Then in the project with code that requires a low version of guava and a new version of guava, include the empty project as a dependency.

    <dependency>
        <groupId>com.yoursite.yourwork</groupId>
        <artifactId>shader</artifactId>
        <version>0.0.1</version>
    </dependency>

Then in a .scala file you import the new (version 28) guava classes like this:

import shader.com.google.common.graph.Network

Why this works

Since the error only occurs in scala projects where you refer to your own class that uses the dependency, or as said in the question "Scala files still contain the older package names", shading a project that does not refer to its own dependencies bypasses the bug.

Rorschach
  • 3,684
  • 7
  • 33
  • 77
-2

Yes, it does. Choose any build version you want and import the library into your Scala project.

Rex
  • 558
  • 2
  • 9
  • I don’t have sbt because I am using maven as a build tool and using maven Scala plugin with project structure following conventions src/main/java and src/main/scala – Jas Bali Mar 23 '19 at 20:32
  • org.apache.maven.plugins maven-shade-plugin 3.2.1 – Rex Mar 24 '19 at 02:30
  • 3
    I don’t think you read my question, i know how to use the plugin. I am doing all of that but package names inside scala files are not getting updated to the new shaded package names. – Jas Bali Mar 24 '19 at 03:50
  • maven-shade plugin is supposed to relocate all package references in a jar. This doesn't answer the question why some of the relocation fails – tribbloid Nov 22 '20 at 22:42