Gradle:强制自定义任务始终运行(无缓存) [英] Gradle: Force Custom Task to always run (no cache)
问题描述
我写了一个定制的Gradle任务来处理路径可配置的文件系统上的一些依赖关系解析。我想要这种类型的任务总是运行。它似乎虽然他们只运行一次,我猜测,因为输入从来没有改变。
我知道使用配置{resolutionStrategy。 cacheChangingModulesFor 0,'seconds'}
有效地禁用缓存,但我只希望它适用于非常特定的任务。还知道 - rerun-tasks
命令行提示符,它也是类似的。既不觉得最好的解决方案,也必须在自定义任务定义中正确设置它。
以下是我目前的实施情况。
类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表示预发布环境的属性集是分开的
newArcFile = project.file(project.ext.separatedDependencyRoot + relativeArchivePath + archiveFilename)
} else {
newArcFile = project.file('../../'+ relative ArchivePath @ archiveFilename)
}
if(!newArcFile.isFile()){
println警告:无法找到+ archiveFilename +的最新副本。
$ b $ if(!arcFile.isFile()){
println错误:未找到+ archiveFilename +的版本
throw new StopExecutionException(archiveFilename +missing )
}
}
if(!arcFile.isFile()){
println archiveFilename +jar不在依赖关系中,从存档中提取
} else {
println archiveFilename +jar in dependencies。检查过时
def oldHash = generateMD5(new File(arcFile.path))
def newHash = generateMD5(new File(newArcFile.path))
如果(newHash.equals(oldHash)){
println哈希匹配的罐子。不需要复制
return
}
}
//复制档案
project.copy {
println复制 + archiveFilename
from newArcFile
intodependencies /+ dependencyScope
}
}
def generateMD5(final file){
MessageDigest摘要= 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)
}
}
下面是一个使用任务的例子:
任务handleManagementArchive(类型: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(){
pre>
outputs.upToDateWhen {false}
}
I wrote up a custom Gradle task to handle some dependency resolution on the file system where the paths are configurable. I want tasks of this type to always run. It seems though they only run once, I'm guessing because the inputs never seem to change.
I am aware of using
configurations { resolutionStrategy.cacheChangingModulesFor 0, 'seconds' }
to effectively disable the cache, but I only want it to apply to very specific tasks. Also am aware of--rerun-tasks
command line prompt, which is also similar. Neither feel like the best solution, there must be a way to set this up properly in the custom task definition.Follows is my current implementation. I also had a version prior where the first 3 def String statements were instead @Input annotated String declarations.
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) } }
Here's an example of usage of the task:
task handleManagementArchive (type: com.moremagic.ResolveProjectArchiveDependency) { archiveFilename = 'management.jar' relativeArchivePath = 'management/dist/' dependencyScope = 'compile/archive' outputFile = file('dependencies/'+dependencyScope+'/'+archiveFilename) }
解决方案You can achieve this by setting
outputs.upToDateWhen { false }
on the task.This can be performed in your
build.gradle
file:handleManagementArchive.outputs.upToDateWhen { false }
It can also be done in the constructor of your custom task.
ResolveProjectArchiveDependency() { outputs.upToDateWhen { false } }
这篇关于Gradle:强制自定义任务始终运行(无缓存)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!