下面的 POM 片段显示了如何控制应该在 uber JAR 中包含/排除哪些项目依赖项:
<project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.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>3.2.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 是什么。
除了用户指定的过滤器外,该插件还可以配置为自动删除项目未使用的所有依赖项类,从而最小化生成的 uber JAR:
<project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</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 将尊重专门标记为包含在过滤器中的类。请注意,为工件中的类指定包含过滤器会隐式排除该工件中所有未指定的类。<excludeDefaults>false<\excludeDefaults>将覆盖此行为,以便仍然包含所有未指定的类。
<project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</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> <filter> <artifact>foo:bar</artifact> <excludeDefaults>false</excludeDefaults> <includes> <include>foo/Bar.class</include> </includes> </filter> </filters> </configuration> </execution> </executions> </plugin> </plugins> </build> ... </project>