首页 > 解决方案 > Exec 任务的动态 gradle 命令失败

问题描述

我正在尝试将 Lambda zip 文件发布到 s3- s3://my-aws-lambda/<projectName>/[release|SNAPSHOT]/。下面定义的任务publishToS3失败并显示消息

Caused by: java.io.FileNotFoundException: /Users/me/my-lambda/build/distributions

当我跑步时

./gradlew clean build -x test -x release

感谢任何帮助。谢谢。

task buildZip(type: Zip) {
    from compileJava
    from processResources
    into('lib') {
        from configurations.runtimeClasspath
    }
}

task publishToS3(type: Exec, dependsOn: buildZip) {
    onlyIf { file("${project.buildDir}/distributions").exist() }

    def artifacts = new FileNameByRegexFinder().getFileNames("${project.buildDir}/distributions", /.*\.zip/)
    assert artifacts.size() == 1

    def isSnapShot = artifacts[0].endsWith('-SNAPSHOT.zip')

    def releaseCmd = ("aws s3 cp " +
            "${artifacts[0]} " +
            "s3://my-aws-lambdas/${project.name}/${isSnapShot ? 'SNAPSHOT' : 'release'}/ ").trim().tokenize(' ') as List

    workingDir "${project.buildDir}/distributions"

    commandLine releaseCmd
}

build.dependsOn buildZip

标签: javagradleaws-lambda

解决方案


如果您在 UNIX 环境中,您可以使用该find命令来搜索文件并使用它的输出。

task scanFiles() {
    def a= "find ${project.buildDir} -name *.zip".execute().text
    String[] files=a.split('\n')
    if(files.length == 1){
        println("We'v got a file :"+a)
    }
    else if(files.length ==0){
        println("We've not no files ")
    }
    else{
        println("We've got multiple files\n"+a)
    }
}


推荐阅读