Programmatically replacing task


(Pranav K Varshney) #1

I have created IAJC task and replacing compileJava with it. My code is –

IAJCPlugin.groovy (custom plug-in)

class IAJCPlugin implements Plugin<Project> {
 void apply(Project project) {
  project.getPlugins().apply(JavaPlugin.class)
    task compileJava(type: IAJC)
 }
}

IAJC.groovy

class IAJC extends DefaultTask {
 IAJC () {
  project.configurations { iajcconf }
  project.dependencies {
iajcconf 'org.aspectj:aspectjtools:1.6.2' }
    doFirst {
   ant.taskdef(resource: 'org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties', classpath: project.configurations.iajcconf.asPath)
   project.sourceSets.main.java.srcDirs.each{ dir ->
    if(dir.exists()) {
     ant.iajc( destdir: project.sourceSets.main.output.classesDir,
       source: "1.6",
       srcdir : dir,
       classpath: project.configurations.compile.asPath)
    }
   }
  }
 }
}

build.gradle

apply plugin: 'iajc' //custom plugin IAJCPlugin

This does not work and I am getting the following error –

Caused by: groovy.lang.MissingMethodException: No signature of method: com.pg.gradle.plugins.IAJCPlugin.compileJava() is applicable for argument types: (java.util.LinkedHashMap) values: [[type:class com.pg.gradle.tasks.IAJC]]
        at com.pg.gradle.plugins.IAJCPlugin.apply(IAJCPlugin.groovy:17)

(Luke Daley) #2

You can’t use the ‘task compileJava(type: IAJC)’ syntax in a plugin. It’s only valid in a build script. You’ll need to use:

project.task(“compileJava”, type: IAJC)

http://gradle.org/current/docs/dsl/org.gradle.api.Project.html#org.gradle.api.Project:task(java.util.Map, java.lang.String)


(Pranav K Varshney) #3

Thanks Luke

I have used the following

project.task([type: IAJC, overwrite: true], "compileJava")