• maven插件学习(maven-shade-plugin和maven-antrun-plugin插件)


    整合spark3.3.x和hive2.1.1-cdh6.3.2碰到个问题,就是spark官方支持的hive是2.3.x,但是cdh中的hive确是2.1.x的,项目中又计划用spark-thrift-server,导致编译过程中有部分报错。其中OperationLog这个类在hive2.3中新增加了几个方法,导致编译报错。这个时候有两种解决办法:

    • 修改spark源码,注释掉调用OperationLog中新增方法的地方,看了下源码一共有好几处需要注释
    • 在spark源码中添加新版本的OperationLog类,打包时候需要替换原先的hive-exec中这些类,否则运行时候可能会加载不到这个类

    最终决定使用第二种方法,减少对源码的修改。

    OperationLog类打包以后会出现在hive-exec-{version}.jar中,其实要做的事情很简单,就是删除这个jar中的OperationLog类,这里通过maven实现我知道的有如下两种实现办法:

    • 通过maven-shade-plugin进行relocation
    • 通过maven-antrun-plugin解压hive-exec-{version}.jar删除OperationLog相关类再(复制新编译的OperationLog相关类 这部分可以省略,不复制也可以)打包新的hive-exec-{version}.jar

    上述两种方法各有优劣

    • 通过shade方法进行relocation后会把原先hive-exec中所有的文件都打包到引入它的jar中,会导致生成的jar比较大,hive-exec自身就34M了。这种处理方法也是开源项目中常用的处理方法
    • 通过antrun解压删除相关类再压缩我目前没见别的项目这么玩过。这个时候可以选择是否将自己写的OperationLog类再打包回hive-exec中,如果不打包别的地方用到这个类就会报错了。

    第一种方法的pom中build部分如下

    1. <build>
    2. <plugins>
    3. <plugin>
    4. <groupId>org.apache.maven.pluginsgroupId>
    5. <artifactId>maven-dependency-pluginartifactId>
    6. <version>3.3.0version>
    7. <executions>
    8. <execution>
    9. <phase>packagephase>
    10. <goals>
    11. <goal>copy-dependenciesgoal>
    12. goals>
    13. <configuration>
    14. <outputDirectory>${project.build.directory}/liboutputDirectory>
    15. <excludeArtifactIds>hive-execexcludeArtifactIds>
    16. configuration>
    17. execution>
    18. executions>
    19. plugin>
    20. <plugin>
    21. <groupId>org.apache.maven.pluginsgroupId>
    22. <artifactId>maven-shade-pluginartifactId>
    23. <version>3.2.4version>
    24. <executions>
    25. <execution>
    26. <phase>packagephase>
    27. <goals>
    28. <goal>shadegoal>
    29. goals>
    30. <configuration>
    31. <shadedArtifactAttached>falseshadedArtifactAttached>
    32. <artifactSet>
    33. <includes>
    34. <include>org.apache.hive:hive-execinclude>
    35. includes>
    36. artifactSet>
    37. <filters>
    38. <filter>
    39. <artifact>org.apache.hive:hive-execartifact>
    40. <excludes>
    41. <exclude>META-INF/*.MFexclude>
    42. <exclude>META-INF/*.SFexclude>
    43. <exclude>META-INF/*.DSAexclude>
    44. <exclude>META-INF/*.RSAexclude>
    45. <exclude>org/apache/hadoop/hive/ql/session/OperationLog*exclude>
    46. excludes>
    47. filter>
    48. filters>
    49. configuration>
    50. execution>
    51. executions>
    52. plugin>
    53. plugins>
    54. build>

    第二种方法的pom中build部分如下

    1. <build>
    2. <plugins>
    3. <plugin>
    4. <groupId>org.apache.maven.pluginsgroupId>
    5. <artifactId>maven-jar-pluginartifactId>
    6. <version>3.2.0version>
    7. <executions>
    8. <execution>
    9. <id>default-jarid>
    10. <phase>packagephase>
    11. <goals>
    12. <goal>jargoal>
    13. goals>
    14. execution>
    15. executions>
    16. <configuration>
    17. <excludes>**/OperationLog*.classexcludes>
    18. configuration>
    19. plugin>
    20. <plugin>
    21. <groupId>org.apache.maven.pluginsgroupId>
    22. <artifactId>maven-dependency-pluginartifactId>
    23. <version>3.3.0version>
    24. <executions>
    25. <execution>
    26. <phase>packagephase>
    27. <goals>
    28. <goal>copy-dependenciesgoal>
    29. goals>
    30. <configuration>
    31. <outputDirectory>${project.build.directory}/liboutputDirectory>
    32. configuration>
    33. execution>
    34. executions>
    35. plugin>
    36. <plugin>
    37. <groupId>org.apache.maven.pluginsgroupId>
    38. <artifactId>maven-antrun-pluginartifactId>
    39. <version>1.8version>
    40. <executions>
    41. <execution>
    42. <phase>packagephase>
    43. <goals>
    44. <goal>rungoal>
    45. goals>
    46. <configuration>
    47. <target>
    48. <echo message="Repackage hive-exec."/>
    49. <unjar src="${project.build.directory}/lib/hive-exec-${hive.version}.jar"
    50. dest="${project.build.directory}/exploded/hive-exec">
    51. <patternset>
    52. <exclude name="**/OperationLog*.class"/>
    53. patternset>
    54. unjar>
    55. <copy todir="${project.build.directory}/exploded/hive-exec">
    56. <fileset dir="${project.build.directory}/classes">
    57. <include name="**/OperationLog*.class" />
    58. fileset>
    59. copy>
    60. <jar destfile="${project.build.directory}/lib/hive-exec-${hive.version}.jar"
    61. basedir="${project.build.directory}/exploded/hive-exec"/>
    62. target>
    63. configuration>
    64. execution>
    65. executions>
    66. plugin>
    67. plugins>
    68. build>

    虽然上述两种方法都能达到目的,第一种看着简洁一点,第二种看着操作复杂一点。不过我还是倾向于第二种,首先第二种不会减少和增加包的数量,包体积变化也不会太大,看着也更加符合预期的目的,其次第二种操作完以后看起来很简洁,更加适合强迫症或者代码洁癖患者。

    参考链接:

    maven-shade-plugin

    maven-antrun-plugin

  • 相关阅读:
    Linux的screen工具库实现多终端
    Mac笔记本安装maven
    Jmeter响应时间和tps监听器使用教程
    Java并发(四)----线程运行原理
    专家建议|首席财务官拥抱财务敏捷转型正当时
    MyBatis笔记
    驱动开发:内核层InlineHook挂钩函数
    用调试来帮你分析并拿捏折半插入排序算法的流程
    Java练习24
    《流畅的python》阅读笔记 - 第七章:函数装饰器和闭包
  • 原文地址:https://blog.csdn.net/catcher92/article/details/128176525