4

我有一个 gradle 多项目,并想创建一个包含我的子项目的所有类和外部依赖项的单个 jar(库)。

我有以下项目结构。每个项目都有自己的第 3 方依赖项。公共依赖项包含在根项目中。两个模块 A 和 B 依赖于内核。

+ root-project (only build.gradle and settings.gradle)
  - core       (src/main/java, src/main/resources, ..)
  - module-A   (src/main/java, src/main/resources, ..)
  - module-B   (src/main/java, src/main/resources, ..)

为了导出单个 jar,我在根项目的 build.gradle 中添加了以下任务:

apply plugin: "java"

subprojects.each { subproject -> evaluationDependsOn(subproject.path)}

task allJar(type: Jar, dependsOn: subprojects.jar) {
    baseName = 'multiproject-test'
    subprojects.each { subproject ->
        from subproject.configurations.archives.allArtifacts.files.collect {
            zipTree(it)
        }
    }
}

artifacts {
    archives allJar
}

这种方法有效,但只收集项目源文件。忽略第 3 方依赖项。所以我尝试了 Shadow Plugin ( http://imperceptiblethoughts.com/shadow/ ),它还应该包含外部依赖项。

不幸的是,该插件根本不收集任何东西。这很可能是由于缺少根项目与其子项目之间的依赖关系。我如何告诉影子插件,它应该收集子项目的来源?或者有没有更好的方法从多个项目中导出单个库?

使用 shadow 插件完成 build.gradle:

/****************************************
 * instructions for all projects
 ****************************************/
allprojects {
    apply plugin: 'idea'
    apply plugin: 'eclipse'

    group = 'com.test.multi-project'
    version = '1.0'
}

/****************************************
 * instructions for each sub project
 ****************************************/
subprojects {
    apply plugin: "java"

    sourceCompatibility = 1.9
    targetCompatibility = 1.9

    repositories {
        mavenCentral()
    }

    dependencies {
        compile "org.slf4j:slf4j-api:1+"
        compile "ch.qos.logback:logback-core:1+"
        compile "ch.qos.logback:logback-classic:1+"

        testCompile "junit:junit:4+"
    }
}

/****************************************
 * Single jar out of all sub projects
 ****************************************/
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.2'
    }
}

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

shadowJar {
    baseName = 'multiproject-test'
} 

子模块包含在根项目的 settings.gradle 中

rootProject.name = 'myproject-root'

// submodules
include ":core"

include ":module-A"
include ":module-B"

谢谢你的帮助!

4

3 回答 3

2

我用这里解释的解决方案解决了我的问题:https ://discuss.gradle.org/t/how-to-get-gradle-install-to-actually-bundle-all-project-subproject-classes-resources-etc/ 12070/4

我的 build.gradle 现在看起来像这样:

/****************************************
 * instructions for all projects
 ****************************************/
allprojects {
    apply plugin: 'idea'
    apply plugin: 'java'

    repositories {
        mavenCentral()
    }

    group = 'com.test.multiproject'
    version = '1.0'

    sourceCompatibility = 1.9
    targetCompatibility = 1.9
}

/****************************************
 * instructions for each sub project
 ****************************************/
subprojects {

    // common dependencies
    dependencies {
        compile "org.slf4j:slf4j-api:1+"
        compile "ch.qos.logback:logback-core:1+"
        compile "ch.qos.logback:logback-classic:1+"

        testCompile "junit:junit:4+"
    }
}

/****************************************
 * Single library jar containing all sub projects and 3rd party dependencies
 ****************************************/
configurations {
    childJars
}

dependencies {
    subprojects.each {
        childJars project(it.path)
    }
}

jar {
    dependsOn configurations.childJars
    from { configurations.childJars.collect { zipTree(it) } }
}
于 2018-02-21T07:59:49.267 回答
2

如何在构建 jar 本身时获取所有运行时库

jar {
    archiveName 'Some.jar'

    manifest {
        attributes 'Implementation-Title': 'Some',
        'Plugin-Class': 'main'
    }
    from {configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it)}}
}
于 2018-11-26T06:09:54.583 回答
1

像这样简单的事情怎么样:

task fatJar(type: Jar) {
    subprojects.each { subproject ->
        from subproject.configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}
于 2018-02-20T16:57:09.667 回答