当前位置:   article > 正文

gradle项目打包jar

gradle 打 包 all-in-one
gradle 打包可执行jar

参考:

  1. http://www.coderli.com/packag...

  2. http://www.cnblogs.com/yjmyzz...

  3. spring官方文档

方法一:不借助manifest文件

java -classpath jar1:jar2:jar3... mainClassName

解解一下:

红色的是固定部分,中间蓝色的是jar包的路径(多个jar之间用:号连接),最后绿色的部分是main方法所在的类名,按这个思路

把这二个jar包扔同一个目录下,输入如下命令:

java -classpath my-jar.jar:my-lib.jar yjmyzz.runnable.jar.DemoApp

程序就能跑起来了


方法二:借助manifest文件

想办法在my-jar.jar中添加MANIFEST.MF文件,内容参考下面这样:

Main-Class: yjmyzz.runnable.jar.DemoApp
Class-Path: my-lib.jar
同样,将这两个jar包放在一起,然后

java -jar my-jar.jar 就能运行了,至于如何在打包里,自动添加MANIFEST.MF文件,gradle下可以这么做:

  1. jar {
  2. manifest {
  3. attributes 'Main-Class': 'yjmyzz.runnable.jar.DemoApp'
  4. attributes 'Class-Path': 'my-lib.jar'
  5. }
  6. }

maven项目参考http://www.cnblogs.com/yjmyzz...

方法三:借助spring-boot 插件

前面两种方法,主程序的jar包,与依赖的jar包是分开的,这在云环境中,上传部署比较麻烦,得传多个文件(或者上传前,先压缩成一个包,再传到服务器上解压),服务器节点多时,操作起来太累。又到我大Spring出场了,将my-jar项目中的build.gradle改成下面这样:

  1. apply plugin: 'java'
  2. apply plugin: 'spring-boot'
  3. buildscript {
  4. repositories {
  5. maven {
  6. url 'http://maven.oschina.net/content/groups/public/'
  7. }
  8. }
  9. dependencies {
  10. classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.0.RELEASE")
  11. }
  12. }
  13. repositories {
  14. maven {
  15. url 'http://maven.oschina.net/content/groups/public/'
  16. }
  17. }
  18. dependencies {
  19. compile project(':my-lib') // 依赖自身的jar
  20. }

然后命令行输入 gradle bootRepackage 将在build/libs下生成二个文件my-jar.jar以及my-jar.jar.original(先不用管这个original文件是啥)

直接java -jar my-jar.jar 就能运行了(注意:这种方式下,并不需要my-lib.jar这类依赖的jar文件),其原理在于spring-boot插件把所有依赖的jar包,全都打到一个jar包里了。

基本上,到这里这篇文章就可以完结了,如果有同学对spring-boot这种打包方式比较好奇,想深入研究,可以继续向下看,把my-jar.jar.original改名为my-jar-origin.jar,然后输入jar tf my-jar-origin.jar 即显示这个jar包的内容,会得到以下输出:

  1. META-INF/
  2. META-INF/MANIFEST.MF
  3. yjmyzz/
  4. yjmyzz/runnable/
  5. yjmyzz/runnable/jar/
  6. yjmyzz/runnable/jar/DemoApp.class

即.original文件,其实就是一个普通的jar包,其中的MANIFEST.MF并没有什么实质性内容,只是一个空壳,这样的jar包,跟方法1得到的jar包是一样的。

