Usage of 'apply from' in buildscript scope

Some configurations like module dependencies and repositories I’ve separated into dedicated gradle files. Now I try to get a module into the buildscript path.

In order to do so I’ve tried to apply my configurations for modules and repositories in the buildscript scope. This works fine for modules but not for repositories. The repositories.gradle config is applied somehow to the Project and

not as expected by me to the ScriptHandler repositories property. I’ve verified this by printing out the repositories config with their hashCodes.

Is there something wrong with my code? Thanks, David

repositories.gradle:

repositories {
  maven{ url 'http://10.150.225.29:8080/nexus/content/groups/snapshot'}
}

modules.gradle:

modules = [
  anyModule:
[group: 'anyGroup', name: 'anyName', version: '1.0']
]

build.gradle:

buildscript
 {
  apply from: 'repositories.gradle' //this applies on the Project's rather than on the ScriptHandler's repositories property
  apply from: 'modules.gradle' //this seams to work ok at least modules property is available in the code below
    //this works fine but I need to repeat the repository config
  //repositories {
  //
maven{ url 'http://10.150.225.29:8080/nexus/content/groups/snapshot'}
  //}
      dependencies {
    classpath(modules.anyModule)
  }
}

This won’t work due to how the buildscript closure is handled.

What happens is that when the script is compiled, it is effectively split into two scripts: one with the ‘buildscript’ closure and one with everything outside. The ‘buildscript’ script is executed first and it effectively defines the classpath for the execution of second script.

The ‘apply’ methods there are applying to the ‘Project’, not the ‘buildscript’ (it has no apply method).

Try changing your ‘repositories.gradle’ to:

def ourRepos = {
  maven { url 'http://10.150.225.29:8080/nexus/content/groups/snapshot' }
}
  project.buildscript.repositories(ourRepos)
repositories(ourRepos)

I think that will work.

Thanks this works for now.

But it appears to me somehow like a workaround. The apply must be done in the buildscript part otherwise it wouldn’t work. Same is true for the modules.gradle file.

buildscript {
  apply from: 'repositories.gradle'
  apply from: 'modules.gradle'
      dependencies {
    classpath(modules.anyModule)
  }
}

Yeah, it must be in the ‘builscript’ block otherwise it’s too late to effect the buildscript block because of the two pass approach outlined above.

solved it. thanks david

repositories.gradle:

repositories {
  maven{ url 'http://10.150.225.29:8080/nexus/content/groups/snapshot'}
}

modules.gradle:

ext.modules = [
  anyModule:
[group: 'anyGroup', name: 'anyName', version: '1.0']
]

build.gradle:

buildscript {scriptHandler->
  apply from: 'repositories.gradle', to: scriptHandler
  apply from: 'modules.gradle'
      dependencies {
    classpath(ext.modules.samsPlugins)
  }
}