zoukankan      html  css  js  c++  java
  • 【maven插件】maven-shade-plugin

    概述

    该插件提供了将artifact打包到一个本地jar包的能力,包括其依赖关系以及一些参数如 shade -rename重命名依赖关系的包。

    目标

    shade:shade 绑定到建生命周期中的package阶段,用于创建a shaded jar。

      mvn package

    用法

    1.配置

    <project>
      ...
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
              <!-- put your configurations here -->
            </configuration>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>shade</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
      ...
    </project>

    2.支持的transformer

    <configuration>
        <!-- 打包带有主函数入口的jar报 -->
        <transformers>
             <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                 <mainClass>com.ultrapower.secsight.main.Runner</mainClass>
             </transformer>
        </transformers>
    </configuration>
    ApacheLicenseResourceTransformer    防止许可复制
    ApacheNoticeResourceTransformer    Prepares merged NOTICE
    AppendingTransformer    Adds content to a resource
    ComponentsXmlResourceTransformer    Aggregates Plexus components.xml
    DontIncludeResourceTransformer    Prevents inclusion of matching resources
    IncludeResourceTransformer    Adds files from the project
    ManifestResourceTransformer    设置清单MANIFEST中的条目
    ServicesResourceTransformer Merges META-INF/services resources
    XmlAppendingTransformer Adds XML content to an XML resource

    用例

     1.控制本地JAR的依赖

    通过exclude/include控制本地JAR的依赖:

    <project>
      ...
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>shade</goal>
                </goals>
                <configuration>
                  <artifactSet>
                    <excludes>
                      <exclude>classworlds:classworlds</exclude>
                      <exclude>junit:junit</exclude>
                      <exclude>jmock:*</exclude>
                      <exclude>*:xml-apis</exclude>
                      <exclude>org.apache.maven:lib:tests</exclude>
                      <exclude>log4j:log4j:jar:</exclude>
                    </excludes>
                  </artifactSet>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
      ...
    </project>

     2.自动精简uber-jar

    <project>
      ...
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>shade</goal>
                </goals>
                <configuration>
                  <minimizeJar>true</minimizeJar>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
      ...
    </project>

    从版本1.6开始,minimizeJar将保留在filter中标记为include的类。请注意,为artifact中的类指定include filter会隐式将该artifact中的所有其他非指定类排除。

    <project>
      ...
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>shade</goal>
                </goals>
                <configuration>
                  <minimizeJar>true</minimizeJar>
                  <filters>
                    <filter>
                       <artifact>log4j:log4j</artifact>
                       <includes>
                           <include>**</include>
                       </includes>
                    </filter>
                    <filter>
                       <artifact>commons-logging:commons-logging</artifact>
                       <includes>
                           <include>**</include>
                       </includes>
                    </filter>
                  </filters>            
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
      ...
    </project>

    3.Classes重定位

    <project>
      ...
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>shade</goal>
                </goals>
                <configuration>
                  <relocations>
                    <relocation>
                      <pattern>org.codehaus.plexus.util</pattern>
                      <shadedPattern>org.shaded.plexus.util</shadedPattern>
                      <excludes>
                        <exclude>org.codehaus.plexus.util.xml.Xpp3Dom</exclude>
                        <exclude>org.codehaus.plexus.util.xml.pull.*</exclude>
                      </excludes>
                    </relocation>
                  </relocations>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
      ...
    </project>

    遇到错误

    Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:1.6:shade (default) on project justfortest: Error creating shaded jar: null: NullPointerException -> [Help 1]
    org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:1.6:shade (default) on project justfortest: Error creating shaded jar: null

    该插件不能够用于父项目POM,即<packaging>pom</packaging>

  • 相关阅读:
    CKEditor4x word导入不保存格式的解决方案
    为了希望正式开始开发
    HTTP权威指南-URL与资源
    HTTP权威指南-HTTP概述
    同源策略和跨域访问
    普通Html文件图片上传(Storing Image To DB)
    PostgreSQL时间戳提取的特殊需求
    (转)百度前端规范、腾讯前端规范
    整理一下嵌入式WEB开发中的各种屏蔽(转)
    Excel表格指定列数据转换成文本
  • 原文地址:https://www.cnblogs.com/Dhouse/p/6943434.html
Copyright © 2011-2022 走看看