How can I make a plugin available to every build?

Hi Luke,

I wrote a custom plugin and placed that jar under $gradleHome/lib/plugins. It used to work Under Gradle1.0-milestone-3 but under Gradle1.0-milestone-4 it throws java.lang.ClassNotFoundException because of the following reasons mentioned in the below link -

http://wiki.gradle.org/display/GRADLE/Gradle+1.0-milestone-5+Migration+Guide#Gradle1.0-milestone-5MigrationGuide-Buildscriptclasspath

The issue which I see with this is that every team / developer has to write these lines of code in every build files.

buildscript {
    repositories { ... add some repositories ... }
    dependencies {
        classpath 'group:module:version'
    }
}

If build script path can load custom plugin jar from $gradleHome/lib or $gradleHome/lib/plugins then the developer has to just give the following line -

apply plugin: 'pg'

I can put my custom jar under $gradleHome/lib or $gradleHome/lib/plugins and could create custom Gradle installer for my organization.

Is there any clean solution where each and every team / developer do not have to add that few lines of glue code for loading custom plugin in build scripts?

Regards, Pranav

The solution is to use init scripts to add any boilerplate.

http://gradle.org/current/docs/userguide/init_scripts.html

Use with caution though because it does make your build less portable because you are now relying on something external (just like putting jars in ‘$GRADLE_HOME/lib’).

Hi Luke,

I have added the following in init.gradle

buildscript {
    repositories {
     mavenRepo name: 'xxx', urls: "http://speke.yyy.co.in:8080/artifactory/pg-local"
    }
    dependencies {
        classpath group: 'com.xxx.gradle', name: 'pgplugin', version: '6.0'
        classpath group: 'com.xxx.gradle', name: 'pg', version: '1.0', ext: 'properties'
    }
}

and my build file is

apply plugin: 'pg' //plugin defined in pgplugin.jar
  version = "36.0"
  thirdPartyDependencies {
 compile id: 'log4j'
 compile id: 'springframework.core'
 testCompile id: 'junit'
}

after I run “gradle --stacktrace --init-script init.gradle clean”

I get the following error -

Caused by: org.gradle.api.InvalidUserDataException: You can't change a configuration which is not in unresolved state!
        at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.throwExceptionIfNotInUnresolvedState(DefaultConfiguration.java:408)
        at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.addDependency(DefaultConfiguration.java:293)

So how do I download my custom plugin from binary repository from init script?

Regards, Pranav

You need to wrap what you currently have in your init script in:

projectsLoaded {
    rootProject.allprojects {
        buildscript {
          // your code here
        }
    }
}