如何替换 Maven 依赖项的 class?
How do you replace the class of a Maven dependency?
maven 依赖中有一个 class 与 Java 不兼容 8.
您如何正确解决该问题?
现在我正在做以下事情:
- 创建同名包
- 在那个包中创建一个同名的class
- 复制并粘贴代码
- 修复不兼容的 API 调用
问题是此 class 包含 API 对受限 class 的调用,尽管我更改了 Eclipse 编译器设置(Window -> 首选项 -> Java -> Compiler -> Error/Warnings -> Deprecated and restricted API -> Forbidden reference (access rule: Error -> Warning) to allow access 项目有时只会编译。如果它没有编译,我会得到一个 "can't find symbol" 错误。
编辑:
这是您要求的详细信息:
依赖关系:http://mvnrepository.com/artifact/com.sun.xml.wss/xws-security/3.0
Class:加密处理器
必要的改动:
// 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
一般解决方案:
- 下载所有项目源
- 应用您的修改
- 使用版本控制,这样更改就不会丢失
- 更改
pom.xml
中的版本,例如从 3.0
到 3.0-patched
- 启动 Maven 构建
- 复制生成的工件给你repository/Artifactory,如果你使用一个
- 在自己的项目中更改依赖版本
这里有一份详细的指南,描述了我所做的事情:
- 在 Eclipse 中新建 Maven 项目
配置新项目的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>
添加错误 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>
在需要修复的class同一个包中创建Java文件
package com.sun.xml.wss.impl.apachecrypto;
public class EncryptionProcessor {
// The FIX goes here
}
添加 Maven shade 构建插件来处理修补 JAR 文件的创建(这不是处理此类任务的唯一插件 - 例如 dependency: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 -> "Resolve dependencies from Workspace projects" :false)
以防您不熟悉 Maven。这是完整的 pom.xml:http://pastebucket.com/88444
类似于 Steven S. 的回答,但使用 maven-dependency-plugin. Based on this blog post。
我更改了补丁库的名称(不是版本),但这取决于您的需要,哪个更适合您。
对原库的依赖应该标记为<optional>true</optional>
。否则,依赖你的补丁库的项目也将依赖于原始库,这意味着补丁版本和原始版本都将在类路径中,这可能会导致各种问题。
如果您的项目是子项目,您仍然可以使用与父项目完全不同的 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>
maven 依赖中有一个 class 与 Java 不兼容 8.
您如何正确解决该问题?
现在我正在做以下事情:
- 创建同名包
- 在那个包中创建一个同名的class
- 复制并粘贴代码
- 修复不兼容的 API 调用
问题是此 class 包含 API 对受限 class 的调用,尽管我更改了 Eclipse 编译器设置(Window -> 首选项 -> Java -> Compiler -> Error/Warnings -> Deprecated and restricted API -> Forbidden reference (access rule: Error -> Warning) to allow access 项目有时只会编译。如果它没有编译,我会得到一个 "can't find symbol" 错误。
编辑:
这是您要求的详细信息:
依赖关系:http://mvnrepository.com/artifact/com.sun.xml.wss/xws-security/3.0
Class:加密处理器
必要的改动:
// 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
一般解决方案:
- 下载所有项目源
- 应用您的修改
- 使用版本控制,这样更改就不会丢失
- 更改
pom.xml
中的版本,例如从3.0
到3.0-patched
- 启动 Maven 构建
- 复制生成的工件给你repository/Artifactory,如果你使用一个
- 在自己的项目中更改依赖版本
这里有一份详细的指南,描述了我所做的事情:
- 在 Eclipse 中新建 Maven 项目
配置新项目的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>
添加错误 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>
在需要修复的class同一个包中创建Java文件
package com.sun.xml.wss.impl.apachecrypto; public class EncryptionProcessor { // The FIX goes here }
添加 Maven shade 构建插件来处理修补 JAR 文件的创建(这不是处理此类任务的唯一插件 - 例如 dependency: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 -> "Resolve dependencies from Workspace projects" :false)
以防您不熟悉 Maven。这是完整的 pom.xml:http://pastebucket.com/88444
类似于 Steven S. 的回答,但使用 maven-dependency-plugin. Based on this blog post。
我更改了补丁库的名称(不是版本),但这取决于您的需要,哪个更适合您。
对原库的依赖应该标记为<optional>true</optional>
。否则,依赖你的补丁库的项目也将依赖于原始库,这意味着补丁版本和原始版本都将在类路径中,这可能会导致各种问题。
如果您的项目是子项目,您仍然可以使用与父项目完全不同的 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>