利用maven-shade-plugin打包包含所有依赖jar包
来源:互联网 发布:cf游戏数据异常怎么办 编辑:程序博客网 时间:2024/05/21 10:20
1. Why?
通过 maven-shade-plugin 生成一个 uber-jar,它包含所有的依赖 jar 包。
2. Goals
3. Usage
- 配置 maven-shade-plugin
maven-shade-plugin 将 goal shade:shade 绑定到 phase package 上。
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.3</version> <configuration> <!-- put your configurations here --> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
- 执行命令
mvn clean package
会在 target 文件生成一个 uber-jar,以 -shaded.jar 为后缀的 jar 包。
4. Examples
- Selecting Contents for Uber JAR
将该工程依赖的部分 Jar 包 include/exclude 掉。
<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>
将依赖的某个 Jar 包内部的类或者资源 include/exclude 掉。
<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>
maven-shade-plugin 自动将所有不使用的类全部排除掉,将 uber-jar 最小化。
<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>
- Attaching the Shaded Artifact
默认会生成一个Jar包和一个以 "-shaded"为结尾的uber-jar包,可以通过配置来指定uber-jar的后缀名。
<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> <shadedArtifactAttached>true</shadedArtifactAttached> <shadedClassifierName>jackofall</shadedClassifierName> <!-- Any name that makes sense --> </configuration> </execution> </executions> </plugin> </plugins> </build>
- Executable JAR
通过设置 MainClass 创建一个可执行 Jar 包。
<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> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>org.sonatype.haven.HavenCli</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build>
- Relocating Classes
Java 工程经常会遇到第三方 Jar 包冲突,使用 maven shade plugin 解决 jar 或类的多版本冲突。 maven-shade-plugin 在打包时,可以将项目中依赖的 jar 包中的一些类文件打包到项目构建生成的 jar 包中,在打包的时候把类重命名。下面的配置将 org.codehaus.plexus.util jar 包重命名为 org.shaded.plexus.util。
<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> <relocations> <relocation> <pattern>org.codehaus.plexus.util</pattern> <shadedPattern>org.shaded.plexus.util</shadedPattern> <excludes> <exclude>org.codehaus.plexus.util.xml.Xpp3Dom</exclude> <exclude>org.codehaus.plexus.util.xml.pull.*</exclude> </excludes> </relocation> </relocations> </configuration> </execution> </executions> </plugin> </plugins> </build>
参考
maven 入门指南
maven 生命周期
Maven 默认插件以及功能
maven 依赖管理
maven-shade-plugin 入门指南
maven-assembly-plugin 入门指南
作者:要饭真得好难
链接:http://www.jianshu.com/p/7a0e20b30401
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
阅读全文
0 0
- 利用maven-shade-plugin打包包含所有依赖jar包
- 使用maven shade plugin打包uber jar包
- Maven打包生成包含所有依赖的jar包
- Maven打包生成包含所有依赖的jar包
- 使用 maven 插件 maven-shade-plugin 对可执行 java 工程及其全部依赖 jar 进行打包
- 使用 maven 插件 maven-shade-plugin 对可执行 java 工程及其全部依赖 jar 进行打包
- 使用 maven 插件 maven-shade-plugin 对可执行 java 工程及其全部依赖 jar 进行打包
- Maven使用maven-shade-plugin对java工程及其全部依赖打包可执行jar
- 【Maven学习】Maven打包生成普通jar包、可运行jar包、包含所有依赖的jar包
- 【Maven学习】Maven打包生成普通jar包、可运行jar包、包含所有依赖的jar包
- maven-shade-plugin 打包
- Maven Spring 项目打包成可运行的jar包 (用maven-shade-plugin)
- 【Maven学习】Maven打包生成包含所有依赖的jar包
- 【Maven学习】Maven打包生成包含所有依赖的jar包
- 【Maven学习】Maven打包生成包含所有依赖的jar包
- maven打包一个包含依赖jar包的可执行jar
- Maven打包可执行jar,包含依赖jar包
- Java之——使用 maven 插件 maven-shade-plugin 对可执行 java 工程及其全部依赖 jar 进行打包
- PAT乙级1037. 在霍格沃茨找零钱(20)
- 一、为什么选择SpringBoot
- Java——代码块
- Linux字符设备驱动
- DEDECMS点击主栏目默认显示第一个子栏目列表的方法
- 利用maven-shade-plugin打包包含所有依赖jar包
- poj-3982(矩阵快速幂+大数模板)
- 机器学习笔记8:基于TensorFlow的数据预测
- 校园招聘-2017滴滴研发工程师内推笔试编程题
- CURL:Protocol http not supported or disabled in libcurl
- JS正则表达式
- php7中使用mongoDB的聚合操作对数据进行分组求和统计操作
- RDD、DataFrame、Dataset介绍
- 【大二最后两题】Hrbust 2064 萌萌哒十五酱的宠物~【思维+树链剖分 / 树上倍增LCA】