技巧|如何优雅的替换第三方依赖中的类

WilliamGates
发布于 2022-6-20 17:53
浏览
0收藏

 

大家好,我是威哥,《RocketMQ技术内幕》作者、RocketMQ社区首席布道师、中通快递基础架构资深架构师,越努力越幸运,唯有坚持不懈,与大家共勉。

1、背景

在项目中需要依赖其他第三方的jar包,但有时我们需要扩展第三方jar的功能,或者修复依赖中已知还未修复的bug,但碰到过很多种情况,第三方类库中并没有提供对应的扩展点导致无法优雅的使用继承等方法对代码进行改造。

如果第三方类库并没有提供扩展点,我们通常会使用如下两种办法来进行二次开发:

  • 将第三方jar的源工程下载下来,放入maven中,重新进行修改,并发布在私有仓库中
  • 直接将需要修改的代码复制到当前工程,进行修改
    如果这个第三方类库是全公司都需要使用的,使用第一种无疑是最好的,不过也带来了一定的维护成本。

如果只是单个项目需要引入,直接将代码拷贝到工程中,直接修改,但编译后的文件会在当前classpath路径下生成对应的class文件,能否将这些修改的class打入到最终的第三方jar类库。

 

2、通过插件体会Jar包中的类

下面,我们介绍通过maven插件,将修改的类代码类,直接替换jar中的类。

2.1 maven-dependency-plugin
maven-dependency-plugin简单说明情况如下:

  • 插件说明:依赖项插件提供了操作工件的功能。它可以将项目从本地或远程存储库复制和/或解压缩到指定位置。官方地址
  • 使用命令:从存储库中检索项目列表并将其解压缩到定义位置的目标。具体命令如下:
    dependency:unpack​
    • 1.
  • 操作原理
    这个命令可以将我们指定的dependency解压到class目录中,然后设置不覆盖本地项目相同class文件(类的全限定名相同),就达到了本地文件替换源jar中class文件的目的。

2.2 实操
计划将org.apache.flink:flink-kubernetes_2.11:1.14.3中的KubernetesClusterClientFactory类,将createClusterDescriptor方法第一行输出日志。

在需要的代码中添加一行日志如下图所示:

package org.apache.flink.kubernetes;

...
/** A {@link ClusterClientFactory} for a Kubernetes cluster. */
@Internal
public class KubernetesClusterClientFactory
        extends AbstractContainerizedClusterClientFactory<String> {
...

    @Override
    public KubernetesClusterDescriptor createClusterDescriptor(Configuration configuration) {
        //新添加代码
        System.out.println("configuration : "+ configuration)
        //=======
        checkNotNull(configuration);
        if (!configuration.contains(KubernetesConfigOptions.CLUSTER_ID)) {
            final String clusterId = generateClusterId();
            configuration.setString(KubernetesConfigOptions.CLUSTER_ID, clusterId);
        }
        return new KubernetesClusterDescriptor(
                configuration,
                FlinkKubeClientFactory.getInstance().fromConfiguration(configuration, "client"));
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

为了在打包的时候修改的代码能打入到第三方jar包,需要修改pom文件,对应的代码如下:

 <properties>
  <flink.version>1.14.3</flink.version>
  <scala.binary.version>2.11</scala.binary.version>
  </properties>

  <dependencies>
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-kubernetes_${scala.binary.version}</artifactId>
            <version>${flink.version}</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>unpack</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.apache.flink</groupId>
                                    <artifactId>flink-kubernetes_${scala.binary.version}</artifactId>
                                    <overWrite>false</overWrite>
                                    <outputDirectory>${project.build.directory}/classes</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.

 

接下来通过maven打包命令打包:

mvm clean install
  • 1.

从class目录中查看KubernetesClusterClientFactory,发现代码已经是我们修改后的类。同时,当前项目编译的jar包,类也已经被替换成我们自定义的类,目标达成。

 

文章转自公众号:中间件兴趣圈

标签
已于2022-6-20 17:53:30修改
收藏
回复
举报


回复
    相关推荐