简体   繁体   中英

Permanently add a plugin to 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.

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])
                    }
            }
        }
    }
}

On http://ensime.github.io//build_tools/gradle/ I found this alternative solution (this is for the ENSIME plugin):

apply plugin: AddEnsimePlugin

class AddEnsimePlugin  implements Plugin<Gradle> {
  def supportedPlugins = [
    'org.gradle.api.plugins.JavaPlugin',
    'org.gradle.api.plugins.ScalaPlugin',
    'jp.leafytree.gradle.AndroidScalaPlugin'
  ]

  void apply(Gradle gradle) {
    def added = false

    gradle.allprojects { project ->
      project.with { 
        if (parent == null) {
          buildscript { 
            repositories {
              jcenter()
              maven {
                name 'JFrog OSS Snapshot Repository'
                url 'http://oss.jfrog.org/oss-snapshot-local'
              }
            }
            dependencies {
              classpath 'net.coacoas.gradle:ensime-gradle:0.2.6'
            }
          }
        }

        plugins.whenPluginAdded { plugin ->
          if (!added && supportedPlugins.contains(plugin.class.name)) { 
            rootProject.apply plugin: 'org.ensime.gradle'
            added = true
          }
        }
      }
    }
  }
}

It works for me with Gradle 2.12. The other answer also works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM