摇篮:强制自定义任务始终运行(无缓存)(Gradle: Force Custom Task to a

2019-08-31 19:58发布

我写了一个自定义的摇篮任务处理文件系统所在的路径是可配置一些依赖解析。 我想这种类型的任务始终运行。 看来虽然他们只运行一次,我猜是因为投入似乎从来没有改变。

我知道使用的configurations { resolutionStrategy.cacheChangingModulesFor 0, 'seconds' }有效地禁用缓存,但我只希望它应用到非常具体的任务。 另外我知道--rerun-tasks的命令行提示符,这还差不多。 像最好的解决方案无论是手感,必须有自定义任务定义正确设置它的方式。

以下是我目前的执行情况。 我也有一个版本,其中前3个DEF STRING发言,而不是@input注释字符串声明之前。

class ResolveProjectArchiveDependency extends DefaultTask {
    def String archiveFilename = ""
    def String relativeArchivePath = ""
    def String dependencyScope = ""

    @OutputFile
    File outputFile

    @TaskAction
    void resolveArtifact() {
        def arcFile = project.file("dependencies/"+dependencyScope+"/"+archiveFilename)
        def newArcFile = ""

        if(project.hasProperty('environmentSeparated') && project.hasProperty('separatedDependencyRoot')){
            println "Properties set denoting the prerelease environment is separated"
            newArcFile = project.file(project.ext.separatedDependencyRoot+relativeArchivePath+archiveFilename)
        }   else {
            newArcFile = project.file('../../'+relativeArchivePath+archiveFilename)
        }

        if(!newArcFile.isFile()){
            println "Warn: Could not find the latest copy of " + archiveFilename + ".."

            if(!arcFile.isFile()) {
                println "Error: No version of " + archiveFilename + " can be found"
                throw new StopExecutionException(archiveFilename +" missing")
            }
        }

        if(!arcFile.isFile()) {
            println archiveFilename + " jar not in dependencies, pulling from archives"
        } else {
            println archiveFilename + " jar in dependencies. Checking for staleness"

            def oldHash = generateMD5(new File(arcFile.path))
            def newHash = generateMD5(new File(newArcFile.path))

            if(newHash.equals(oldHash)) {
                println "Hashes for the jars match. Not pulling in a copy"
                return
            }
        }

        //Copy the archive
        project.copy {
            println "Copying " + archiveFilename
            from newArcFile
            into "dependencies/"+dependencyScope
        }
    }

    def generateMD5(final file) {
       MessageDigest digest = MessageDigest.getInstance("MD5")
       file.withInputStream(){is->
       byte[] buffer = new byte[8192]
       int read = 0
          while( (read = is.read(buffer)) > 0) {
                 digest.update(buffer, 0, read);
             }
         }
       byte[] md5sum = digest.digest()
       BigInteger bigInt = new BigInteger(1, md5sum)
       return bigInt.toString(16)
    }
}

这里的任务使用的例子:

task handleManagementArchive (type: com.moremagic.ResolveProjectArchiveDependency) {
    archiveFilename = 'management.jar'
    relativeArchivePath = 'management/dist/'
    dependencyScope = 'compile/archive'
    outputFile = file('dependencies/'+dependencyScope+'/'+archiveFilename)
}

Answer 1:

您可以通过设置实现这一outputs.upToDateWhen { false }的任务。

你可以在你进行build.gradle文件:

handleManagementArchive.outputs.upToDateWhen { false }

它也可以在您的自定义任务的构造函数中完成。

ResolveProjectArchiveDependency() {
    outputs.upToDateWhen { false }
}


文章来源: Gradle: Force Custom Task to always run (no cache)