Tôi đã viết một nhiệm vụ Gradle tùy chỉnh để xử lý một số độ phân giải phụ thuộc vào hệ thống tệp nơi đường dẫn có thể cấu hình được. Tôi muốn nhiệm vụ của loại này luôn chạy. Dường như họ chỉ chạy một lần, tôi đoán vì các yếu tố đầu vào dường như không thay đổi.Gradle: Buộc Nhiệm vụ tùy chỉnh luôn chạy (không có bộ nhớ cache)
Tôi biết sử dụng configurations { resolutionStrategy.cacheChangingModulesFor 0, 'seconds' }
để vô hiệu hóa hiệu quả bộ nhớ cache, nhưng tôi chỉ muốn áp dụng cho các tác vụ rất cụ thể. Ngoài ra, tôi cũng biết về lời nhắc dòng lệnh --rerun-tasks
, cũng tương tự. Không cảm thấy như giải pháp tốt nhất, phải có một cách để thiết lập này đúng trong định nghĩa nhiệm vụ tùy chỉnh.
Theo dõi là triển khai hiện tại của tôi. Tôi cũng đã có một phiên bản trước khi các câu lệnh 3 def String đầu tiên thay vào đó là các khai báo chuỗi có chú thích đầu vào @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)
}
}
Dưới đây là một ví dụ về việc sử dụng các nhiệm vụ:
task handleManagementArchive (type: com.moremagic.ResolveProjectArchiveDependency) {
archiveFilename = 'management.jar'
relativeArchivePath = 'management/dist/'
dependencyScope = 'compile/archive'
outputFile = file('dependencies/'+dependencyScope+'/'+archiveFilename)
}
Cảm ơn. Tôi có thể xác minh phiên bản của hàm tạo cũng hoạt động. – Rich