[Apache Maven Shade Plugin] [example] [001] 官方例子:includes-excludes

链接地址:[Selecting Contents for Uber JAR](http://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html)

apache网站在国内打开太慢了。因此我将这些有用的资源收集起来,保存在博客中!

Apache Maven Shade Plugin:是一个Maven打包的插件。其官网英文定义如下:This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade

001.

 1 <project>
 2   ...
 3   <build>
 4     <plugins>
 5       <plugin>
 6         <groupId>org.apache.maven.plugins</groupId>
 7         <artifactId>maven-shade-plugin</artifactId>
 8         <version>2.4.3</version>
 9         <executions>
10           <execution>
11             <phase>package</phase>
12             <goals>
13               <goal>shade</goal>
14             </goals>
15             <configuration>
16               <artifactSet>
17                 <excludes>
18                   <exclude>classworlds:classworlds</exclude>
19                   <exclude>junit:junit</exclude>
20                   <exclude>jmock:*</exclude>
21                   <exclude>*:xml-apis</exclude>
22                   <exclude>org.apache.maven:lib:tests</exclude>
23                   <exclude>log4j:log4j:jar:</exclude>
24                 </excludes>
25               </artifactSet>
26             </configuration>
27           </execution>
28         </executions>
29       </plugin>
30     </plugins>
31   </build>
32   ...
33 </project>

002.

 1 <project>
 2   ...
 3   <build>
 4     <plugins>
 5       <plugin>
 6         <groupId>org.apache.maven.plugins</groupId>
 7         <artifactId>maven-shade-plugin</artifactId>
 8         <version>2.4.3</version>
 9         <executions>
10           <execution>
11             <phase>package</phase>
12             <goals>
13               <goal>shade</goal>
14             </goals>
15             <configuration>
16               <filters>
17                 <filter>
18                   <artifact>junit:junit</artifact>
19                   <includes>
20                     <include>junit/framework/**</include>
21                     <include>org/junit/**</include>
22                   </includes>
23                   <excludes>
24                     <exclude>org/junit/experimental/**</exclude>
25                     <exclude>org/junit/runners/**</exclude>
26                   </excludes>
27                 </filter>
28                 <filter>
29                   <artifact>*:*</artifact>
30                   <excludes>
31                     <exclude>META-INF/*.SF</exclude>
32                     <exclude>META-INF/*.DSA</exclude>
33                     <exclude>META-INF/*.RSA</exclude>
34                   </excludes>
35                 </filter>
36               </filters>
37             </configuration>
38           </execution>
39         </executions>
40       </plugin>
41     </plugins>
42   </build>
43   ...
44 </project>

003.

 1 <project>
 2   ...
 3   <build>
 4     <plugins>
 5       <plugin>
 6         <groupId>org.apache.maven.plugins</groupId>
 7         <artifactId>maven-shade-plugin</artifactId>
 8         <version>2.4.3</version>
 9         <executions>
10           <execution>
11             <phase>package</phase>
12             <goals>
13               <goal>shade</goal>
14             </goals>
15             <configuration>
16               <minimizeJar>true</minimizeJar>
17             </configuration>
18           </execution>
19         </executions>
20       </plugin>
21     </plugins>
22   </build>
23   ...
24 </project>

004.

 1 <project>
 2   ...
 3   <build>
 4     <plugins>
 5       <plugin>
 6         <groupId>org.apache.maven.plugins</groupId>
 7         <artifactId>maven-shade-plugin</artifactId>
 8         <version>2.4.3</version>
 9         <executions>
10           <execution>
11             <phase>package</phase>
12             <goals>
13               <goal>shade</goal>
14             </goals>
15             <configuration>
16               <minimizeJar>true</minimizeJar>
17               <filters>
18                 <filter>
19                    <artifact>log4j:log4j</artifact>
20                    <includes>
21                        <include>**</include>
22                    </includes>
23                 </filter>
24                 <filter>
25                    <artifact>commons-logging:commons-logging</artifact>
26                    <includes>
27                        <include>**</include>
28                    </includes>
29                 </filter>
30               </filters>
31             </configuration>
32           </execution>
33         </executions>
34       </plugin>
35     </plugins>
36   </build>
37   ...
38 </project>

(001)`Shade`官网上的介绍也不多。可能因为其相对比较简单吧。但是,我这个菜鸟仍然很头疼如何配置`Shade`。网上的资料比较少,估计也是认为比较简单吧,没人撰文详细介绍`Shade`。所以,官方提供的例子就成为最有价值的资料了。

(002)结合`Shade`源码,我开始踏上研究如何使用的`Shade`旅程。源码的获得比较简单,为了研究`Shade`可以将其添加到pom.xml的`dependencies`中。当然也可以去官网镜像下载

(003)研究任何`Maven Plugin`之前,都应该先看看这篇插件的介绍 [Guide to Configuring Plug-ins](http://maven.apache.org/guides/mini/guide-configuring-plugins.html),这会让你少走很多弯路(我不会告诉你我是掉坑里了爬起来才知道的)。

(003001)在这篇官方文档里,我首次看到了`Mojo`,这是Maven Plugin使用的一个IOC框架。(当下我并没有去研究Mojo所以知道的有限也许未来会去研究下。有兴趣的朋友可以去搜索下。)

(003002)`Mojo`中的字段是和plugin中的配置对应的,如果对例子中的配置很疑惑,就可以翻开源码中的`Mojo`顺藤摸瓜,找到它们的原理。`Shade`中的`Mojo`就是`ShadeMojo.java`了。

(004)end.

时间: 2024-08-17 09:57:54

[Apache Maven Shade Plugin] [example] [001] 官方例子:includes-excludes的相关文章

施用 maven shade plugin 解决 jar 或类的多版本冲突

施用 maven shade plugin 解决 jar 或类的多版本冲突 使用 maven shade plugin 解决 jar 或类的多版本冲突java 应用经常会碰到的依赖的三方库出现版本冲突,下面举一个具体的例子. Dubbo 是一个分布式的服务框架,其中的一种 rpc 实现(dubbo 协议)使用 hessian 3.2.0 来做序列化,另外一种实现(hsf协议)同样使用了 hesssian,但使用的版本是 3.0.14.如果现在一个应用中同时使用了 dubbo 协议和 hsf 协议

maven shade插件小记

maven shade plugin插件小用 项目中一直使用assembly插件来整合依赖包到一个胖jar,在做这个akka http项目的时候,在scala ide的run/debug中都执行正常,打包后执行就一直报报不到configuration项目的问题. 判断是assembly在解压合并相关配置jar包中的同名配置文件时丢失所致,例如引用中使用了akka-http与akka相关的jar包,它们都有reference.conf,没有进行合并,找到了问题根本原因. 解决方法: 使用maven

Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.0:shade

Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.0:shade (default) on project QMServer: Execution default of goal org.apache.maven.plugins:maven-shade-plugin:2.0:shade failed: A required class was missing while executing org.apach

Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5

Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5 or one of its dependencies could not be resolved 此问题的解决: 1.要确认spring sts已经设置代理 2.要确认windows -> Preferences -> maven 的settings.xml文件中,设置代理. 3.C:\Users\ttt\.m2\

Eclipse导入Maven项目出现:Could not calculate build plan: Plugin org.apache.maven.plugins:maven-war-plugin:2.2

错误如下: Could not calculate build plan: Plugin org.apache.maven.plugins:maven-war-plugin:2.2 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-war-plugin:jar:2.2... 解决方法: 1.项目右键->[Ma

CoreException: Could not calculate build plan: Plugin org.apache.maven.plugins:maven-compiler-plugin:3.1 or one of its dependencies could not be resolved

CoreException: Could not calculate build plan: Plugin org.apache.maven.plugins:maven-compiler-plugin:3.1 or one of its dependencies could not be resolved: Failure to find org.apache.maven.plugins:maven-compiler-plugin:jar:3.1 in http://repository.jbo

maven install 报错Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin

Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.6 解决方法: 删除maven库

Maven命令行创建web项目,并部署到jobss当中(解决No plugin found for prefix &#39;jboss-as&#39; in the current project and in the plugin groups [org.apache.maven.plugins,问题)

首件创建项目:此处可参照:http://maven.apache.org/guides/mini/guide-webapp.html mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp 将目录切换至my-webapp下,编译和打包: mvn clean package 此时,启动jboss服务器(我的是jboss

could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5 or one of(maven报错)

could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5 or one of 解决方法: 首先要确保eclipse中maven的配置正确! 找到自己的maven仓库所在的位置,例如我的: C:\Users\liugen.xu.m2\repository\org\apache\maven\plugins 找到repository\org\apache\maven\plugin