Building custom Groovy / Gradle plugin


(Pranav K Varshney) #1

Hi,

I have written a custom plugin that imports org.gradle.api.* For example -

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.tasks.compile.Compile
import com.pg.gradle.tasks.IAJC
  class IAJCPlugin implements Plugin<Project> {
 void apply(Project project) {
  project.getPlugins().apply(JavaPlugin.class)
  project.task([type: IAJC, overwrite: true], "compileJava")
 }
}

I have the following build.gradle to build custom plugin

apply plugin: 'groovy'
apply plugin: 'maven'
  version = 1.0
group = 'com.xxxxx.gradle'
archivesBaseName = 'pgplugin'
  sourceSets {
 main {
   groovy {
    srcDir 'src/main'
   }
  }
 test {
   groovy {
    srcDir 'src/test'
   }
  }
}
   dependencies {
    groovy group: 'org.codehaus.groovy', name: 'groovy', version: '1.8.4', transitive = false
}
  repositories {
 mavenRepo name: 'xxxxx', url: 'http://speke.yyyy.co.in:8080/artifactory/xxxxx'
}
  uploadArchives {
 repositories.mavenDeployer {
  repository(url: 'http://speke.yyyy.co.in:8080/artifactory/pg-local') {
   authentication(userName: 'admin', password: 'password')
  }
 }
}

I get the following error

[ant:groovyc] D:\B2D_PROJECTS-R36\B2D\GradlePGPlugins\src\main\com\pg\gradle\plugins\IAJCPlugin.groovy: 6: unable to resolve class org.gradle.api.plugins.JavaPlugin
  [ant:groovyc]
@ line 6, column 1.
[ant:groovyc]
  import org.gradle.api.plugins.JavaPlugin
[ant:groovyc]
  ^
[ant:groovyc]
[ant:groovyc] D:\B2D_PROJECTS-R36\B2D\GradlePGPlugins\src\main\com\pg\gradle\plugins\IAJCPlugin.groovy: 5: unable to resolve class org.gradle.api.Task
  [ant:groovyc]
@ line 5, column 1.
[ant:groovyc]
  import org.gradle.api.Task

Why is it not able to include gradle-core-1.0-milestone-6.jar in classpath? Do I need to specify it separately?

Regards, Pranav


(Pranav K Varshney) #2

Looked into build.gradle provided under gradle-1.0-milestone-6-src and adding the following fixed the problem

compile gradleApi()

So the dependencies closure looks like -

dependencies {
    compile gradleApi()
    groovy group: 'org.codehaus.groovy', name: 'groovy', version: '1.8.4'
}

Not sure what compile gradleApi() does.

Regards, Pranav


(Peter Niederwieser) #3

compile gradleApi() adds the Gradle API to the compile class path. If you leave that out, the Gradle Jars won’t be passed to the Groovy compiler, and a compile error will occur.