主要讲3点,生成runnable jar 方法1是生成一个目录 方法2是直接一个runnable的jar 方法3是关于包含spring工程的情况  方法2和3其实是一致的

1、生成runnable jar,但是最后并不是一个可执行的jar 而是一个目录

主要使用 三个插件 将所需要的资源拷贝到 /target/Crunchify目录下,注意并没有生成一个jar中去

maven-resources-pluginmaven-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.

几点说明:

  1. CrunchifyMavenBuildPlugins is a Maven Project. If you have Java project and wanted to convert it into Maven project then follow this tutorial.
  2. We do have two folders. src and resources.
  3. Inside resources folder we do have a folder called Scripts which contains one executableshell script file.
  4. CrunchifyMain.java is a main starting point which has main(String args[]) method inside.
  5. pom.xml file 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 做 Aggregating classes/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 的方法的更多相关文章

  1. .net又一个生成缩略图的方法,不变形

    生成缩略图是一个十分常用功能,找到了一个方法,重写部分代码,实用又好用,.net又一个生成缩略图的方法,不变形 /// <summary> /// 为图片生成缩略图 by 何问起 /// ...

  2. .net又一个生成缩略图的方法,不变形,非常好用

    生成缩略图是一个十分常用功能,找到了一个方法,重写部分代码,实用又好用,.net又一个生成缩略图的方法,不变形 /// <summary> /// 为图片生成缩略图 by 何问起 /// ...

  3. php 依据字符串生成相应数组方法

    php 依据字符串生成相应数组方法 比如: <?php $config = array( 'project|page|index' => 'content', 'project|page| ...

  4. C#生成缩略图的方法

    1.需要添加应用System.Drawing.dll 2.命名空间 using System.IO; using System.Drawing; using System.Drawing.Imagin ...

  5. js生成随机数的方法实例总结 [收藏]

    js生成随机数的方法实例总结 js生成随机数主要用到了内置的Math对象的random()方法.用法如:Math.random().它返回的是一个 0 ~ 1 之间的随机数.有了这么一个方法,那生成任 ...

  6. Python生成随机数的方法

    这篇文章主要介绍了Python生成随机数的方法,有需要的朋友可以参考一下 如果你对在Python生成随机数与random模块中最常用的几个函数的关系与不懂之处,下面的文章就是对Python生成随机数与 ...

  7. oracle手工生成AWR报告方法记录

    AWR(Automatic Workload Repository)报告是我们进行日常数据库性能评定.问题SQL发现的重要手段.熟练掌握AWR报告,是做好开发.运维DBA工作的重要基本功. AWR报告 ...

  8. DEA快速生成get&set方法

    将下图UserInfo类中的几个对象全部生成 get/set方法: 方法步骤: 1.   将光放置空白区域,按 [alt + (fn+insert)] ,或 [ alt + insert] 键! 2. ...

  9. C#生成MD5的方法

    ///C#生成MD5的方法 public static string GetMD5(string sDataIn) { MD5CryptoServiceProvider md5 = new MD5Cr ...

随机推荐

  1. 安卓app设计规范整理和Android APP设计篇(转)

    随着安卓智能手机不停的更新换代.安卓手机系统越来越完美,屏幕尺寸也越来越大啦!比如最近小米的miui 6的发布和魅族手机系统的更新等等. 以小米MIUI6的安卓手机来说,MIUI6进行了全新设计,坚持 ...

  2. Mysql怎样取消错误命令

    1.补上分号. 2.quit 3.由于Mysql中,‘号和"号都是成对出现的,故当错误键入'号或"号时,需要补全另一半才能退出.

  3. 数据库ORM框架GreenDao

    常用的数据库: 1). Sql Server2). Access3). Oracle4). Sysbase5). MySql6). Informix7). FoxPro8). PostgreSQL9) ...

  4. Java学习随笔1:Java是值传递还是引用传递?

    Java always passes arguments by value NOT by reference. Let me explain this through an example: publ ...

  5. Base64编码保存到文件服务器

    byte[] buffer = Convert.FromBase64String(param.Base64Code); System.Net.WebClient webClient = new Sys ...

  6. 【maven】 maven的setting.xml文件的详解

    1       Maven的安装 安装Maven之前要确保已经安装好了jdk,并且配置好了环境变量JAVA_HOME.具体安装步骤如下: 从apache网上下载maven项目的压缩包.下载地址为:ht ...

  7. phpexecel 导入导出,格式

    1.日期时间合并到c中 =a1 &b1 或 =a1 + b1 这些都是运算符 2.此时c1的值是这条公式,而并不是公式运算的结果 复制c,粘贴到d,选择粘贴值 3.此时c是时间日期格式的,如需 ...

  8. PHP之数据类型

    1.PHP字符串(String):一个字符串是一串字符的序列,就像"Hello world!":可以将任何文本放在单引号和双引号中: <?php $x="Hello ...

  9. SPOJ DISUBSTR 后缀数组

    题目链接:http://www.spoj.com/problems/DISUBSTR/en/ 题意:给定一个字符串,求不相同的子串个数. 思路:直接根据09年oi论文<<后缀数组——出来字 ...

  10. Uva12206 Stammering Aliens 后缀数组&&Hash

    Dr. Ellie Arroway has established contact with an extraterrestrial civilization. However, all effort ...