簡體   English   中英

永久性地將插件添加到Gradle

[英]Permanently add a plugin to Gradle

我在很多項目中使用第三方Gradle插件,並希望將此插件永久添加到我的gradle安裝中。 目前我需要將插件添加到每個build.gradle中,如下所示:

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

有沒有辦法將此插件添加到我的Gradle安裝中,以便我不需要將它包含在每個構建文件中?

我確實意識到它可能不是最佳實踐,並且可能導致無法重復的構建。

這是一個黑客而不是解決方案

這是一個更新版本,它也可以應用插件和添加maven存儲庫。 Testet與gradle 2.10。

將此插件添加到.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])
                    }
            }
        }
    }
}

http://ensime.github.io//build_tools/gradle/我找到了這個替代解決方案(這是針對ENSIME插件):

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

它適用於Gradle 2.12。 另一個答案對我也有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM