【问题标题】:Gradle how to use plugin in an included common build scriptGradle 如何在包含的通用构建脚本中使用插件
【发布时间】:2017-07-06 18:07:43
【问题描述】:

项目A:

   apply from : 'common.gradle'

common.gradle

plugins {
    id 'com.eriwen.gradle.js' version '2.14.1'
}

apply plugin: 'js'

import com.eriwen.gradle.js.tasks.MinifyJsTask;

task minify(type: MinifyJsTask) {
   ...
}

错误

Only Project build scripts can contain plugins {} blocks

如果插件块移动到项目 A,

错误

unable to resolve class com.eriwen.gradle.js.tasks.MinifyJsTask

如何在包含的构建脚本(称为脚本插件)中使用插件(来自公共责任)?

【问题讨论】:

    标签: gradle plugins


    【解决方案1】:

    这是一种方法。 (我使用 this page 作为资源,但不确定它是否仍然准确。)使用 Gradle 4.0:

    给定build.gradle

    apply from: 'common.gradle'
    

    这里是common.gradle:

    buildscript {
      repositories {
        maven {
          url "https://plugins.gradle.org/m2/"
        }
      }
      dependencies {
        classpath "com.eriwen:gradle-js-plugin:2.14.1"
      }
    }
    
    apply plugin: com.eriwen.gradle.js.JsPlugin 
    
    task minify(type: com.eriwen.gradle.js.tasks.MinifyJsTask) {
        // ...
    }
    

    【讨论】:

    • 您将如何使用此解决方法来应用诸如 applicationjava-library 之类的内置插件之一?
    【解决方案2】:

    当使用 Gradle 的 Kotlin DSL 时,Michael 解释的方法不允许使用插件提供的类型安全模型访问器。相反,您可以将通用构建脚本放入 buildSrc 目录结构中。这允许使用 plugins{} 块,并为您提供类型安全 Kotlin DSL 的所有优点。 https://docs.gradle.org/current/userguide/organizing_gradle_projects.html#sec:build_sources

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 2023-01-29
      • 1970-01-01
      • 2014-12-09
      相关资源
      最近更新 更多