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 ...
随机推荐
- @property中strong跟weak的区别
strong关键字与retain关似,用了它,引用计数自动+1,用实例更能说明一切 @property (nonatomic, strong) NSString *string1; @property ...
- Android NDK开发实例教程
WINDOWS系统+ Eclipse + NDK+Android 最近开始学习Android平台开发,Android还没有玩转,Java也是刚入门,这又要开始在Android中调用C语言,需要利用ND ...
- Open judge C16H:Magical Balls 快速幂+逆元
C16H:Magical Balls 总时间限制: 1000ms 内存限制: 262144kB 描述 Wenwen has a magical ball. When put on an infin ...
- Google自己的下拉刷新组件SwipeRefreshLayout
SwipeRefreshLayout SwipeRefreshLayout字面意思就是下拉刷新的布局,继承自ViewGroup,在support v4兼容包下,但必须把你的support librar ...
- ios透明代理抓包
之前接到一些ios测试的时候,一些应用往往由于这样那样的原因(比如自实现的发包函数)导致直接使用本地ios系统的代理很难将数据代理到主机的burp或findler中,本文提供了一种解决该问题的途径 原 ...
- Ubuntu mysql
To install mysql database in the ubuntu: 1. sudo apt-get install mysql-server 2. apt-get isntall ...
- hdu 4990 Reading comprehension 二分 + 快速幂
Description Read the program below carefully then answer the question. #pragma comment(linker, " ...
- VS链接过程中与MSVCRT.lib冲突
vs代码生成有/MT,/MTd,/Md,/MDd四个编译选项,分别代表多线程.多线程调试.多线程DLL.多线程调试DLL. 编译时引用的lib分别为libcmt.li.libcmtd.lib.msvc ...
- matlab绘图--线性规划图解法示意
matlab绘图--线性规划图解法示意 图解法 matlab绘图 区域填充 线性规划问题: matlab绘图 L1=[4,0;4,4]; plot(L1(:,1),L1(:,2));hold on ...
- Excel: Switch (transpose) columns and rows
链接:https://support.office.com/en-in/article/Switch-transpose-columns-and-rows-ed1215f5-59af-47e6-953 ...