37

我编写了一个自定义 Gradle 任务来处理路径可配置的文件系统上的一些依赖关系解析。我希望这种类型的任务始终运行。似乎虽然它们只运行一次,但我猜是因为输入似乎永远不会改变。

我知道使用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)
}
4

1 回答 1

74

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

这可以在您的build.gradle文件中执行:

handleManagementArchive.outputs.upToDateWhen { false }

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

ResolveProjectArchiveDependency() {
    outputs.upToDateWhen { false }
}
于 2013-04-22T19:11:47.530 回答