6

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

2 回答 2

1

可悲的是,我相信 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)番石榴类,如下所示:

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

为什么这有效

由于错误仅发生在您引用自己的使用依赖项的类的 scala 项目中,或者如问题“Scala files still contains old package names”中所述,对不引用其自身依赖项的项目进行着色会绕过漏洞。

于 2020-12-29T05:13:41.937 回答
-2

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

于 2019-03-23T03:25:05.230 回答