scala - Maven-shade-plugin是否可以与scala类一起使用?

标签 scala maven maven-shade-plugin scala-maven-plugin

我有一个同时包含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>```

最佳答案

可悲的是,我相信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文件仍包含较早的程序包名称” 所述,请为未引用其依赖项的项目加上阴影绕过该错误。

关于scala - Maven-shade-plugin是否可以与scala类一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55309684/

相关文章:

java - 多模块项目中的奇怪编译器行为

java - 如何在多模块项目中配置 Maven shade 插件?

java - 如何阻止 maven-shade-plugin 阻止 opensaml-impl 类型的 java.util.ServiceLoader 初始化

java - 有选择地对已弃用的 JavaEE 模块使用第三方实现

java - spring MVC欢迎程序中Bean配置错误

java - 对第三方库使用 Maven 的阴影插件?

scala - 选择与 future

scala - 同时执行两个 fs2 任务(非确定性)

java - NiFi开发自定义处理器: import org. apache.nifi.processors错误

scala - 在 Scala 2 中,使用 .toSet 生成的 Set 类型推断失败?