[英]How do you replace the class of a Maven dependency?
maven依賴中有一個類與Java 8不兼容。
你如何正確解決這個問題?
現在我正在做以下事情:
問題是這個類包含對受限類的API調用,雖然我更改了Eclipse編譯器設置(Window - > Preferences - > Java - > Compiler - > Error / Warnings - > Deprecated and restricted API - > Forbidden reference(access rule) :錯誤 - >警告)允許訪問項目有時只會編譯。 如果它沒有編譯我會得到一個“找不到符號”的錯誤。
編輯:
以下是您要求的詳細信息:
依賴關系: http : //mvnrepository.com/artifact/com.sun.xml.wss/xws-security/3.0
類:EncryptionProcessor
必要的變化:
// Change line 1053 FROM: // _dataEncryptor = XMLCipher.getInstance(dataEncAlgo, _dataCipher); // TO: _dataEncryptor = XMLCipher.getInstance(dataEncAlgo);
編輯-2:
Maven構建錯誤:
[ERROR] symbol: class XMLCipher
[ERROR] location: class com.sun.xml.wss.impl.apachecrypto.EncryptionProcessor
[ERROR] /C:/Users/{name}/development/eclipse_workspace/git/xws-security/src/main/java/com/sun/xml/wss/impl/apachecrypto/EncryptionProcessor.java:[1482,98] cannot find symbol
這是一個詳細的指南,描述了我的確切做法:
配置新項目的Maven設置(重要:使用相同的組和工件ID,僅更改版本號)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sun.xml.wss</groupId> <artifactId>xws-security</artifactId> <version>3.0-java8-fix</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> </project>
添加有bug的JAR的依賴項
<dependencies> <dependency> <groupId>com.sun.xml.wss</groupId> <artifactId>xws-security</artifactId> <version>3.0</version> <exclusions> <exclusion> <artifactId>xmldsig</artifactId> <groupId>javax.xml.crypto</groupId> </exclusion> <exclusion> <artifactId>activation</artifactId> <groupId>javax.activation</groupId> </exclusion> </exclusions> </dependency> </dependencies>
在需要修復的同一類包中創建Java文件
package com.sun.xml.wss.impl.apachecrypto; public class EncryptionProcessor { // The FIX goes here }
添加Maven shade構建插件來處理修補的JAR文件的創建(這不是處理這種任務的唯一插件 - 例如依賴:unpack)
<build> <plugins> <!-- plug in for creation of patched JAR file --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <artifact>com.sun.xml.wss:xws-security:3.0</artifact> <includes> <include>**/*.class</include> <include>**/*.xml</include> </includes> <excludes> <exlude> com/sun/xml/wss/impl/apachecrypto/EncryptionProcessor.class </exlude> </excludes> </filter> </filters> </configuration> </execution> </executions> </plugin> </plugins> </build>
根據需要在其他項目中包含修補的JAR(注意:如果遇到ClassNotFoundExceptions或類似錯誤,請執行以下操作:右鍵單擊項目 - >屬性 - > Maven - >“從Workspace項目中解析依賴項”:false)
如果你不熟悉Maven。 這是完整的pom.xml: http : //pastebucket.com/88444
一般解決方案
pom.xml
版本,例如從3.0
到3.0-patched
類似於Steven S.的回答,但使用了maven-dependency-plugin 。 基於這篇博文 。
我更改了修補庫(不是版本)的名稱,但這取決於您的需求哪些更適合您。
對原始庫的依賴應標記為<optional>true</optional>
。 否則,依賴於修補庫的項目也將依賴於原始庫,這意味着修補版本和原始版本都將位於類路徑上,這可能導致各種問題。
如果您的項目是子項目,您仍然可以使用與父pom完全不同的groupId和版本。 無所謂。
你可以從解包中排除你修補的類,但它可能沒有必要,因為Maven將首先解壓縮原始庫然后編譯你的新版本,這意味着原始類被覆蓋。 太好了!
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<!-- remove this if you don't have a parent pom -->
<parent>
<groupId>my.company</groupId>
<artifactId>my.company</artifactId>
<version>1.2.3</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>com.foo</groupId>
<artifactId>foo-bar-patched</artifactId>
<version>4.5.6</version>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.foo</groupId>
<artifactId>foo-bar</artifactId>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<!-- excludes are probably not necessary -->
<!-- <excludes>**/Foo.class,**/Bar.class</excludes> -->
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.foo</groupId>
<artifactId>foo-bar</artifactId>
<version>4.5.6</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.