Inherit / inject buildscript dependencies into custom script within subproject


(rainer.frey) #1

I want to use a task from a binary plugin in a custom script within a subproject. This looks like that:

build.gradle

buildscript {
 repositories {
  maven {
                        // from gradle.properties
   url nexusDependencies
  }
        }
 dependencies {
  classpath "com.inxmail:gradle-nsis-plugin:0.0.1-SNAPSHOT"
 }
}

client/build.gradle

// provides task class NsisTask
apply plugin: "nsis"
// ...
apply from: "gradle/distribution.gradle"

client/gradle/distribution.gradle

import com.inxmail.gradle.plugins.*
  // script block provided by our custom nsis plugin, does evaluate
  nsis {
 nsisHome = "/usr/share/nsis"
 verbosity = 1
 define "version", project.majorVersion
 define "product_version", project.version
}
  task windowsExe(type: NsisTask) {
 inputs.dir prepareNsis.destinationDir
 dependsOn prepareNsis
 outputFile = file({"$buildDir/distributions/${distBaseName}-${version}.exe"})
 script = file({"$prepareNsis.destinationDir/inst/install.nsi"})
}

This produces following error:

$ gradle client:clean client:createInstallers client:windowsExe
  FAILURE: Build failed with an exception.
  * Where:
Script '/Users/rfy/Developer/inxmail/git/xpro/client/gradle/distribution.gradle' line: 149
  * What went wrong:
A problem occurred evaluating script.
> Could not find property 'NsisTask' on project ':client'.

If I keep the task of type NsisTask in client/build.gradle, it works. If I copy the buildscript block into client/gradle/distribution.gradle, it works as well.

So it seems that: * buildscript settings from root build script are inherited by a subproject’s main build script * but these settings are not inherited in a custom script in that subproject

Is this by design? Any plans to change this?

And is there anything I can do differently to achieve: * keeping the relevant task in a custom script (for modularity’s sake) * and not duplicating the repo/dependency settings of the buildsrcipt block?

Thanks Rainer


(rainer.frey) #2

As an aside: User guide chapter 52 says: > For multi-project builds, the dependencies declared in the a project’s build script, are available to the build scripts of all sub-projects.

IMO the wording “the build scripts” in plural is a bit ambiguous here: is it the single main build script, of all subprojects, or all scripts that comprise the build of any subproject.


(rainer.frey) #3

Thanks, Peter, for another very quick and to-the-point answer. I’ll do that. Support here on this forum is awesome.


(rainer.frey) #4

There is a problem: apparently gradle.properties is not evaluated from gradle/buildscript.gradle:

gradle dependencies
  FAILURE: Build failed with an exception.
  * Where:
Script '/Users/rfy/Developer/inxmail/git/xpro/gradle/buildscript.gradle' line: 3
  * What went wrong:
A problem occurred evaluating script.
> Could not find property 'nexusDependencies' on org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepository_Decorated@7380e224.

Anything I need/can do?


(rainer.frey) #5

actually using a gradle script that is referenced from buildsrc eliminated the need to keep repository config in gradle.properties. Therefore I don’t need to have this resolved.


(thomas.wright) #6

I too am suffering from the second problem reported above, with the buildscript.gradle file being unable to access properties being defined in gradle.properties; unfortunately, as I am retro-fitting this to an existing build system, I cannot easily work around it with the suggested idea. Are there any other options?


(Peter Niederwieser) #7

Please create a new topic and provide all relevant information.


(thomas.wright) #8

Thank you, moved to here.


(Peter Niederwieser) #9

Script plugins aren’t currently affected by the buildscript section of the build scripts they get applied to. (Actually, it’s questionable whether subproject build scripts should be affected by the buildscript sections of parent build scripts, even though that’s the way it currently is.) What you can do is to move the reusable parts of the buildscript section into its own script plugin:

gradle/buildscript.gradle:

repositories {
    maven {
        // from gradle.properties
        url nexusDependencies
    }
}
dependencies {
    classpath "com.inxmail:gradle-nsis-plugin:0.0.1-SNAPSHOT"
}

You can then include this code in any buildscript section that requires it: client/gradle/distribution.gradle:

buildscript {
  apply from: "$rootProject.projectDir/gradle/buildscript.gradle", to: buildscript
}

This might get revised at some point, but it’s too early to tell.


Gradle.properties are not available when applying external script in buildscript
Multiproject gradle seems to need plugin repository declared in each subproject