博主使用案例:加强flink-connector-es 组件,在不改变源码的前提下,将加强功能与源码打包到一个jar中,替换官方提供的源码,很好解决版本更新开发成本高
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>shade-flink</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadeTestJar>false</shadeTestJar>
<artifactSet>
<includes>
// *:* 表示将当前包与所有依赖包全部打包到一个jar中
// 语法:groupId:artifactId:version
// * 标识全部
<include>*:*</include>
<!-- // 指定某个包
<include>org.apache.commons:*</include>
<include>org.apache.flink:*</include>
<include>org.elasticsearch:*</include>
<include>org.elasticsearch.client:*</include>
<include>org.apache.httpcomponents:*</include>
<include>org.apache.lucene:*</include>
<include>com.fasterxml.jackson.core:*</include>-->
</includes>
<excludes>
<!-- These dependencies are not required. -->
// 排除某些包及文件
// 语法:
<exclude>com.carrotsearch:hppc</exclude>
<exclude>com.tdunning:t-digest</exclude>
<exclude>joda-time:joda-time</exclude>
<exclude>net.sf.jopt-simple:jopt-simple</exclude>
<exclude>org.elasticsearch:jna</exclude>
<exclude>org.hdrhistogram:HdrHistogram</exclude>
<exclude>org.yaml:snakeyaml</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>*.properties</exclude>
<exclude>*.config</exclude>
<exclude>*.txt</exclude>
</excludes>
</artifactSet>
<filters>
<!-- Unless otherwise noticed these filters only serve to reduce the size of the resulting
jar by removing unnecessary files -->
<filter>
<artifact>org.elasticsearch:elasticsearch</artifact>
<excludes>
<exclude>config/**</exclude>
<exclude>modules.txt</exclude>
<exclude>plugins.txt</exclude>
<exclude>org/joda/**</exclude>
</excludes>
</filter>
<filter>
<artifact>org.elasticsearch.client:elasticsearch-rest-high-level-client</artifact>
<excludes>
<exclude>forbidden/**</exclude>
</excludes>
</filter>
<filter>
<artifact>org.apache.httpcomponents:httpclient</artifact>
<excludes>
<exclude>mozilla/**</exclude>
</excludes>
</filter>
<filter>
<artifact>org.apache.lucene:lucene-analyzers-common</artifact>
<excludes>
<exclude>org/tartarus/**</exclude>
</excludes>
</filter>
<filter>
<artifact>*:*</artifact>
<excludes>
<!-- exclude Java 9 specific classes as otherwise the shade-plugin crashes -->
<exclude>META-INF/versions/**</exclude>
<exclude>META-INF/services/com.fasterxml.**</exclude>
<exclude>META-INF/services/org.apache.lucene.**</exclude>
<exclude>META-INF/services/org.elasticsearch.**</exclude>
<exclude>META-INF/LICENSE.txt</exclude>
</excludes>
</filter>
</filters>
<relocations>
<!-- Force relocation of all Elasticsearch dependencies. -->
<relocation>
<pattern>org.apache.commons</pattern>
<shadedPattern>org.apache.flink.elasticsearch6.shaded.org.apache.commons</shadedPattern>
</relocation>
<relocation>
<pattern>org.apache.http</pattern>
<shadedPattern>org.apache.flink.elasticsearch6.shaded.org.apache.http</shadedPattern>
</relocation>
<relocation>
<pattern>org.apache.lucene</pattern>
<shadedPattern>org.apache.flink.elasticsearch6.shaded.org.apache.lucene</shadedPattern>
</relocation>
<relocation>
<pattern>org.elasticsearch</pattern>
<shadedPattern>org.apache.flink.elasticsearch6.shaded.org.elasticsearch</shadedPattern>
</relocation>
<relocation>
<pattern>com.fasterxml.jackson</pattern>
<shadedPattern>org.apache.flink.elasticsearch6.shaded.com.fasterxml.jackson</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<includes>
// *:* 表示当前包及依赖包全部打包
// 语法:groupId:artifactId:version
// * 表示全部
<include>*:*</include>
<include>com.fasterxml.jackson.core:*</include>
</includes>
<excludes>
// 排除指定包groupId:artifactId
<exclude>joda-time:joda-time</exclude>
// 排序包META-INF目录后缀为SF所有文件
<exclude>META-INF/*.SF</include>
// 排序properties后缀的所有文件
<exclude>*.properties</exclude
</includes>
// 指定某个包做特定的过滤
<filter>
<artifact>org.elasticsearch.client:elasticsearch-rest-high-level-client</artifact>
<excludes>
<exclude>forbidden/**</exclude>
</excludes>
</filter>
// 打包后将org.apache.commons路径全部替换为org.apache.flink.elasticsearch6.shaded.org.apache.commons
<relocations>
<relocation>
<pattern>org.apache.commons</pattern
<shadedPattern>org.apache.flink.elasticsearch6.shaded.org.apache.commons</shadedPattern>
// 指定那些包不重名
<excludes>
<exclude>org.apache.commons.xml.Xpp3Dom</exclude>
<exclude>org.apache.commons.xml.pull.*</exclude>
</excludes>
</relocation>
</relocations>
此设置支持自动移除项目中没有使用到的依赖,以此来最小化 jar 包的体积
<configuration>
<minimizeJar>true</minimizeJar>
</configuration>
<project>
...
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.sonatype.haven.xxx</mainClass>
</transformer>
</transformers>
</configuration>
...
</project>