maven-shade-plugin能够处理scala类吗?

8

我有一个包含Java和Scala组件的Maven项目,但是当我使用maven-shade-plugin插件时,它对Java和Scala文件中的包名进行了重新定位,但仅重命名了Java文件内部的包名,Scala文件仍然包含旧的包名,我错过了什么?

               <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 - SatheeshJM
你找到解决方案了吗? - Grigoriev Nick
2个回答

2

很遗憾,我认为Maven旨在具有此功能,但当前(2020年12月)尚未实现。

可以通过以下错误票证明这一点: https://issues.apache.org/jira/browse/MSHADE-345

解决方法

我个人采用了一个愚蠢的解决方法。我创建一个新的空mvn项目,其中包含依赖项:

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

还有这个插件:

  <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>

在需要低版本的Guava和新版本的Guava代码项目中,将空项目作为依赖项包含进去。
    <dependency>
        <groupId>com.yoursite.yourwork</groupId>
        <artifactId>shader</artifactId>
        <version>0.0.1</version>
    </dependency>

然后在一个 .scala 文件中,您可以像这样导入新的(28版本)Guava类:

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

为什么这个方法有效

由于该错误只出现在引用使用依赖项的自己类的scala项目中,或如问题中所述"Scala文件仍包含旧的包名称",因此对不引用自己依赖项的项目进行阴影处理可以绕过该bug。


-2

是的,它可以。选择任何您想要的 构建版本,并将库导入到您的Scala项目中。


我没有使用sbt,因为我正在使用maven作为构建工具,并使用maven Scala插件,项目结构遵循src/main/java和src/main/scala的约定。 - Jas Bali
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-shade-plugin --> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.1</version> </dependency> - Rex
3
我不认为您读懂了我的问题,我知道如何使用插件。我正在执行所有这些操作,但是在Scala文件中的包名称没有更新为新的Shaded包名称。 - Jas Bali
maven-shade插件应该重新定位jar包中的所有包引用。这并不能回答为什么有些重定位失败的问题。 - tribbloid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接