Gradle:创建具有所有依赖项的单个JAR

时间:2018-01-21 08:07:51

标签: android gradle jar

我有project1和project2。

project1依赖于project2:

dependencies {
    api project(':project2')
}

有没有办法获得一个包含来自project1和project2的类的jar文件?

2 个答案:

答案 0 :(得分:1)

尝试此任务:

task jar(type: Jar) {
 baseName="project"
 from 'src/main/java'
}
task create(type: Jar) {
  baseName = "project1"
  from {
    configurations.compile.collect {
        it.isDirectory() ? it : zipTree(it)
    }
  }
  with jar
}

configurations {
  jarConfig
}

artifacts {
  jarConfig jar
}

答案 1 :(得分:0)

当然可以:

// Make your project 2 api configuration
configurations {
    api
    compile.extendsFrom(api)

}

dependencies {
    api project(':project2')
}

task packAllToJar (type : Jar){
    // pack yours main module
    from sourceSets.main.output

    // pack your other "api" module dependencies
    from {
        configurations.api.collect { it.isDirectory() ? it : zipTree(it) }
    }

    // pack all dependencies in the root project
    from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }

    // Set name and version
    version = "1.0"
    baseName = "customJarName"
}