簡體   English   中英

如何讓gradle到package都依賴jar?

[英]How to make gradle to package all dependency jar?

我正在使用 Gradle 4.0 到 package 一個項目到 jar。這是我的 build.gradle:

group 'dolphin'
version '1.0-SNAPSHOT'

buildscript {
    ext {
        springBootVersion = '1.4.5.RELEASE'
        springVersion = '4.3.7.RELEASE'
        springfoxVersion = '2.6.1'
        jacksonVersion = '2.8.7'
        lombokVersion = '1.16.14'
    }
    ext['tomcat.version'] = '8.0.35'

    repositories {
        mavenCentral()
        jcenter{
            url 'http://jcenter.bintray.com'
        }
    }

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")

    }
}

def getVersionCode() {
    def versionFile = file("$rootDir/version.properties")
    if (!versionFile.canRead()) {
        throw new GradleException("Could not find version.properties!")
    }
    def versionProps = new Properties()
    versionProps.load(new FileInputStream(versionFile))
    def versionCode = versionProps['VERSION'].toString()
    return versionCode
}

repositories {
    mavenCentral()
}

allprojects {
    apply plugin: 'java'
    apply plugin: 'org.springframework.boot'
    repositories {
        mavenCentral()
    }
}


task wrapper(type: Wrapper) {
    description = 'Generates gradlew[.bat] scripts'
    gradleVersion = '4.0'
}


project(":common") {
    description = ''

    dependencies {
        compile("org.springframework:spring-context:" + springVersion)
        compile("commons-codec:commons-codec:1.10")
        compile("org.apache.tomcat:tomcat-juli:" + property('tomcat.version'))
        compile 'org.springframework.boot:spring-boot-starter-web'
        compile("io.springfox:springfox-swagger2:${springfoxVersion}")
        compile group: 'io.swagger', name: 'swagger-annotations', version: '1.5.20'
        compile("org.projectlombok:lombok:${lombokVersion}")
        compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.5'
        compile group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '1.3.0'
        compile group: 'org.mybatis', name: 'mybatis', version: '3.4.4'
        compile group: 'org.mybatis', name: 'mybatis-typehandlers-jsr310', version: '1.0.2'
    }
}

/*project(":api") {

    description = 'dolphin-api'

    dependencies {
        compile project(":business")
        compile project(":data")
        compile project(":composite")
        compile project(":common")
        compile group: 'org.apache.tomcat', name: 'tomcat-juli', version: property('tomcat.version')
        compile("org.projectlombok:lombok:1.16.10")
        compile("org.springframework.boot:spring-boot-devtools")
        compile("org.springframework:spring-test:" + springVersion)
        compile("org.springframework.boot:spring-boot-test:" + springBootVersion)
        testCompile group: 'junit', name: 'junit', version: '4.12'
    }
}*/

project(":composite") {

    description = 'dolphin-composite'

    dependencies {
        compile project(":business")
        compile project(":data")
        compile("org.springframework:spring-context:" + springVersion)
    }
}


project(":web") {

    description = "web"

    jar {
        baseName = "dolphin-web-" + getVersionCode()
    }

    dependencies {
        implementation project(':business')
        implementation project(':api')
        implementation project(':common')
        implementation project(':data')
        implementation project(':composite')
        implementation("com.zaxxer:HikariCP:2.6.0")
        implementation("mysql:mysql-connector-java:5.1.24")
        implementation("org.springframework.boot:spring-boot-starter-web")
        implementation("org.springframework.boot:spring-boot-starter")
        implementation group: 'org.apache.tomcat', name: 'tomcat-juli', version: property('tomcat.version')
        implementation("org.projectlombok:lombok:1.16.14")
        implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.5'
        implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.24'
        implementation group: 'org.mybatis', name: 'mybatis', version: '3.4.2'
        implementation group: 'org.mybatis', name: 'mybatis-typehandlers-jsr310', version: '1.0.2'
        testCompile group: 'junit', name: 'junit', version: '4.12'
    }
}

project(":business") {

    description = "business"

    dependencies {
        compile project(':data')
        compile project(':common')
        compile('org.springframework.boot:spring-boot-starter-web')
    }
}

project(":data") {

    description = "data"

    dependencies {
        compile project(':dolphin-mybatis')
        compile project(':common')
        compile("org.projectlombok:lombok:${lombokVersion}")
        compile("com.zaxxer:HikariCP:2.6.0")
        compile group: 'org.postgresql', name: 'postgresql', version: '42.1.4'
        compile("org.hibernate:hibernate-validator:5.2.4.Final")
        compile("org.mybatis.spring.boot:mybatis-spring-boot-starter:1.1.1")
        compile("org.apache.commons:commons-lang3:3.5")
        compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.24'
        compile group: 'org.mybatis', name: 'mybatis', version: '3.4.2'
        compile group: 'org.mybatis', name: 'mybatis-typehandlers-jsr310', version: '1.0.2'
        testCompile group: 'junit', name: 'junit', version: '4.11'
        compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonVersion")

    }
}

project(":dolphin-mybatis") {

    description = "dolphin-mybatis"

    dependencies {

    }
}

當我使用命令 package 所有項目到單個 jar 時,區分 jar 文件不包含依賴項。只有 104KB。如何修復它?這是我的 package 命令:

./gradlew -p web -x test build

java 版本“1.8.0_112”

默認的jar任務僅捆綁目標項目中的已編譯類。 如果你想將 package 它的依賴關系變成所謂的fat jar ,你可以隨時調整jar任務(或類型為Jar的自定義任務),從運行時配置中手動添加元素,如下所示:

jar {
    // Will include every single one of your dependencies, project or not
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

或者更好的是,使用專用插件,例如Gradle Shadow

buildscript {
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:4.0.2'
    }
}

apply plugin: 'com.github.johnrengelman.shadow'    

...

// Outputs to build/libs/dolphin-web-<version>.jar
shadowJar {
   baseName = 'dolphin-web'
   classifier = null
   version = getVersionCode()
}

使用 Gradle 7+,您可以像這樣在分發存檔中 package 依賴項(第三方)jars:

configurations {
    runtimeLib.extendsFrom implementation
}

task dist(type: Zip) {
    into('MyProjectName') {
        from "$workDir"
        exclude "log/*", "temp"
        into('lib') {
            from configurations.runtimeLib
        }
        includeEmptyDirs = true
    }
}

或者,您可以使用 Gradle Distribution 插件 它將收集依賴項 jars,還將創建啟動腳本(在項目“build/distributions”目錄中)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM