Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a single gradle task from command line

Tags:

In my project I have several tasks in my build.gradle. I want those tasks to be independent while running. ie I need to run a single task from command line. But the command "gradle taskA" will run both taskA and taskB which I do not want. How to prevent a task being running?.

Here's a sample of what I am doing.

   task runsSQL{     description 'run sql queries'     apply plugin: 'java'     apply plugin: 'groovy'      print 'Run SQL'  }  task runSchema{     apply plugin: 'java'     apply plugin: 'groovy'      print 'Run Schema' } 

Here's the output I'm getting. enter image description here

like image 941
Tomin Avatar asked Jan 08 '15 10:01

Tomin


People also ask

How do I run a single test case using Gradle?

single system property can be used to specify a single test. You can do gradle -Dtest. single=ClassUnderTestTest test if you want to test single class or use regexp like gradle -Dtest. single=ClassName*Test test you can find more examples of filtering classes for tests under this link.

How do I run a Gradle file in command prompt?

To run a Gradle command, open a command window on the project folder and enter the Gradle command. Gradle commands look like this: On Windows: gradlew <task1> <task2> … ​ e.g. gradlew clean allTests.


2 Answers

I guess the point that you missed is that you dont define tasks here but you configured tasks. Have a look at the gradle documentation: http://www.gradle.org/docs/current/userguide/more_about_tasks.html.

What you wanted is something like this:

task runsSQL (dependsOn: 'runSchema'){     description 'run sql queries'     println 'Configuring SQL-Task'      doLast() {         println "Executing SQL"     } }  task runSchema << {     println 'Creating schema'  } 

Please mind the shortcut '<<' for 'doLast'. The doLast step is only executed when a task is executed while the configuration of a task will be executed when the gradle file is parsed.

When you call

gradle runSchema 

You'll see the 'Configuring SQL-Task' and afterwards the 'Creating schema' output. That means the runSQLTask will be configured but not executed.

If you call

gradle runSQL 

Than you you'll see:

Configuring SQL-Task :runSchema Creating schema :runsSQL Executing SQL

runSchema is executed because runSQL depends on it.

like image 109
TobiSH Avatar answered Sep 21 '22 05:09

TobiSH


You can use -x option or --exclude-task switch to exclude task from task graph. But it's good idea to provide runnable example.

like image 41
Opal Avatar answered Sep 22 '22 05:09

Opal