主要讲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. C[泊车管理系统]

    // //  main.c //  泊车管理系统 // //  Created by 丁小未 on 13-7-14. //  Copyright (c) 2013年 dingxiaowei. All ...

  2. java线程之——sleep()与wait()的区别

    sleep()是Thread的方法,wait()是Object的方法 如果线程进入了同步锁,sleep不会释放对象锁,wait会释放对象锁 sleep的作用就是让正在执行的线程主动让出CPU,给其它线 ...

  3. Jmeter 分布式性能测试

    作为一个纯 JAVA 的GUI应用,JMeter 对于CPU和内存的消耗还是很惊人的,所以当需要模拟数以千计的并发用户时,使用单台机器模拟所有的并发用户就有些力不从心,甚至还会引起JAVA内存溢出的错 ...

  4. 【jQuery 区别】attr()和prop()的区别

    1>>> 今天实现一个 点击更新按钮 ,可以勾选上本行的的checkbox的功能: 使用代码: /** * updateproduct.htmls 更新 产品信息 */ $(docu ...

  5. JAVA Day6

    1.对象:用来描述客观事物的一个实体,由一组属性和方法组成 2.属性--对象具有的各种特征    *每个对象的每个属性都拥有特定值    *例如:张浩和李明的年龄.姓名不一样 3.方法--对象执行的操 ...

  6. 那些Android中的性能优化

    性能优化是一个大的范畴,如果有人问你在Android中如何做性能优化的,也许都不知道从哪开始说起. 首先要明白的是,为什么我们的App需要优化,最显而易见的时刻:用户say,什么狗屎,刷这么久都没反应 ...

  7. hdu 1078 FatMouse and Cheese

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  8. JS实现选择不同select标签option值的验证

    js实现不同select标签option值的验证 功能描述: 选择中文时,匹配中文的正则表达式,选择英文选项是匹配英文的表达式,并且有对应的提示信息. html代码片段: <select id= ...

  9. 【CQgame】[下一百层] [Down]

    简单的下一百层的c++实现,代码一晚上就码完了 注意:游戏前请在 默认值 或 属性 中调整缓冲区大小,否则会输出爆屏,方法写在代码里了 觉得速度 快/慢 的可以在第 23 行手动改一下,相信大神们能看 ...

  10. mysql修改默认编码为UTF8

    Linux下一般是 /etc/my.cnf --在 [mysqld] 标签下加上三行default-character-set = utf8character_set_server = utf8 -- ...