下面的 POM 片段显示了如何控制应该在 uber JAR 中包含/排除哪些项目依赖项:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</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>
当然,<includes>也可以用来指定工件的白名单。工件由groupId : artifactId [[: type ]: classifier ] 形式的复合标识符表示。从插件版本 1.3 开始,通配符 '*' 和 '?' 可用于进行类似 glob 的模式匹配。
对于包含来自所选依赖项的哪些类的细粒度控制,可以使用工件过滤器:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</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>
在这里,类似 Ant 的模式用于从依赖项junit:junit中指定只有某些类/资源应该包含在 uber JAR 中。第二个过滤器演示了在插件版本 1.3 中引入的工件标识的通配符的使用。它从每个工件中排除所有与签名相关的文件,无论其组或工件 ID 是什么。