Reuse of buildscript block

Hi,

we have developed our own gradle plugin to share commonly used logic across multiple project.

In order to use our plugin we use the following buildscript block

buildscript { 	
	apply from: "../conf/repository.gradle"
	repositories {
		mavenLocal()
		maven {
			url nexusGradlePluginsUrl
		}
	}
	dependencies {
		classpath "myGroup:myModule:1.0"
	}
}
plugins {
  id "org.sonarqube" version "2.5"
}
apply plugin: 'org.myGradlePlugin'

As we have to apply this code in all projects, we wanted to put the buildscript block into a gradle-file (lets call it setupPlugins.gradle) and include it with apply from. But this wont work because bevor a plugins block you can’t have a anything else than a buildscript block.
I don’t want to apply sonarcube at all projects which use our plugin.

Any ideas how to reuse buildscript blocks?

Thank you in advance,
stdoubleu

buildscript {
  apply from: 'https://.../setup-plugins.gradle', to: buildscript
}

Make sure the content of that URL never changes (use a version number in the URL) so that your builds remain reproducible.

Thank you for your quick response.
Unfortunately, in repository.gradle we are setting some project extra properties (like nexusGradlePluginsUrl) and using this approach results now in

Could not get unknown property ‘project’ for object of type org.gradle.api.internal.initialization.DefaultScriptHandler.

Although, I understand the problem (kind of :wink: ) I don’t know how to solve it.

buildscript {
  apply from: 'https://.../bootstrap.gradle'
}

bootstrap.gradle

apply from: "../conf/repository.gradle"
project.buildscript {
  //configure default repos
}

It’s a bit hacky, as it only works when invoked inside a buildscript block like I showed above, but it should get the job done.

This did the job. Thank you very much.

How would this look in Kotlin DSL?

rootProject.apply { from(rootProject.file(“make/buildscript.gradle.kts”)) } ???

And how should buildscript.gradle.kts look like?

I can’t get this to work in 5.6.1. I tried exactly as noted above (“apply from: …” within the “buildscript {…}” block with one exception - I did not use the URLs in “apply from: …” but a file path that is in root project’s subfolder (not a subproject).

Tried:

  1. relative and absolute paths, tried rootProject.file(…)
  2. with “project.buildscript {…}” in “bootstratp.gradle” as well as just “buildscript {…}” (no “project.”)
  3. with “to: buildscript” and without it

Nothing worked. Help!

I see 2 main problems with sharing build script:

  • How to distribute the shared script.
  • How to make it configurable and re-usable for all your needs.

I am distributing it in a jar. I release that jar to Maven Central.

I make it re-usable by creating a defaultCOnfig in the script. And a buildConfig in projects using it. Those are merged, in the script to create an effective config.

My projects may have build.gradle looking something like this:

apply plugin: 'java'

buildscript {
 repositories { mavenCentral() mavenLocal() }
 dependencies { classpath 'se.bjurr.gradle:gradle-scripts:2.+' }
}
project.ext.buildConfig = [
  publishing: [
    relocate: [
      "org:org",
      "com:com"
    ]
  ],
  manifest: [
    mainClass: 'se.bjurr.gitchangelog.main.Main'
  ]
]
apply from: project.buildscript.classLoader.getResource('main.gradle').toURI()


dependencies {
 ...
}

I have my code here:

I also blogged about it here:

Actually that code looks a bit anti-pattern.
Whenever you use ext you should think twice about it as usually avoid it in favor of a proper extensions that also gives you type-safety and if added in a plugin that is applied using plugins block in Kotlin DSL also gives you type-safe accessors for convenient usage.

So I’d recommend you make your code a proper plugin (can be a precompiled script plugin) and add a proper extension in it. :slight_smile: