Reuse of buildscript block


(stdoubleu) #1

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


(Stefan Oehme) #2
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.


(stdoubleu) #3

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.


(Stefan Oehme) #4
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.


(stdoubleu) #5

This did the job. Thank you very much.