mvn生成runnablejar 的方法
主要讲3点,生成runnable jar 方法1是生成一个目录 方法2是直接一个runnable的jar 方法3是关于包含spring工程的情况 方法2和3其实是一致的
1、生成runnable jar,但是最后并不是一个可执行的jar 而是一个目录
主要使用 三个插件 将所需要的资源拷贝到 /target/Crunchify目录下,注意并没有生成一个jar中去
maven-resources-plugin, maven-dependency-plugin & maven-jar-plugin to generate your complete executable Jar Project? As a result it creates / copies all required files to /target/Crunchify folder.

几点说明:
CrunchifyMavenBuildPluginsis a Maven Project. If you have Java project and wanted to convert it into Maven project then follow this tutorial.- We do have two folders.
srcandresources. - Inside
resourcesfolder we do have a folder calledScriptswhich contains one executableshell script file. CrunchifyMain.javais a main starting point which hasmain(String args[])method inside.pom.xmlfile in which we will add Maven Plugins which will build executable .jar project with all included dependancies
pom文件如下:注意build标签下的3个plugins<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins> <!--说明:The Resources Plugin 会将project resources拷贝到输出目录.project resources are the resources associated to the main source code-->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/Crunchify</outputDirectory>
<resources>
<resource>
<directory>resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<!--说明:The dependency plugin provides the capability to manipulate artifacts. It can copy and/or unpack artifacts from local or remote repositories to a specified location-->
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/Crunchify/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<!-- This plugin provides the capability to build and sign jars.-->
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.crunchify.tutorial.CrunchifyMain</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive> <finalName>Crunchify/Crunchify</finalName>
</configuration>
</plugin>
</plugins>
</build>
最后看看结果

2、生成到一个jar包中,这个时候需要在插件maven-shade-plugin上做文章,主要用到的技术是 Resource Transformers

