gradle 打包可执行jar
参考:
方法一:不借助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下可以这么做:
- jar {
- manifest {
- attributes 'Main-Class': 'yjmyzz.runnable.jar.DemoApp'
- attributes 'Class-Path': 'my-lib.jar'
- }
- }
maven项目参考http://www.cnblogs.com/yjmyzz...
方法三:借助spring-boot 插件
前面两种方法,主程序的jar包,与依赖的jar包是分开的,这在云环境中,上传部署比较麻烦,得传多个文件(或者上传前,先压缩成一个包,再传到服务器上解压),服务器节点多时,操作起来太累。又到我大Spring出场了,将my-jar项目中的build.gradle改成下面这样:
- apply plugin: 'java'
- apply plugin: 'spring-boot'
-
- buildscript {
- repositories {
- maven {
- url 'http://maven.oschina.net/content/groups/public/'
- }
- }
-
- dependencies {
- classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.0.RELEASE")
- }
- }
-
- repositories {
- maven {
- url 'http://maven.oschina.net/content/groups/public/'
- }
- }
-
- dependencies {
- compile project(':my-lib') // 依赖自身的jar
- }
然后命令行输入 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包的内容,会得到以下输出:
- META-INF/
- META-INF/MANIFEST.MF
- yjmyzz/
- yjmyzz/runnable/
- yjmyzz/runnable/jar/
- yjmyzz/runnable/jar/DemoApp.class
即.original文件,其实就是一个普通的jar包,其中的MANIFEST.MF并没有什么实质性内容,只是一个空壳,这样的jar包,跟方法1得到的jar包是一样的。
再输入jar tf my-jar.jar,会得到以下输出:
- jar tf wxredpack-0.1.jar
- META-INF/
- META-INF/MANIFEST.MF
- com/
- com/mm/
- com/mm/springboot/
- com/mm/springboot/api/
- com/mm/springboot/api/exception/
- com/mm/springboot/api/exception/BaseException.class
- com/mm/springboot/api/exception/InvalidMchBillNoException.class
- com/mm/springboot/api/exception/InvalidMoneyException.class
- com/mm/springboot/api/exception/InvalidOpenidException.class
- com/mm/springboot/api/exception/InvalidRpNumException.class
- com/mm/springboot/api/exception/InvalidWishingException.class
- com/mm/springboot/api/RedPackApi.class
- com/mm/springboot/Application.class
- com/mm/springboot/common/
- com/mm/springboot/common/RedPackConfig.class
- com/mm/springboot/common/RedPackErrorStatus.class
- com/mm/springboot/controller/
- com/mm/springboot/controller/FreemarkerController.class
- com/mm/springboot/controller/UserController.class
- com/mm/springboot/controller/WxRedPackController.class
- com/mm/springboot/entity/
- com/mm/springboot/entity/BaseEntity.class
- com/mm/springboot/entity/NormalRedPack.class
- com/mm/springboot/entity/User.class
- com/mm/springboot/handler/
- com/mm/springboot/handler/GlobalExceptionHandler.class
- com/mm/springboot/listener/
- com/mm/springboot/listener/ApplicationStartedEventListener.class
- com/mm/springboot/repository/
- com/mm/springboot/repository/UserRepository.class
- com/mm/springboot/service/
- com/mm/springboot/service/RedPackService.class
- com/mm/springboot/service/UserService.class
- com/mm/springboot/utils/
- com/mm/springboot/utils/ReadPackUtils.class
- com/mm/springboot/utils/WebUtils.class
- application-dev.properties
- application-test.properties
- application.properties
- import.sql
- static/
- static/js/
- static/js/a.js
- templates/
- templates/hello.ftl
- lib/
- lib/spring-boot-starter-web-1.3.0.RELEASE.jar
- lib/spring-boot-starter-data-jpa-1.3.0.RELEASE.jar
- lib/spring-boot-starter-freemarker-1.3.0.RELEASE.jar
- lib/jfinal-weixin-1.7.jar
- lib/jfinal-2.2.jar
- lib/spring-boot-starter-1.3.0.RELEASE.jar
- lib/spring-boot-starter-tomcat-1.3.0.RELEASE.jar
- lib/spring-boot-starter-validation-1.3.0.RELEASE.jar
- lib/jackson-databind-2.6.3.jar
- lib/spring-web-4.2.3.RELEASE.jar
- lib/spring-webmvc-4.2.3.RELEASE.jar
- lib/spring-boot-starter-aop-1.3.0.RELEASE.jar
- lib/spring-boot-starter-jdbc-1.3.0.RELEASE.jar
- lib/hibernate-entitymanager-4.3.11.Final.jar
- lib/javax.transaction-api-1.2.jar
- lib/spring-data-jpa-1.9.1.RELEASE.jar
- lib/spring-aspects-4.2.3.RELEASE.jar
- lib/freemarker-2.3.23.jar
- lib/spring-context-support-4.2.3.RELEASE.jar
- lib/cglib-nodep-3.1.jar
- lib/spring-boot-1.3.0.RELEASE.jar
- lib/spring-boot-autoconfigure-1.3.0.RELEASE.jar
- lib/spring-boot-starter-logging-1.3.0.RELEASE.jar
- lib/spring-core-4.2.3.RELEASE.jar
- lib/snakeyaml-1.16.jar
- lib/tomcat-embed-core-8.0.28.jar
- lib/tomcat-embed-el-8.0.28.jar
- lib/tomcat-embed-logging-juli-8.0.28.jar
- lib/tomcat-embed-websocket-8.0.28.jar
- lib/hibernate-validator-5.2.2.Final.jar
- lib/jackson-annotations-2.6.3.jar
- lib/jackson-core-2.6.3.jar
- lib/spring-aop-4.2.3.RELEASE.jar
- lib/spring-beans-4.2.3.RELEASE.jar
- lib/spring-context-4.2.3.RELEASE.jar
- lib/spring-expression-4.2.3.RELEASE.jar
- lib/aspectjweaver-1.8.7.jar
- lib/tomcat-jdbc-8.0.28.jar
- lib/spring-jdbc-4.2.3.RELEASE.jar
- lib/jboss-logging-3.3.0.Final.jar
- lib/jboss-logging-annotations-1.2.0.Beta1.jar
- lib/hibernate-core-4.3.11.Final.jar
- lib/dom4j-1.6.1.jar
- lib/hibernate-commons-annotations-4.0.5.Final.jar
- lib/hibernate-jpa-2.1-api-1.0.0.Final.jar
- lib/javassist-3.18.1-GA.jar
- lib/spring-data-commons-1.11.1.RELEASE.jar
- lib/spring-orm-4.2.3.RELEASE.jar
- lib/spring-tx-4.2.3.RELEASE.jar
- lib/slf4j-api-1.7.13.jar
- lib/jcl-over-slf4j-1.7.13.jar
- lib/logback-classic-1.1.3.jar
- lib/jul-to-slf4j-1.7.13.jar
- lib/log4j-over-slf4j-1.7.13.jar
- lib/validation-api-1.1.0.Final.jar
- lib/classmate-1.1.0.jar
- lib/aopalliance-1.0.jar
- lib/tomcat-juli-8.0.28.jar
- lib/antlr-2.7.7.jar
- lib/jandex-1.1.0.Final.jar
- lib/xml-apis-1.0.b2.jar
- lib/logback-core-1.1.3.jar
- lib/h2-1.4.190.jar
- org/
- org/springframework/
- org/springframework/boot/
- org/springframework/boot/loader/
- org/springframework/boot/loader/LaunchedURLClassLoader$Java7LockProvider.class
- org/springframework/boot/loader/PropertiesLauncher$ArchiveEntryFilter.class
- org/springframework/boot/loader/PropertiesLauncher$PrefixMatchingArchiveFilter.class
- org/springframework/boot/loader/ExecutableArchiveLauncher$1.class
- org/springframework/boot/loader/PropertiesLauncher.class
- org/springframework/boot/loader/LaunchedURLClassLoader$ResourceEnumeration.class
- org/springframework/boot/loader/data/
- org/springframework/boot/loader/data/ByteArrayRandomAccessData.class
- org/springframework/boot/loader/data/RandomAccessDataFile$DataInputStream.class
- org/springframework/boot/loader/data/RandomAccessData.class
- org/springframework/boot/loader/data/RandomAccessDataFile$FilePool.class
- org/springframework/boot/loader/data/RandomAccessDataFile.class
- org/springframework/boot/loader/data/RandomAccessData$ResourceAccess.class
- org/springframework/boot/loader/util/
- org/springframework/boot/loader/util/SystemPropertyUtils.class
- org/springframework/boot/loader/util/AsciiBytes.class
- org/springframework/boot/loader/LaunchedURLClassLoader$1.class
- org/springframework/boot/loader/InputArgumentsJavaAgentDetector.class
- org/springframework/boot/loader/Launcher.class
- org/springframework/boot/loader/LaunchedURLClassLoader.class
- org/springframework/boot/loader/JarLauncher.class
- org/springframework/boot/loader/jar/
- org/springframework/boot/loader/jar/JarEntryFilter.class
- org/springframework/boot/loader/jar/JarURLConnection.class
- org/springframework/boot/loader/jar/JarEntry.class
- org/springframework/boot/loader/jar/Bytes.class
- org/springframework/boot/loader/jar/CentralDirectoryEndRecord.class
- org/springframework/boot/loader/jar/JarFile$2.class
- org/springframework/boot/loader/jar/ZipInflaterInputStream.class
- org/springframework/boot/loader/jar/JarFile.class
- org/springframework/boot/loader/jar/JarFile$1.class
- org/springframework/boot/loader/jar/JarURLConnection$1.class
- org/springframework/boot/loader/jar/Handler.class
- org/springframework/boot/loader/jar/JarURLConnection$JarEntryName.class
- org/springframework/boot/loader/jar/JarEntryData.class
- org/springframework/boot/loader/MainMethodRunner.class
- org/springframework/boot/loader/InputArgumentsJavaAgentDetector$1.class
- org/springframework/boot/loader/WarLauncher.class
- org/springframework/boot/loader/PropertiesLauncher$1.class
- org/springframework/boot/loader/ExecutableArchiveLauncher.class
- org/springframework/boot/loader/LaunchedURLClassLoader$LockProvider.class
- org/springframework/boot/loader/archive/
- org/springframework/boot/loader/archive/JarFileArchive$JarFileEntry.class
- org/springframework/boot/loader/archive/JarFileArchive.class
- org/springframework/boot/loader/archive/FilteredArchive.class
- org/springframework/boot/loader/archive/JarFileArchive$1.class
- org/springframework/boot/loader/archive/ExplodedArchive.class
- org/springframework/boot/loader/archive/FilteredArchive$2.class
- org/springframework/boot/loader/archive/Archive$Entry.class
- org/springframework/boot/loader/archive/ExplodedArchive$1.class
- org/springframework/boot/loader/archive/Archive$EntryFilter.class
- org/springframework/boot/loader/archive/FilteredArchive$1.class
- org/springframework/boot/loader/archive/ExplodedArchive$FileEntry.class
- org/springframework/boot/loader/archive/Archive.class
- org/springframework/boot/loader/archive/Archive$EntryRenameFilter.class
- org/springframework/boot/loader/archive/ExplodedArchive$FilteredURLStreamHandler.class
- org/springframework/boot/loader/archive/ExplodedArchive$FileNotFoundURLConnection.class
- org/springframework/boot/loader/JavaAgentDetector.class
很明显,多出了很多内容,注意lib/,可以发现依赖的jar包,已经打包到my-jar.jar内部了,这时的MANIFEST.MF内容为:
- 1 Manifest-Version: 1.0
- 2 Start-Class: yjmyzz.runnable.jar.DemoApp
- 3 Spring-Boot-Version: 1.3.0.RELEASE
- 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