S-K'
S-K'

Reputation: 3209

How can a custom gradle task be ran from the body of a build

I have the following custom plugin:

class GenPlugin implements Plugin<Project> {
   void apply(Project project) {
      project.task("gen", type:GenTask)
   }
}

That adds the following task to the project:

class GenTask extends DefaultTask {

   @TaskAction
   def gen() {
      println "hello"
   }
}

The groovy code is packaged and deployed to a local maven repository, I then reference the dependency in a build with:

apply plugin: 'gen'

I can run the task successfully by using gradle gen, how can I run this task in my build.gradle without specifying it as an argument?

Upvotes: 1

Views: 126

Answers (1)

Opal
Opal

Reputation: 84756

You can use defaultTasks, have a look at the docs.

Running the task from gradle itself is highly discouraged. You can do it with:

project.tasks.gen.execute()

however I don't recommend you to go that way. Instead you can define a dependency on the task you run from command line. If this task is called e.g. afterGen, then use:

afterGen.dependsOn gen

Upvotes: 2

Related Questions