22
我寫了一個自定義的Gradle任務來處理路徑可配置的文件系統上的一些依賴關係解析。我想要這種類型的任務總是運行。看起來雖然他們只運行一次,但我猜測是因爲輸入似乎沒有改變。Gradle:強制自定義任務始終運行(無緩存)
我知道使用configurations { 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, 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)
}
謝謝。我可以驗證構造函數的版本。 – Rich 2013-04-22 19:36:40