How to run a separate gradle task from the command line

In my project, I have several tasks in my build.gradle. I want these tasks to be independent at work. those. I need to run a separate task from the command line. But the "gradle taskA" command will run both taskA and taskB, which I don't want. How to prevent the task from completing ?.

Here is an example 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 is the result I get. enter image description here

+7
java gradle
source share
2 answers

You can use the -x or --exclude-task option to exclude a task from the task schedule. But it would be nice to provide a runnable example.

+3
source share

I assume that you missed the fact that you did not define tasks here, but configured tasks. Take 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' } 

Note the shortcut '<<for' doLast '. The doLast step is performed only when the task is completed, when the configuration of the task is completed during the analysis of the gradle file.

When you call

 gradle runSchema 

You will see "Configure SQL Task" and then the output "Create Schema". This means that runSQLTask will be configured, but will not be executed.

If you call

 gradle runSQL 

What you will see:

Setting up an SQL task: runSchema Creating a schema: runsSQL Executing SQL

runSchema runs because runSQL depends on it.

+11
source share

All Articles