Multithreading 多线程ant signjar偶尔会导致错误
我的Gradle构建通过Multithreading 多线程ant signjar偶尔会导致错误,multithreading,ant,gradle,Multithreading,Ant,Gradle,我的Gradle构建通过ant.signjar任务标记了近200个JAR。为了加快速度,我决定尝试使用来自和的信息对其进行多线程处理。仅供参考:在我的Core Duo笔记本电脑上,这个时间从25分钟减少到7分钟(快4倍!) 然而,有时它运行成功,有时它失败,我不知道为什么。这可能和多线程有关,但我无法解释为什么它不是每次都发生 我的代码现在看起来像这样 import groovy.io.FileType import static groovyx.gpars.GParsPool.withPool
ant.signjar
任务标记了近200个JAR。为了加快速度,我决定尝试使用来自和的信息对其进行多线程处理。仅供参考:在我的Core Duo笔记本电脑上,这个时间从25分钟减少到7分钟(快4倍!)
然而,有时它运行成功,有时它失败,我不知道为什么。这可能和多线程有关,但我无法解释为什么它不是每次都发生
我的代码现在看起来像这样
import groovy.io.FileType
import static groovyx.gpars.GParsPool.withPool
//This is so that we can use the Groovy parallel library in the build script
buildscript {
repositories {
mavenLocal()
mavenCentral()
//Change to this if you have your own repo
maven {
url "http://localhost:8081/nexus/content/groups/public/"
}
}
dependencies {
classpath group: 'org.codehaus.gpars', name: 'gpars', version: '1.2.1'
}
}
task signWebstartJars() << {
File unsignedFolder = new File('build/maven')
File signedFolder = new File('signed-libs')
signedFolder.mkdirs()
def list = []
unsignedFolder.eachFileRecurse(FileType.FILES) { file ->
list << file
}
list.sort()
//Create a closure that allows us to run the actual ant task multi-threaded later
def antClosure = {
println "Updating manifest: " + it
//Manifest needs extra attributes added for security
ant.jar(destfile: it, update: true) {
delegate.manifest {
attribute(name: 'permissions', value: 'all-permissions')
attribute(name: 'codebase', value: '*')
}
}
println "Signing the jar: " + it
ant.signjar(
destDir: signedFolder,
alias: alias,
sigfile: sigfile,
jar: it,
keystore: keystore,
storepass: storepass,
storetype: storetype,
preservelastmodified: "true",
verbose: "true"
)
}
//Use twice as many CPUs for no. of threads
result = withPool(Runtime.runtime.availableProcessors() * 2) {
list.collectParallel antClosure
}
}
它不是线程安全的。在闭包中创建新的AntBuilder可以修复它。我已经从Gradle论坛粘贴了下面的片段,因为它比我在原始帖子中的示例要短
import groovyx.gpars.GParsPool
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'org.codehaus.gpars', name: 'gpars', version: '1.1.0'
}
}
task signMyJars {
doLast {
List filesToSign = [
file('file1.jar'),file('file2.jar'),file('file3.jar')
]
GParsPool.withPool {
filesToSign.eachParallel { f ->
def antLocal = project.createAntBuilder()
antLocal.signjar(
alias: "my key alias",
jar: f,
keystore: "my_key_store",
storepass: "key_store_pass",
storetype: "pkcs12",
)
}
}
}
}
这些罐子也是用Gradle制造的吗?有什么理由不能将它们作为构建过程的一部分进行签名,而不是像现在这样批量进行签名?这些是第三方JAR,我需要使用自己的证书进行签名,以便使用Java Web Start启动我的应用程序。
import groovyx.gpars.GParsPool
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'org.codehaus.gpars', name: 'gpars', version: '1.1.0'
}
}
task signMyJars {
doLast {
List filesToSign = [
file('file1.jar'),file('file2.jar'),file('file3.jar')
]
GParsPool.withPool {
filesToSign.eachParallel { f ->
def antLocal = project.createAntBuilder()
antLocal.signjar(
alias: "my key alias",
jar: f,
keystore: "my_key_store",
storepass: "key_store_pass",
storetype: "pkcs12",
)
}
}
}
}