Gradle:使用包含的构建版本中的Zip工件

nhaq1z21  于 2023-01-31  发布在  其他
关注(0)|答案(1)|浏览(186)

我有一个生成zip文件的项目和另一个消耗后者的项目。它一定是以某种类似的方式工作的,但是,我还不能做到这一点。我也不知道依赖关系是如何通过包含的构建连接在一起的。
我是这么试的:

  • 压缩生产项目/设置。gradle:*
rootProject.name = 'zip-producing-project'
  • zip生成项目/构建. gradle:*
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

task createZip(type: Zip) {
    from 'src/main/resources'
    include '*'
    archiveName 'zip-producing-project.zip'
}

artifacts {
    archives file('build/distributions/zip-producing-project.zip') // not sure "archives" is the right configuration
}

tasks.build.dependsOn "createZip"
  • 压缩消耗项目/设置。gradle:*
rootProject.name = 'zip-consuming-project'
includeBuild '../zip-producing-project'
  • zip使用项目/构建。gradle:*
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

dependencies {
    archives 'org.example:zip-producing-project:1.0-SNAPSHOT@zip' // is that correct?
}

task unzip(type: Copy) {

    configurations.archives.resolve().forEach {
        if (it.name.endsWith(".zip")) {
            from zipTree(it)
        }
    }

    into "${project.buildDir}"
}

调用gradle clean build得到:

FAILURE: Build failed with an exception.

* Where:
Build file 'zip-consuming-project\build.gradle' line: 14

* What went wrong:
A problem occurred evaluating root project 'zip-consuming-project'.
> Could not resolve all files for configuration ':archives'.
   > Could not find zip-producing-project.zip (project :zip-producing-project)

我如何使它工作,即使zip-consuming-project找到工件zip-producing-project.zip

vkc1a9a2

vkc1a9a21#

  • 我找到了一个解决办法,但我不满意 *

包含的内部版本(发布者):

artifacts {
    add("archives", tasks.register<Zip>("publishSomething") {
        archiveClassifier.set("something")
        from(...)
    })
}

包括构建(消费者)

val extractSomething = tasks.register<Copy>("extractSomething") {
    dependsOn(gradle.includedBuild("included-name").task(":publishSomething"))
    // This is the hacky part, ideally this would be simply `from(gradle.iB().t())`
    from(zipTree(rootProject.file("some/included-name/build/distributions/included-name-something.zip")))
    into(layout.buildDirectory.dir("extracted-something"))
}
tasks.named("compileKotlin").configure { dependsOn(extractSomething) }

相关问题