首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使gradle打包所有依赖jar?

如何使gradle打包所有依赖jar?
EN

Stack Overflow用户
提问于 2019-03-13 13:04:52
回答 2查看 5.1K关注 0票数 2

我正在使用Gradle 4.0将一个项目打包到jar.This是我的build.gradle:

代码语言:javascript
复制
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 {

    }
}

当我使用命令将所有项目打包到一个jar中时,distination文件不包含dependencies.Only有104KB.How来修复它?这是我的package命令:

代码语言:javascript
复制
./gradlew -p web -x test build

java版本"1.8.0_112“

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-13 13:33:20

默认的jar任务仅捆绑从目标项目编译的类。如果您想将其依赖项打包到所谓的胖jar中,您可以随时调整jar任务(或类型为Jar的自定义任务),手动从运行时配置中添加元素,如下所示:

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

或者更好的是,使用一个专用插件,比如阶梯式阴影

代码语言:javascript
复制
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()
}
票数 1
EN

Stack Overflow用户

发布于 2022-03-13 08:04:40

使用Gradle 7+,您可以将依赖项(第三方) jars打包到您的发行版存档中,如下所示:

代码语言:javascript
复制
configurations {
    runtimeLib.extendsFrom implementation
}

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

或者,您也可以使用Gradle 分配插件。它将收集依赖jars并创建启动脚本(在项目“构建/发行版”目录中)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55142587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档