主要看下pom文件
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
97 <groupId>org.apache.maven.plugins</groupId>
98 <artifactId>maven-shade-plugin</artifactId>
<!-- 主要使用了Resource Transformers 做 Aggregatingclasses/resourcesfrom several artifacts into one jar-->
99 <version>1.7</version>
100 <executions>
101 <execution>
102 <phase>package</phase>
103 <goals>
104 <goal>shade</goal>
105 </goals>
106 <configuration>
107 <!-- Optional Start -->
108 <finalName>Crunchify</finalName>
109 <shadedArtifactAttached>true</shadedArtifactAttached>
110 <shadedClassifierName>jar-with-dependencies</shadedClassifierName>
111 <!-- Optional End -->
112
113 <transformers>
114 <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
115 <mainClass>com.crunchify.tutorial.CrunchifyMain</mainClass>
116 </transformer>
117 <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
118 <resource>META-INF/spring.handlers</resource>
119 </transformer>
120 <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
121 <resource>META-INF/spring.schemas</resource>
122 </transformer>
123 <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
124 <resource>META-INF/spring.tooling</resource>
125 </transformer>
126 </transformers>
127 </configuration>
128 </execution>
129 </executions>
130 </plugin> </plugins>
</build>
</project>
3、一般的,如果是spring工程,打成可执行包的话在读取xml文件时会报错,这个时候需要在mvn中做些处理,生成一个runnable的jar,直接用mvn添加一些选项。这里其实也是使用了resouce transform
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>pc-guess-like-fsf-app</finalName> //1、可执行jar包名字
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>jar-with-dependencies</shadedClassifierName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.feiniu.recom.soa.consumer.pcGuessLike.FsfTest</mainClass> //2、这里是可执行jar包主类名字
</transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.tooling</resource>
</transformer>
</transformers>
</configuration> </execution> </executions>
</plugin>
mvn生成runnablejar 的方法的更多相关文章
- .net又一个生成缩略图的方法,不变形
生成缩略图是一个十分常用功能,找到了一个方法,重写部分代码,实用又好用,.net又一个生成缩略图的方法,不变形 /// <summary> /// 为图片生成缩略图 by 何问起 /// ...
- .net又一个生成缩略图的方法,不变形,非常好用
生成缩略图是一个十分常用功能,找到了一个方法,重写部分代码,实用又好用,.net又一个生成缩略图的方法,不变形 /// <summary> /// 为图片生成缩略图 by 何问起 /// ...
- php 依据字符串生成相应数组方法
php 依据字符串生成相应数组方法 比如: <?php $config = array( 'project|page|index' => 'content', 'project|page| ...
- C#生成缩略图的方法
1.需要添加应用System.Drawing.dll 2.命名空间 using System.IO; using System.Drawing; using System.Drawing.Imagin ...
- js生成随机数的方法实例总结 [收藏]
js生成随机数的方法实例总结 js生成随机数主要用到了内置的Math对象的random()方法.用法如:Math.random().它返回的是一个 0 ~ 1 之间的随机数.有了这么一个方法,那生成任 ...
- Python生成随机数的方法
这篇文章主要介绍了Python生成随机数的方法,有需要的朋友可以参考一下 如果你对在Python生成随机数与random模块中最常用的几个函数的关系与不懂之处,下面的文章就是对Python生成随机数与 ...
- oracle手工生成AWR报告方法记录
AWR(Automatic Workload Repository)报告是我们进行日常数据库性能评定.问题SQL发现的重要手段.熟练掌握AWR报告,是做好开发.运维DBA工作的重要基本功. AWR报告 ...
- DEA快速生成get&set方法
将下图UserInfo类中的几个对象全部生成 get/set方法: 方法步骤: 1. 将光放置空白区域,按 [alt + (fn+insert)] ,或 [ alt + insert] 键! 2. ...
- C#生成MD5的方法
///C#生成MD5的方法 public static string GetMD5(string sDataIn) { MD5CryptoServiceProvider md5 = new MD5Cr ...
随机推荐
- 【转】GeoHash核心原理解析
好久没更新过博客了,先转载一篇文章吧. 源地址:http://www.cnblogs.com/LBSer/p/3310455.html 引子 机机是个好动又好学的孩子,平日里就喜欢拿着手机地图点点按按 ...
- hdu 4281(MTSP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4281 题意:给出N个点,第一个点是裁判,其他N-1个点需要裁判过去回答问题,每个点需要的时间不一样,而 ...
- LoadRunner IP欺骗(转)
直接转了篇运用LR来实现IP欺骗的文章. http://www.cnblogs.com/fnng/archive/2013/03/02/2940284.html
- java判断字符串是否为数字或中文或字母
个人认为最好的方法 *各种字符的unicode编码的范围: * 汉字:[0x4e00,0x9fa5](或十进制[19968,40869]) * 数字:[0x30,0x39](或十进制 ...
- filter应用案例二:权限控制
filter可以用来进行权限控制,比如admin文件夹下的文件只允许管理员进入,那么,可以给admin文件夹加上一个过滤器: 简单代码示例: import java.io.IOException; i ...
- 简单的c#插件框架
插件式架构,一种全新的.开放性的.高扩展性的架构体系.插件式架构设计近年来非常流行,基于插件的设计好处很多,把扩展功能从框架中剥离出来,降低了框架的复杂度,让框架更容易实现.扩展功能与框架以一种很松的 ...
- js 事件监听
addEventListener() 方法 element.addEventListener(event, function, useCapture); 第一个参数是事件的类型 (如 "cl ...
- AndroidManifest.xml权限大全
本文转 https://my.oschina.net/duwaiweb/blog/75935 问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES , ...
- js小例子(简单模糊匹配输入信息)
该例子实现的是用户输入信息或者字母时可以搜索出来,鼠标点击选择 <!DOCTYPE html> <html> <style> p{ width:200px; hei ...
- jquerymobile 基础教程
http://www.w3cplus.com/blog/tags/331.html?page=1