Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle task should not execute automatically

I'm defining a task in gradle:

task releaseCandidate(type: Exec) {     commandLine 'git', 'checkout', 'develop'      // Increment version code in Manifest     String manifest = new File('AndroidManifest.xml').getText('UTF-8')     Pattern pattern = Pattern.compile('android:versionCode="([0-9]+)"')     Matcher matcher = pattern.matcher(manifest)     matcher.find()     int newVersionCode = Integer.parseInt(matcher.group(1)) + 1     manifest = manifest.replaceAll(         "android:versionCode=\"([0-9]+)\"", "android:versionCode=\"$newVersionCode\""     )     new File('AndroidManifest.xml').write(manifest, 'UTF-8')      commandLine 'git', 'diff' } 

Which I want to execute only when I explicitly call it as gradle releaseCandidate. However, when I run any other task, such as gradle assembleDebug, it also runs task releaseCandidate. I don't want that behaviour to happen. There is no task depending on releaseCandidate or vice-versa.

My project is an Android app, so I am using android gradle plugin.

like image 895
André Staltz Avatar asked May 08 '14 15:05

André Staltz


People also ask

Why is Gradle task skipped?

The result of the closure must be true or false . If the task must be skipped, the result of the closure must be false , otherwise the task is executed. The task object is passed as a parameter to the closure. Gradle evaluates the closure just before the task is executed.

What is default task Gradle?

Gradle allows you to define one or more default tasks that are executed if no other tasks are specified. defaultTasks 'clean', 'run' tasks. register('clean') { doLast { println 'Default Cleaning! ' } } tasks.


1 Answers

A common pitfall. Add an action to the task otherwise code will run at configuration phase. Sample task with action:

task sample << { } 

As I see You'd rather need to write a custom task than using Exec type. I suppose it's not valid to define commandLine twice.

EDIT

You can read this post to get the general idea how it all works.

like image 92
Opal Avatar answered Sep 28 '22 10:09

Opal