Cannot change dependencies of configuration after it has been included in dependency resolution

I’m new in gradle and I’m trying to migrate the Intermine mulitproject from ant to gradle.
I started creating the intermine gradle multiproject containing 3 subprojects:

  1. intermine-model-main

  2. intermine-objectstore-main

  3. testmodel

The intermine-objectstore-main project uses the anltr plugin.
The testmodel project imports the ant tasks.

When testmodel is commented out the build task is executed successfully, but when I un-comment out the testmodel project I have the following error:
:intermine-objectstore-main:generateGrammarSource FAILED. Cannot change dependencies of configuration ':intermine-objectstore-main:antlr' after it has been included in dependency resolution

This is the build.gradle of the main intermine project.

task wrapper(type: Wrapper) {
gradleVersion = ‘3.0’
}

subprojects {
apply plugin: “java”
sourceCompatibility = 1.6
targetCompatibility = 1.6
repositories {
mavenCentral()
}
}

project(‘:intermine-model-main’) {
dependencies {
compile group: ‘log4j’, name: ‘log4j’, version: ‘1.2.17’
compile group: ‘commons-lang’, name: ‘commons-lang’, version: ‘2.6’
compile group: ‘commons-collections’, name: ‘commons-collections’, version: ‘3.2’
compile group: ‘cglib’, name: ‘cglib’, version: ‘3.1’
}
}

project(‘:intermine-objectstore-main’) {
dependencies {
compile project(‘:intermine-model-main’)
compile group: ‘org.apache.ant’, name: ‘ant’, version: ‘1.9.7’
compile group: ‘org.postgresql’, name: ‘postgresql’, version: ‘9.4.1212’
compile group: ‘torque’, name: ‘torque-gen’, version: ‘3.3’
compile group: ‘commons-io’, name: ‘commons-io’, version: ‘2.5’
compile group: ‘commons-beanutils’, name: ‘commons-beanutils’, version: ‘1.9.2’
compile group: ‘com.zaxxer’, name: ‘HikariCP’, version: ‘2.2.5’
compile group: ‘net.iharder’, name: ‘base64’, version: ‘2.3.8’
compile group: ‘au.com.bytecode’, name: ‘opencsv’, version: ‘2.4’
compile group: ‘org.ow2.asm’, name: ‘asm’, version: ‘4.2’
}
}

project(‘:testmodel’) {
dependencies {
compile project(‘:intermine-objectstore-main’)
compile fileTree(dir: “$buildDir/distributions”, include: ‘*.zip’)
compile group: ‘ant-contrib’, name: ‘ant-contrib’, version: ‘1.0b3’
}
}

The intermine-objectstore-main project uses the anltr plugin. This the build.gradle:

apply plugin: “antlr”

sourceSets {
main {
java {
srcDirs = [‘src’, ‘build/generated-src/antlr/main’]
}
resources {
srcDirs = [‘resources’]
}
antlr {
srcDirs = [‘antlr’]
}
}
}

The testmodel subproject imports an ant build (successfully) and tries to set up the ant classpath (which causes the error mentioned before). This the build.gradle

ClassLoader antClassLoader = org.apache.tools.ant.Project.class.classLoader
configurations.compile.each { File f →
antClassLoader.addURL(f.toURI().toURL())
}

ant.importBuild(‘build.xml’) {
antTargetName → ‘ant-’ + antTargetName
}

sourceSets {
main {
java {
srcDirs = [‘build/gen/src’]
}
resources {
srcDirs = [‘resources’]
}
}
}

task showMeCache {
doLast {
println configurations.compile.asPath
}
}

ant.properties[‘no.dep’] = ‘true’
ant.properties[‘regenerate.java’] = ‘true’
ant.properties[‘model.name’] = ‘testmodel’
ant.properties[‘gen.src.dir’] = ‘build/gen/src’

tasks.compileJava.dependsOn ‘ant-generate’

If I comment out the first 3 lines (where I set the ant class loader), I don’t have any dependency error but at the poin gradle can not find (and execute) the macrodef defined in ant build.xml.
Any suggestion would be very very appreciated.
I’m using gradle 3.3 and Java 1.8
Thanks in advance !!!

Never mind, I figured out the problem.
After more reading and studying :slight_smile:, in particular the build lifecycle I moved the code setting the ant classloader from the configuration phase to the execution, wrapping the code in a task

task setAntClassLoader {
doLast {
ClassLoader antClassLoader = org.apache.tools.ant.Project.class.classLoader
configurations.compile.each { File f →
antClassLoader.addURL(f.toURI().toURL())
}
}
}
Hope this can help someone else.