Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permanently add a plugin to Gradle

Tags:

java

gradle

I use a third-party Gradle plugin in a lot of projects and would like to add this plugin permanently to my gradle installation. Currently I need to add the plugin to each build.gradle like so:

buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath "com.github.dcendents:android-maven-plugin:1.2"
  }
}

Is there a way to add this plugin to my Gradle installation so that I don't need to include it in every build file?

I do realise it might not be the best practice and can result in unreproducible builds.

like image 724
introex Avatar asked Feb 18 '15 11:02

introex


People also ask

How do I create a Gradle plugin?

To create a Gradle plugin, you need to write a class that implements the Plugin interface. When the plugin is applied to a project, Gradle creates an instance of the plugin class and calls the instance's Plugin. apply() method.

What is plugin base in Gradle?

The Base Plugin adds the base extension to the project. This allows to configure the following properties inside a dedicated DSL block. Example 2. Using the base extension. build.gradle.

When Gradle plugins are applied to a project?

Applying a plugin to a project allows the plugin to extend the project's capabilities. It can do things such as: Extend the Gradle model (e.g. add new DSL elements that can be configured) Configure the project according to conventions (e.g. add new tasks or configure sensible defaults)


1 Answers

This is a hack and not a solution

Here is now an updated version which is also able to apply plugins and add maven repositories. Testet with gradle 2.10.

Add this Plugin to your .gradle/init.gradle:

apply plugin:AddDepPlugin

class AddDepPlugin  implements Plugin<Gradle> {
    def addDeps = [
        "org.ensime.gradle": "gradle.plugin.net.coacoas.gradle:ensime-gradle:0.2.2",
        "com.github.dcendents.android-maven": "com.github.dcendents:android-maven-plugin:1.2"] 
    def addRepos = ["https://plugins.gradle.org/m2/"]
    void apply(Gradle gradle) {
        def add = 0
        gradle.allprojects { project ->
            plugins.whenPluginAdded { t ->
                if (++add == 1) {
                    project.getBuildScriptSource()
                    def bs = project.getBuildscript()
                    bs.getDependencies()
                    def repo = bs.getRepositories()
                    def ccf = bs.class.getDeclaredField("classpathConfiguration")
                    ccf.setAccessible(true)
                    def cc = ccf.get(bs)
                    addDeps.each { k,v-> cc.dependencies.add(project.dependencies.create(v))}
                    addRepos.each { k-> repo.maven { -> setUrl(k) } }
                }
                if (add == 8)
                    addDeps.each { k,v ->
                        if (!k.startsWith("x")) project.apply([plugin: k])
                    }
            }
        }
    }
}
like image 103
bebbo Avatar answered Oct 21 '22 00:10

bebbo