再输入jar tf my-jar.jar,会得到以下输出:

  1. jar tf wxredpack-0.1.jar
  2. META-INF/
  3. META-INF/MANIFEST.MF
  4. com/
  5. com/mm/
  6. com/mm/springboot/
  7. com/mm/springboot/api/
  8. com/mm/springboot/api/exception/
  9. com/mm/springboot/api/exception/BaseException.class
  10. com/mm/springboot/api/exception/InvalidMchBillNoException.class
  11. com/mm/springboot/api/exception/InvalidMoneyException.class
  12. com/mm/springboot/api/exception/InvalidOpenidException.class
  13. com/mm/springboot/api/exception/InvalidRpNumException.class
  14. com/mm/springboot/api/exception/InvalidWishingException.class
  15. com/mm/springboot/api/RedPackApi.class
  16. com/mm/springboot/Application.class
  17. com/mm/springboot/common/
  18. com/mm/springboot/common/RedPackConfig.class
  19. com/mm/springboot/common/RedPackErrorStatus.class
  20. com/mm/springboot/controller/
  21. com/mm/springboot/controller/FreemarkerController.class
  22. com/mm/springboot/controller/UserController.class
  23. com/mm/springboot/controller/WxRedPackController.class
  24. com/mm/springboot/entity/
  25. com/mm/springboot/entity/BaseEntity.class
  26. com/mm/springboot/entity/NormalRedPack.class
  27. com/mm/springboot/entity/User.class
  28. com/mm/springboot/handler/
  29. com/mm/springboot/handler/GlobalExceptionHandler.class
  30. com/mm/springboot/listener/
  31. com/mm/springboot/listener/ApplicationStartedEventListener.class
  32. com/mm/springboot/repository/
  33. com/mm/springboot/repository/UserRepository.class
  34. com/mm/springboot/service/
  35. com/mm/springboot/service/RedPackService.class
  36. com/mm/springboot/service/UserService.class
  37. com/mm/springboot/utils/
  38. com/mm/springboot/utils/ReadPackUtils.class
  39. com/mm/springboot/utils/WebUtils.class
  40. application-dev.properties
  41. application-test.properties
  42. application.properties
  43. import.sql
  44. static/
  45. static/js/
  46. static/js/a.js
  47. templates/
  48. templates/hello.ftl
  49. lib/
  50. lib/spring-boot-starter-web-1.3.0.RELEASE.jar
  51. lib/spring-boot-starter-data-jpa-1.3.0.RELEASE.jar
  52. lib/spring-boot-starter-freemarker-1.3.0.RELEASE.jar
  53. lib/jfinal-weixin-1.7.jar
  54. lib/jfinal-2.2.jar
  55. lib/spring-boot-starter-1.3.0.RELEASE.jar
  56. lib/spring-boot-starter-tomcat-1.3.0.RELEASE.jar
  57. lib/spring-boot-starter-validation-1.3.0.RELEASE.jar
  58. lib/jackson-databind-2.6.3.jar
  59. lib/spring-web-4.2.3.RELEASE.jar
  60. lib/spring-webmvc-4.2.3.RELEASE.jar
  61. lib/spring-boot-starter-aop-1.3.0.RELEASE.jar
  62. lib/spring-boot-starter-jdbc-1.3.0.RELEASE.jar
  63. lib/hibernate-entitymanager-4.3.11.Final.jar
  64. lib/javax.transaction-api-1.2.jar
  65. lib/spring-data-jpa-1.9.1.RELEASE.jar
  66. lib/spring-aspects-4.2.3.RELEASE.jar
  67. lib/freemarker-2.3.23.jar
  68. lib/spring-context-support-4.2.3.RELEASE.jar
  69. lib/cglib-nodep-3.1.jar
  70. lib/spring-boot-1.3.0.RELEASE.jar
  71. lib/spring-boot-autoconfigure-1.3.0.RELEASE.jar
  72. lib/spring-boot-starter-logging-1.3.0.RELEASE.jar
  73. lib/spring-core-4.2.3.RELEASE.jar
  74. lib/snakeyaml-1.16.jar
  75. lib/tomcat-embed-core-8.0.28.jar
  76. lib/tomcat-embed-el-8.0.28.jar
  77. lib/tomcat-embed-logging-juli-8.0.28.jar
  78. lib/tomcat-embed-websocket-8.0.28.jar
  79. lib/hibernate-validator-5.2.2.Final.jar
  80. lib/jackson-annotations-2.6.3.jar
  81. lib/jackson-core-2.6.3.jar
  82. lib/spring-aop-4.2.3.RELEASE.jar
  83. lib/spring-beans-4.2.3.RELEASE.jar
  84. lib/spring-context-4.2.3.RELEASE.jar
  85. lib/spring-expression-4.2.3.RELEASE.jar
  86. lib/aspectjweaver-1.8.7.jar
  87. lib/tomcat-jdbc-8.0.28.jar
  88. lib/spring-jdbc-4.2.3.RELEASE.jar
  89. lib/jboss-logging-3.3.0.Final.jar
  90. lib/jboss-logging-annotations-1.2.0.Beta1.jar
  91. lib/hibernate-core-4.3.11.Final.jar
  92. lib/dom4j-1.6.1.jar
  93. lib/hibernate-commons-annotations-4.0.5.Final.jar
  94. lib/hibernate-jpa-2.1-api-1.0.0.Final.jar
  95. lib/javassist-3.18.1-GA.jar
  96. lib/spring-data-commons-1.11.1.RELEASE.jar
  97. lib/spring-orm-4.2.3.RELEASE.jar
  98. lib/spring-tx-4.2.3.RELEASE.jar
  99. lib/slf4j-api-1.7.13.jar
  100. lib/jcl-over-slf4j-1.7.13.jar
  101. lib/logback-classic-1.1.3.jar
  102. lib/jul-to-slf4j-1.7.13.jar
  103. lib/log4j-over-slf4j-1.7.13.jar
  104. lib/validation-api-1.1.0.Final.jar
  105. lib/classmate-1.1.0.jar
  106. lib/aopalliance-1.0.jar
  107. lib/tomcat-juli-8.0.28.jar
  108. lib/antlr-2.7.7.jar
  109. lib/jandex-1.1.0.Final.jar
  110. lib/xml-apis-1.0.b2.jar
  111. lib/logback-core-1.1.3.jar
  112. lib/h2-1.4.190.jar
  113. org/
  114. org/springframework/
  115. org/springframework/boot/
  116. org/springframework/boot/loader/
  117. org/springframework/boot/loader/LaunchedURLClassLoader$Java7LockProvider.class
  118. org/springframework/boot/loader/PropertiesLauncher$ArchiveEntryFilter.class
  119. org/springframework/boot/loader/PropertiesLauncher$PrefixMatchingArchiveFilter.class
  120. org/springframework/boot/loader/ExecutableArchiveLauncher$1.class
  121. org/springframework/boot/loader/PropertiesLauncher.class
  122. org/springframework/boot/loader/LaunchedURLClassLoader$ResourceEnumeration.class
  123. org/springframework/boot/loader/data/
  124. org/springframework/boot/loader/data/ByteArrayRandomAccessData.class
  125. org/springframework/boot/loader/data/RandomAccessDataFile$DataInputStream.class
  126. org/springframework/boot/loader/data/RandomAccessData.class
  127. org/springframework/boot/loader/data/RandomAccessDataFile$FilePool.class
  128. org/springframework/boot/loader/data/RandomAccessDataFile.class
  129. org/springframework/boot/loader/data/RandomAccessData$ResourceAccess.class
  130. org/springframework/boot/loader/util/
  131. org/springframework/boot/loader/util/SystemPropertyUtils.class
  132. org/springframework/boot/loader/util/AsciiBytes.class
  133. org/springframework/boot/loader/LaunchedURLClassLoader$1.class
  134. org/springframework/boot/loader/InputArgumentsJavaAgentDetector.class
  135. org/springframework/boot/loader/Launcher.class
  136. org/springframework/boot/loader/LaunchedURLClassLoader.class
  137. org/springframework/boot/loader/JarLauncher.class
  138. org/springframework/boot/loader/jar/
  139. org/springframework/boot/loader/jar/JarEntryFilter.class
  140. org/springframework/boot/loader/jar/JarURLConnection.class
  141. org/springframework/boot/loader/jar/JarEntry.class
  142. org/springframework/boot/loader/jar/Bytes.class
  143. org/springframework/boot/loader/jar/CentralDirectoryEndRecord.class
  144. org/springframework/boot/loader/jar/JarFile$2.class
  145. org/springframework/boot/loader/jar/ZipInflaterInputStream.class
  146. org/springframework/boot/loader/jar/JarFile.class
  147. org/springframework/boot/loader/jar/JarFile$1.class
  148. org/springframework/boot/loader/jar/JarURLConnection$1.class
  149. org/springframework/boot/loader/jar/Handler.class
  150. org/springframework/boot/loader/jar/JarURLConnection$JarEntryName.class
  151. org/springframework/boot/loader/jar/JarEntryData.class
  152. org/springframework/boot/loader/MainMethodRunner.class
  153. org/springframework/boot/loader/InputArgumentsJavaAgentDetector$1.class
  154. org/springframework/boot/loader/WarLauncher.class
  155. org/springframework/boot/loader/PropertiesLauncher$1.class
  156. org/springframework/boot/loader/ExecutableArchiveLauncher.class
  157. org/springframework/boot/loader/LaunchedURLClassLoader$LockProvider.class
  158. org/springframework/boot/loader/archive/
  159. org/springframework/boot/loader/archive/JarFileArchive$JarFileEntry.class
  160. org/springframework/boot/loader/archive/JarFileArchive.class
  161. org/springframework/boot/loader/archive/FilteredArchive.class
  162. org/springframework/boot/loader/archive/JarFileArchive$1.class
  163. org/springframework/boot/loader/archive/ExplodedArchive.class
  164. org/springframework/boot/loader/archive/FilteredArchive$2.class
  165. org/springframework/boot/loader/archive/Archive$Entry.class
  166. org/springframework/boot/loader/archive/ExplodedArchive$1.class
  167. org/springframework/boot/loader/archive/Archive$EntryFilter.class
  168. org/springframework/boot/loader/archive/FilteredArchive$1.class
  169. org/springframework/boot/loader/archive/ExplodedArchive$FileEntry.class
  170. org/springframework/boot/loader/archive/Archive.class
  171. org/springframework/boot/loader/archive/Archive$EntryRenameFilter.class
  172. org/springframework/boot/loader/archive/ExplodedArchive$FilteredURLStreamHandler.class
  173. org/springframework/boot/loader/archive/ExplodedArchive$FileNotFoundURLConnection.class
  174. org/springframework/boot/loader/JavaAgentDetector.class

很明显,多出了很多内容,注意lib/,可以发现依赖的jar包,已经打包到my-jar.jar内部了,这时的MANIFEST.MF内容为:

  1. 1 Manifest-Version: 1.0
  2. 2 Start-Class: yjmyzz.runnable.jar.DemoApp
  3. 3 Spring-Boot-Version: 1.3.0.RELEASE
  4. 4 Main-Class: org.springframework.boot.loader.JarLauncher

Main-Class被设置成org.springframework.boot.loader.JarLauncher,此外还增加了Start-Class指向我们真正的程序入口yjmyzz.runnable.jar.DemoApp,换句话说,程序运行时,先调用org.springframework.boot.loader.JarLauncher,然后找到Start-Class对应的类,最终运行,运行过程中,会查找内部lib下的依赖jar包my-lib.jar,当然这一切是需要有额外的代码来处理的,所以多出来的org/springframework/boot下的一堆class,就是spring用来干这件事儿的。

第三种方式,很适合云环境的部署,只需要扔一个jar包上去就完事了,这种all-in-one的jar包,也被换为fat-jar

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/265518
推荐阅读
相关标签
  

闽ICP备14008679号