Shade
官网英文定义:This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade
可能高手们认为此插件使用比较简单吧,网上介绍Shade
的资料不多,中文的就更少了。即使找到,也没有解答我心中的疑惑。
主要是我太菜,平时使用Maven也都是ctrl c + ctrl v
直接复制前人的成果,毫无积累。
某天,特别是当某人问你为什么要这样子写的时候,就无言以答了。
譬如:
<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>
看英文单词就知道是排除(excludes
)掉一些依赖(artifactSet
), 但是我纠结在<exclude>junit:junit</exclude>
为什么是用:
分隔的, 以及exclude
是定义在哪里的?
经过一番查找……
package org.apache.maven.plugins.shade.mojo;
import java.util.Set;
/**
* @author Jason van Zyl
*/
public class ArtifactSet
{
private Set<String> includes;
private Set<String> excludes;
public Set<String> getIncludes()
{
return includes;
}
public Set<String> getExcludes()
{
return excludes;
}
}
在...shade.mojo
下的ArtifactSet
中可以很清楚的看到属性Set<String> excludes
在这个包下还可以看到ShadeMojo.java
package org.apache.maven.plugins.shade.mojo;
/**
* Mojo that performs shading delegating to the Shader component.
*/
@Mojo( name = "shade", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true,
requiresDependencyResolution = ResolutionScope.RUNTIME )
public class ShadeMojo extends AbstractMojo implements Contextualizable
{
@Parameter( defaultValue = "${session}", readonly = true, required = true )
private MavenSession session;
@Parameter( defaultValue = "${project}", readonly = true, required = true )
private MavenProject project;
@Component
private MavenProjectHelper projectHelper;
@Component( hint = "default", role = org.apache.maven.plugins.shade.Shader.class )
private Shader shader;
@Component
private DependencyGraphBuilder dependencyGraphBuilder;
@Component
private ProjectBuilder projectBuilder;
@Component
private ArtifactMetadataSource artifactMetadataSource;
@Parameter( readonly = true, required = true, defaultValue = "${project.remoteArtifactRepositories}" )
protected List<ArtifactRepository> remoteArtifactRepositories;
@Parameter( readonly = true, required = true, defaultValue = "${localRepository}" )
protected ArtifactRepository localRepository;
@Component
protected ArtifactFactory artifactFactory;
@Component
protected ArtifactResolver artifactResolver;
@Parameter
private ArtifactSet artifactSet;
@SuppressWarnings( "MismatchedReadAndWriteOfArray" )
@Parameter
private PackageRelocation[] relocations;
@Parameter
private ResourceTransformer[] transformers;
@SuppressWarnings( "MismatchedReadAndWriteOfArray" )
@Parameter
private ArchiveFilter[] filters;
@Parameter( defaultValue = "${project.build.directory}" )
private File outputDirectory;
@Parameter
private String finalName;
@Parameter( defaultValue = "${project.artifactId}" )
private String shadedArtifactId;
@Parameter
private String shadedGroupFilter;
@Parameter
private boolean shadedArtifactAttached;
@Parameter( defaultValue = "true" )
private boolean createDependencyReducedPom;
@Parameter( defaultValue = "${basedir}/dependency-reduced-pom.xml" )
private File dependencyReducedPomLocation;
@Parameter( defaultValue = "false" )
private boolean generateUniqueDependencyReducedPom;
@Parameter
private boolean keepDependenciesWithProvidedScope;
@Parameter
private boolean promoteTransitiveDependencies;
@Parameter( defaultValue = "shaded" )
private String shadedClassifierName;
@Parameter
private boolean createSourcesJar;
@Parameter( property = "shadeSourcesContent", defaultValue = "false" )
private boolean shadeSourcesContent;
@Parameter
private boolean minimizeJar;
@Parameter
private File outputFile;
@Parameter
private String shaderHint;
@Parameter( defaultValue = "false" )
private boolean useBaseVersion;
@Parameter( defaultValue = "false" )
private boolean shadeTestJar;
private PlexusContainer plexusContainer;
}
这里只列出了成员属性, 可以看到@Parameter private ArtifactSet artifactSet;
在其中。pom.xml
中的Shade配置在ShadeMojo.java
中都能找到。
顺藤摸瓜,你能找到ArtifactId.java
package org.apache.maven.plugins.shade.mojo;
class ArtifactId
{
private final String groupId;
private final String artifactId;
private final String type;
private final String classifier;
public ArtifactId( String id )
{
String[] tokens = new String[0];
if ( id != null && id.length() > 0 )
{
tokens = id.split( ":", -1 );
}
groupId = ( tokens.length > 0 ) ? tokens[0] : "";
artifactId = ( tokens.length > 1 ) ? tokens[1] : "*";
type = ( tokens.length > 3 ) ? tokens[2] : "*";
classifier = ( tokens.length > 3 ) ? tokens[3] : ( ( tokens.length > 2 ) ? tokens[2] : "*" );
}
}
这里有对:
的解释。
梳理下吧:
(1) 在疑惑Maven Plugin
配置时, 首先要去看下这个Guide to Configuring Plug-ins
这里集中介绍了插件的基本知识, 我就是在这里认识了Mojo
, 进而找到ShadeMojo
的。
(2) 不知如何配置, 看看官网的例子
(3) 还是很疑惑, 找到Mojo - ShadeMojo.java
。
(4) 然后就是实践+时间,慢慢理解吧!
官网访问太慢,把例子镜像过来。
001
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</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>
002
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</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>
003
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
004
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</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>