Cannot define inputs/outputs when creating a custom task

Hi, I’m trying to create a custom task to generate files with xdoclet.

My attempt is:

import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
  class XDocletTask extends DefaultTask {
   String sourceDir = "src/main/java"
  String localMergeDir = this.sourceDir + "/../xdoclet"
 String targetDir = this.sourceDir + "/../ejbgen"
    inputs.sourceDir( file(this.localMergeDir) )
   inputs.sourceDir( fileTree(this.sourceDir){ include "**/*Bean.java" } )
   outputs.dir(new File(this.targetDir))
  ... omitted...
  }

But when I try to compile, I get this error:

:compileJava UP-TO-DATE
:compileGroovy
startup failed:
XDocletTask.groovy: 28: unexpected token: inputs @ line 28, column 3.
        inputs.sourceDir( file(this.localMergeDir) )
     ^
  1 error
    FAILURE: Build failed with an exception.
  * What went wrong:
Execution failed for task ':compileGroovy'.
> Compilation failed; see the compiler error output for details.
  * Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
  BUILD FAILED

It’s possible to define inputs and/or outputs for a custom task?

Thanks, Luca

You get a syntax error because your inputs/outputs code is right in the class body, outside a method. Neither Groovy nor Java allows code in this location. You could move the code into a constructor.

The usual way to specify inputs and outputs for task classes is with annotations. Something like:

class XDocletTask extends DefaultTask {
    @InputDirectory
    File sourceDir
    @InputDirectory
    File localMergeDir
    @OutputDirectory
    File targetDir
      XDocletTask {
        sourceDir = project.file("src/main/java")
        localMergeDir = project.file("$sourceDir/../xdoclet")
        targetDir = project.file("$sourceDir/../ejbgen")
    }

Thanks, I came from ant and I often forget that this is executable code. May be I’m too newbie, but I suggest to put this info in the section “create a custom task”, I didn’t spotted in any page of the docs. Thanks again, Luca

I tried to do this in eclipse, but couldn’t get your example to work unless I did:

import java.io.File;
  import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.InputDirectory;
import org.gradle.api.tasks.OutputDirectory;
  class XDocletTask extends DefaultTask {
  @InputDirectory
  File sourceDir
  @InputDirectory
  File localMergeDir
  @OutputDirectory
  File targetDir
      public XDocletTask() {
    sourceDir = project.file("src/main/java")
    localMergeDir = project.file("$sourceDir/../xdoclet")
    targetDir = project.file("$sourceDir/../ejbgen")
  }
  }

For classes declared outside a build script, you will need those imports.

Oh, no, not what I meant - it was the constructor part:

public XDocletTask() {
    sourceDir = project.file("src/main/java")
    localMergeDir = project.file("$sourceDir/../xdoclet")
    targetDir = project.file("$sourceDir/../ejbgen")
  }

Eclipse kept giving me errors without the public and (). Is that some groovy shortcut eclipse doesn’t support I guess?

You cannot leave out the ‘()’ in Groovy. You can leave out the ‘public’.

OK, ty