eastwater
eastwater

Reputation: 5588

Gradle how to use plugin in an included common build script

project 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) {
   ...
}

Error

Only Project build scripts can contain plugins {} blocks

If the plugins block moved to project A,

Error

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

How to use an plugin (from public responsitory) in an included build script (called script plugin)?

Upvotes: 2

Views: 2317

Answers (2)

Stefan Feuerhahn
Stefan Feuerhahn

Reputation: 1804

When using Gradle's Kotlin DSL the method explained by Michael does not allow to use the type-safe model accessors provided by the plugin(s). Instead you can put your common build script into the buildSrc directory structure. This allows the use of the plugins{} block and gives you all the advantages of the type-safe Kotlin DSL. https://docs.gradle.org/current/userguide/organizing_gradle_projects.html#sec:build_sources

Upvotes: 0

Michael Easter
Michael Easter

Reputation: 24468

Here is one way to do it. (I used this page as a resource, but not sure if it is still accurate.) With Gradle 4.0:

Given build.gradle:

apply from: 'common.gradle'

here is 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) {
    // ...
}

Upvotes: 4

Related Questions