Custom "provided" configuration not working with Gradle 2.0 rc2 in multi-project mode

Trying to implement the following with Gradle 2.0:

The idea that the “provided” configuration is used for JAR projects that need access to Servlet API only for compiling purposes but shouldn’t be included as transitive dependency at runtime. To keep build DRY (don’t repeat yourself) was trying to define this configuration for all projects.

I attached a project that works with Gradle 1.2 but fails with Gradle 2.0 rc2.

* Where:
Build file 'C:\Users\Kirk\Workspaces\Gradle2\build.gradle' line: 54
  * What went wrong:
A problem occurred evaluating root project 'Gradle2'.
> You can't change configuration 'provided' because it is already resolved!

It works with 2.0 only if used in certain contexts, e.g. in subprojects closure

//
  dependencies {
//
      provided 'org.jboss.spec:jboss-javaee-web-7.0:1.0.0.Final'
//
  }

But when used outside it fails:

project(':services:webservice') {
    dependencies {
        provided 'org.jboss.spec:jboss-javaee-web-7.0:1.0.0.Final'
    }
}

Perhaps there is a better way now.

Thanks

Where did you attach the project?

https://gist.github.com/eec778ca65292de20f7f.git

Gradle 2 updated to Groovy 2.3, which no longer supports the use of ‘+=’ for adding a single element to a collection. So instead of ‘scopes.PROVIDED.plus += configurations.provided’ it’s now ‘scopes.PROVIDED.plus += [configurations.provided]’. (The other uses of ‘+=’ are OK.)

Is there a better way to attach zip file? I basically just modded the sample Java multiproject. I just uploaded the root build.gradle file to gist.

You can probably figure out the issue just from that file but if not let me know.

Thanks!

task wrapper(type: Wrapper) {
    gradleVersion = '2.0-rc-2'
}
  subprojects {
    apply plugin: 'java'
    apply plugin: 'idea'
      repositories {
        maven {
            url nexusRepoUrl
        }
    }
      configurations {
        provided
    }
      sourceSets {
        main {
            compileClasspath += configurations.provided
            test.compileClasspath += configurations.provided
            test.runtimeClasspath += configurations.provided
        }
    }
      idea {
        module {
            scopes.PROVIDED.plus += configurations.provided
            downloadJavadoc = true
            downloadSources = true
        }
    }
      dependencies {
        testCompile 'junit:junit:4.11'
    }
      version = '1.0'
      jar {
        manifest.attributes provider: 'gradle'
    }
}
  // doesn't like this part
project(':services:webservice') {
    dependencies {
        provided 'org.jboss.spec:jboss-javaee-web-7.0:1.0.0.Final'
    }
}

Actually if you only change the build.gradle file so that dependency is added inside the subprojects closure, i.e.

subprojects {
   dependencies {
       provided 'org.jboss.spec:jboss-javaee-web-7.0:1.0.0.Final'
 }
  }

It adds the dependency ok and my code compiles (webservice references a Servlet API).

Did you miss my earlier answer?

No I saw it. I’m saying I didn’t have to do what you said in order for it to compile successfully.

So in org.gradle.webservice.TestTest I added:

public void method2(ServletRequest servletRequest) {
      }

And this works ok:

subprojects {
    apply plugin: 'java'
    apply plugin: 'idea'
      repositories {
        maven {
            url nexusRepoUrl
        }
    }
      configurations {
        provided
    }
      sourceSets {
        main {
            compileClasspath += configurations.provided
            test.compileClasspath += configurations.provided
            test.runtimeClasspath += configurations.provided
        }
    }
      dependencies {
        provided 'org.jboss.spec:jboss-javaee-web-7.0:1.0.0.Final'
    }
      idea {
        module {
            scopes.PROVIDED.plus += configurations.provided
            downloadJavadoc = true
            downloadSources = true
        }
    }
      dependencies {
        testCompile 'junit:junit:4.11'
    }
      version = '1.0'
      jar {
        manifest.attributes provider: 'gradle'
    }
}

It will cause a problem sooner or later.

However this does NOT: notice I added your change but moved provided dependency out to the project closure instead of subprojects

subprojects {
    apply plugin: 'java'
    apply plugin: 'idea'
      repositories {
        maven {
            url nexusRepoUrl
        }
    }
      configurations {
        provided
    }
      sourceSets {
        main {
            compileClasspath += [configurations.provided]
            test.compileClasspath += [configurations.provided]
            test.runtimeClasspath += [configurations.provided]
        }
    }
  //
  dependencies {
//
      provided 'org.jboss.spec:jboss-javaee-web-7.0:1.0.0.Final'
//
  }
      idea {
        module {
            scopes.PROVIDED.plus += configurations.provided
            downloadJavadoc = true
            downloadSources = true
        }
    }
      dependencies {
        testCompile 'junit:junit:4.11'
    }
      version = '1.0'
      jar {
        manifest.attributes provider: 'gradle'
    }
}
  project(':services:webservice') {
    dependencies {
        provided 'org.jboss.spec:jboss-javaee-web-7.0:1.0.0.Final'
    }
}

Correction: Only ‘scopes.PROVIDED.plus += configurations.provided’ is where the ‘[]’ has to be added. The other uses of ‘+=’ are OK.

Success. Never would have guessed that! Idea configuration wasn’t even important as I’m running only from command-line.

Thanks for your help!