Ant threadsafe and how to get new ant instance


(ylemoigne) #1

Hi,

I’m using gradle’s ant support to sign a lot of jar (for a Java Web Start). This is a quite slow operation, so I tried using Gpars to do it :

def libFiles = ParallelEnhancer.enhanceInstance(files { file("${buildDir}/uiClassicLibs").listFiles() })
libFiles.eachParallel {
 ant.signjar(
  destDir: destDir,
  alias: 'foo',
  jar: it,
  keystore: rootProject.file('signing/foo.keystore'),
  storepass: 'bar',
  preservelastmodified: 'true')
}

Now, my problem is that ant return the same AntBuilder instance and it doesn’t seem threadsafe (random fail).

Here is the question : How can I get new correctly configured AntBuilder instance ? Or how can I do jarSigning in parrallel ?


(Peter Niederwieser) #2

You could try to manage your own ‘groovy.util.AntBuilder’ instances. Alternatively, you could run the build with ‘–parallel’. This won’t currently sign a project’s artifacts in parallel, but it will build multiple projects in parallel (in a multi-project build). In this case, you could also use Gradle’s ‘signing’ plugin instead of the Ant task.


(Peter Niederwieser) #3

Another way to create a new ‘AntBuilder’ is ‘project.createAntBuilder()’.


(ylemoigne) #4

Hi, thanks for the answer.

I already use --parallel, but for the Java Web Start jar signing, all happen in one project (the war project). The keyring I have was created with java keytool, so the signing plugin won’t read it.

Creating new AntBuilder seems my better option :wink:

Thanks again


(Jason Zwolak) #5

I got this working in my build script. I extracted and simplified the code to share with other developers. The code is below and hasn’t been tested, but it works in its original form in my build script. I hope it works for others, too.

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

(Tobias Schulte) #6

If AntBuilder is not thread safe, would it be a good idea to automatically let AbstractProject.getAnt() return a new AntBuilder per thread?


(Tobias Schulte) #7

OK, never mind, is that the reason, parallel build only affects multi-module-builds, because Project is not meant to be used from different threads? In that case it seems completely ok for getAnt() to return a non-thread-safe object.