[英]Does maven-shade-plugin work with scala classes?
我有一個包含 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)番石榴類,如下所示:
import shader.com.google.common.graph.Network
為什么這有效
由於錯誤僅發生在您引用自己的使用依賴項的類的 Scala 項目中,或者如問題“Scala 文件仍然包含舊的包名稱”中所述,對不引用其自身依賴項的項目進行着色會繞過漏洞。
是的,它確實。 選擇您想要的任何構建版本並將庫導入您的 Scala 項目。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.