maven-shade-plugin 使用介绍

龙永逸
2023-12-01

maven-shade-plugin 使用介绍

1 功能描述

  1. 把整个项目及其它的依赖都打包到一个 “xxx.jar” 中
  2. 打包冲突,重命名操作

2 使用案例

博主使用案例:加强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>

3 相关配置介绍

3.1 设置依赖-includes

      <includes>
           // *:* 表示当前包及依赖包全部打包
           // 语法:groupId:artifactId:version
           // * 表示全部
           <include>*:*</include>
           <include>com.fasterxml.jackson.core:*</include>
     </includes>

3.2 排除不需要打包的依赖-excludes

      <excludes>
           // 排除指定包groupId:artifactId
           <exclude>joda-time:joda-time</exclude>
           // 排序包META-INF目录后缀为SF所有文件
           <exclude>META-INF/*.SF</include>
           // 排序properties后缀的所有文件
           <exclude>*.properties</exclude
     </includes>

3.3 filter 过滤设置

// 指定某个包做特定的过滤
 <filter>
        <artifact>org.elasticsearch.client:elasticsearch-rest-high-level-client</artifact>
             <excludes>
                <exclude>forbidden/**</exclude>
            </excludes>
</filter>

3.4 relocations 包重命名

    // 打包后将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>

3.5 minimizeJar 自动适配依赖

此设置支持自动移除项目中没有使用到的依赖,以此来最小化 jar 包的体积

<configuration>
    <minimizeJar>true</minimizeJar>
</configuration>

3.6 生成一个可执行的jar

<project>
    ...
    <configuration>
        <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <mainClass>org.sonatype.haven.xxx</mainClass>
            </transformer>
        </transformers>
    </configuration>
    ...
</project>

 类似资料: