gradle專案打包jar

weixin_34253539發表於2017-02-21

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下可以這麼做:

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

相關文章