加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

groovy – Gradle:强制自定义任务总是运行(无缓存)

发布时间:2020-12-14 16:37:37 所属栏目:大数据 来源:网络整理
导读:我写了一个自定义的Gradle任务来处理文件系统上的一些依赖关系解析,其中路径是可配置的。我想要这种类型的任务总是运行。似乎虽然他们只运行一次,我猜,因为输入似乎没有改变。 我知道使用配置{resolutionStrategy.cacheChangingModulesFor 0,’seconds’
我写了一个自定义的Gradle任务来处理文件系统上的一些依赖关系解析,其中路径是可配置的。我想要这种类型的任务总是运行。似乎虽然他们只运行一次,我猜,因为输入似乎没有改变。

我知道使用配置{resolutionStrategy.cacheChangingModulesFor 0,’seconds’}有效地禁用缓存,但我只希望它适用于非常具体的任务。还知道–rerun-tasks命令行提示,也是类似的。既不喜欢最佳解决方案,必须有一种方法可以在自定义任务定义中正确设置。

以下是我目前的实施。我还有一个版本,之前的前3个def String语句是@Input注释的String声明。

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,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)
}

解决方法

您可以通过在任务上设置outputs.upToDateWhen {false}来实现此目的。

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

handleManagementArchive.outputs.upToDateWhen { false }

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

ResolveProjectArchiveDependency() {
    outputs.upToDateWhen { false }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读