Inherit gradle plugin config from "parent" gradle script

Hi,

I am thinking to move lots of common plugin config move into a common gradle script.

My script looks like this
allprojects {

    buildscript {
        repositories {
            maven {
                url 'https://plugins.gradle.org/m2/'
            }

        }

        dependencies {
            classpath 'com.diffplug.gradle.spotless:spotless:1.3.1'
        }
    }


    apply plugin: 'java'
    apply plugin: 'maven'
    apply plugin: 'com.diffplug.gradle.spotless'


    spotless {
        java {
            eclipseFormatFile 'format.xml'  // XML file dumped out by the Eclipse formatter
        }
    }

    spotlessApply.dependsOn processResources

}


group = 'hello'
version = '1.0-SNAPSHOT'

In another script, I am using the apply from to access this common script.

The final build result looks like this
* What went wrong:
A problem occurred evaluating script.
> You can’t change configuration ‘classpath’ because it is already resolved!

Any idea how can I achieve this?

Br,
Tim

You are trying to manipulate the project’s buildscript classpath. But before your project can apply the common script, its buildscript classpath has already been resolved. Gradle has a two-pass compile: First the buildscript block in a script is executed, then everything else. So your apply from: 'common.gradle' comes too late to change the buildscript classpath.

To fix this, move the buildscript block out of the allProjects block. This way, you will be configuring the classpath of the common script itself. Note though that any external classes it resolves won’t be visible to the project buildscripts. This is a limitation of the current plugin mechanism.

Hi,

Really thanks for your answer. I have done as you said, but it give me another error.

Plugin with id ‘com.diffplug.gradle.spotless’ not found. So classpath totally disappear for the sub projects.

Br,

Tim

Sorry I forgot to mention another current limitation: External plugins have to be applied by class name when applying them from a script.

apply plugin: com.diffplug.gradle.spotless.SpotlessPlugin