Gradle - 将依赖和资源文件打入jar包

Posted 龙畅

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gradle - 将依赖和资源文件打入jar包相关的知识,希望对你有一定的参考价值。

https://www.cnblogs.com/jyx140521/p/6855210.html

用以下build.gradle打包出来的jar包,依赖是分离的:

apply plugin: \'java\'

dependencies {
    compile \'commons-codec:commons-codec:1.4\'
    compile \'commons-logging:commons-logging:1.1.1\'
    compile \'com.google.code.gson:gson:2.4\'
    compile \'org.apache.httpcomponents:httpclient:4.3.6\'
    compile \'com.strategicgains:RestExpress:0.11.2\'
    compile \'com.fasterxml.jackson.core:jackson-databind:2.6.4\'
    compile \'com.fasterxml.jackson.core:jackson-core:2.6.4\'
    compile \'com.fasterxml.jackson.core:jackson-annotations:2.6.4\'
    compile "ch.qos.logback:logback-core:1.1.3"
    compile "ch.qos.logback:logback-classic:1.1.3"
    compile \'net.kencochrane.raven:raven-logback:6.0.0\'
    compile \'net.kencochrane.raven:raven:6.0.0\'
    compile "org.slf4j:slf4j-api:1.7.13"
    compile \'com.rabbitmq:amqp-client:4.1.0\'
    compile \'org.apache.commons:commons-lang3:3.4\'
    compile \'commons-net:commons-net:3.4\'
    compile \'org.zeromq:jeromq:0.3.5\'
    compile fileTree(dir: \'libs\', include: [\'*.jar\'])
    compile project(\':tc-das\')
    compile project(\':result-compare\')
}

jar {
    manifest {
        attributes(
                "Manifest-Version": 1.0,
                "Main-Class": "com.testbird.rio.Main",
                "Class-Path": configurations.compile.collect { "lib/${it.name}" }.join(\' \'))
    }
}

将build.gradle修改一下,就能将依赖和资源文件打入jar包了:

apply plugin: \'java\'

dependencies {
    compile \'commons-codec:commons-codec:1.4\'
    compile \'commons-logging:commons-logging:1.1.1\'
    compile \'com.google.code.gson:gson:2.4\'
    compile \'org.apache.httpcomponents:httpclient:4.3.6\'
    compile \'com.strategicgains:RestExpress:0.11.2\'
    compile \'com.fasterxml.jackson.core:jackson-databind:2.6.4\'
    compile \'com.fasterxml.jackson.core:jackson-core:2.6.4\'
    compile \'com.fasterxml.jackson.core:jackson-annotations:2.6.4\'
    compile "ch.qos.logback:logback-core:1.1.3"
    compile "ch.qos.logback:logback-classic:1.1.3"
    compile \'net.kencochrane.raven:raven-logback:6.0.0\'
    compile \'net.kencochrane.raven:raven:6.0.0\'
    compile "org.slf4j:slf4j-api:1.7.13"
    compile \'com.rabbitmq:amqp-client:4.1.0\'
    compile \'org.apache.commons:commons-lang3:3.4\'
    compile \'commons-net:commons-net:3.4\'
    compile \'org.zeromq:jeromq:0.3.5\'
    compile fileTree(dir: \'libs\', include: [\'*.jar\'])
    compile project(\':tc-das\')
    compile project(\':result-compare\')
}

jar {
    manifest {
        attributes(
                "Manifest-Version": 1.0,
                "Main-Class": "com.testbird.rio.Main")
    }
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    into(\'assets\') {
        from \'assets\'
    }
}

以上是关于Gradle - 将依赖和资源文件打入jar包的主要内容,如果未能解决你的问题,请参考以下文章

Android项目实战(二十四):项目包成jar文件,并且将工程中引用的jar一起打入新的jar文件中

IDEA结合maven将依赖都打入jar包

Maven引入本地依赖Jar到可执行Jar包中

springboot打包后资源文件读取问题

如何从具有 gradle 依赖关系的代码创建一个 Android 库?

maven打包成第三方jar包且把pom依赖包打入进来