Maven 精简依赖包

来源:互联网 发布:学生水杯推荐知乎 编辑:程序博客网 时间:2024/06/05 16:15

1、minijar

Maven的插件minijar,用于压缩依赖包。

minijar是mojo项目中的一个插件。

mojo项目:mojo.codehaus.org/

minijar项目:mojo.codehaus.org/minijar-maven-plugin/

该插件分析项目中类的依赖关系。

一般情况下项目并不是依赖库中的所有的类。

minijar能够生成一个项目依赖的相关类的迷你的jar文件版本。

比如一个依赖的jar文件有10个类,当前项目只直接/间接依赖其中的3个,那么minijar将为你生成一个只有3个类的jar。

简单使用
在默认target目录下生成mini jar文件:

[html] view plain copy
  1. <span style="font-weight: normal;"><span style="font-size:18px;">mvn minijar:minijars </span></span>  


另外,比如有多个类库,也可以将它们打在一起生成单一的jar文件。

这需要使用ueberjar,见:mojo.codehaus.org/minijar-maven-plugin/ueberjar-mojo.html

命令如下:
[html] view plain copy
  1. <span style="font-weight: normal;"><span style="font-size:18px;">mvn minijar:ueberjar  </span></span>  


2、 Maven Shade Plugin 

目前minijar已经不推荐使用了。该功能已经被包含在maven shade plugin中了。

Selecting Contents for Uber JAR

The POM snippet below shows how to control which project dependencies should be included/excluded in the uber JAR:
<project>  ...  <build>    <plugins>      <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-shade-plugin</artifactId>        <version>2.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>

Of course, <includes> can be used as well to specify a white list of artifacts. Artifacts are denoted by a composite idenitifer of the form groupId:artifactId[[:type]:classifier]. Since plugin version 1.3, the wildcard characters '*' and '?' can be used to do glob-like pattern matching.

For fine-grained control of which classes from the selected dependencies are included, artifact filters can be used:

<project>  ...  <build>    <plugins>      <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-shade-plugin</artifactId>        <version>2.0</version>        <executions>          <execution>            <phase>package</phase>            <goals>              <goal>shade</goal>            </goals>            <configuration>              <filters>                <filter>                  <artifact>junit:junit</artifact>                  <includes>                    <include>junit/framework/**</include>                    <include>org/junit/**</include>                  </includes>                  <excludes>                    <exclude>org/junit/experimental/**</exclude>                    <exclude>org/junit/runners/**</exclude>                  </excludes>                </filter>                <filter>                  <artifact>*:*</artifact>                  <excludes>                    <exclude>META-INF/*.SF</exclude>                    <exclude>META-INF/*.DSA</exclude>                    <exclude>META-INF/*.RSA</exclude>                  </excludes>                </filter>              </filters>            </configuration>          </execution>        </executions>      </plugin>    </plugins>  </build>  ...</project>

Here, Ant-like patterns are used to specify that from the dependency junit:junit only certain classes/resources should be included in the uber JAR. The second filter demonstrates the use of wildcards for the artifact identity which was introduced in plugin version 1.3. It excludes all signature related files from every artifact, regardless of its group or artifact id.

Besides user-specified filters, the plugin can also be configured to automatically remove all classes of dependencies that are not used by the project, thereby minimizing the resulting uber JAR:

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

As of version 1.6, minimizeJar will respect classes that were specifically marked for inclusion in a filter. Note that specifying an include filter for classes in an artifact implicitly excludes all non-specified classes in that artifact.

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