"onlyIf" issue in gradle 1.8 release candidates (1.8-rc-1 and 1.8-rc-2)


(Igor Popov) #1

How to reproduce the issue

git clone https://github.com/sensui/gradle-onlyif-issue.git
gradlew mytask

The task that fails

task mytask(onlyIf: true) << {
  println 'executing mytask'
}

Actual output

$ gradlew mytask
  FAILURE: Build failed with an exception.
  * Where:
Build file 'D:\projects\others\gradle-onlyif-issue\build.gradle' line: 1
  * What went wrong:
A problem occurred evaluating root project 'gradle-onlyif-issue'.
> Could not create task 'mytask': Unknown argument(s) in task definition: [onlyIf]
  * Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
  BUILD FAILED
  Total time: 0.879 secs

Expected output

$ gradlew mytask
:mytask
executing mytask
  BUILD SUCCESSFUL
  Total time: 1.737 secs

More details

$ gradlew --version
  ------------------------------------------------------------
Gradle 1.8-rc-2
------------------------------------------------------------
  Build time:
 2013-09-19 05:33:14 UTC
Build number: none
Revision:
   4f5d61b493480a5541898257eb1e4eb280d38826
  Groovy:
     1.8.6
Ant:
        Apache Ant(TM) version 1.9.2 compiled on July 8 2013
Ivy:
        2.2.0
JVM:
        1.6.0_27 (Sun Microsystems Inc. 20.2-b06)
OS:
         Windows 7 6.1 amd64

Important

The build passes when using gradle 1.7, but fails when using any of the 1.8 release candidates.


(Peter Niederwieser) #2

You cannot use ‘onlyIf’ as a named argument. In 1.7 and below, this had no effect but wasn’t reported as an error. From 1.8 onwards, it’s reported as an error. The correct syntax is:

task foo {
    onlyIf { someExpr }
}

To disable a task from the get-go, you can do:

task foo {
    enabled = false
}

(Igor Popov) #3

Hi,

Thanks for the information. It seems that I had an issue in my build which I hadn’t noticed… It’s a good thing that gradle reports errors earlier.

I have updated the sample repository with the fix.

Thanks again and sorry for the trouble created…


(Peter Niederwieser) #4

No problem. Thanks for reporting back.