Hello world task not found error


(Chryssa Charalambides) #1

I am just starting with Gradle and I am having problems right off the bat :slight_smile:
I have create a gradle build script with the following in it task
helloWorld << {
println ‘hello, world’
}

I installed both jdk and gradle on my machine. Set up the JAVA_HOME, GRADLE_HOME variables and added both to the path. Installation seems fine since both java and gradle version command on the command line return the appropriate versions.

BUT when I try to run the gradle script I get the following error

FAILURE: Build failed with an exception.

  • What went wrong:
    Task ‘helloWorld’ not found in root project ‘gradle_start’.

  • Try:
    Run gradle tasks to get a list of available tasks. Run with --stacktrace option
    to get the stack trace. Run with --info or --debug option to get more log output
    .

When I try to list the tasks I only get
FAILURE: Build failed with an exception.

  • What went wrong:
    Task ‘helloWorld’ not found in root project ‘gradle_start’.

  • Try:
    Run gradle tasks to get a list of available tasks. Run with --stacktrace option
    to get the stack trace. Run with --info or --debug option to get more log output
    .

which does not include my helloWorld task. Does anyone have any suggestions how to fix this?


(Chris Doré) #2

It looks like you have task and helloWorld on separate lines. If so, put them on the same line.


(Chryssa Charalambides) #3

oh no, I actually don’t on the file. It just copied this way in this window


(Benjamin Muschko) #4

You are missing the task keyword in your declaration. Please make sure to properly format your code samples in future posts. It should be the following:

task helloWorld << {
    println 'hello, world'
}

Please keep in mind that the left shift operator has been deprecated and with be removed in a future version of Gradle. To be future-proof please use doLast.

task helloWorld {
    doLast {
        println 'hello, world'
    }
}

(Chryssa Charalambides) #5

the task declaration is there. Sorry the format on the post is off. I have tried both the following and I still get the same error
task helloWorld {
doLast {
println ‘hello world’
}
}

and

task helloWorld << {
println ‘hello world’
}


(Benjamin Muschko) #6

Maybe you are not executing Gradle from the directory that contains the build.gradle file. Please provide your code as a sample project on GitHub. Otherwise, I am not sure how to help you. I don’t see an issue with the code you are posting.


(Chryssa Charalambides) #7

I figured it out. I was saving the file as gradle.build when it should be build.gradle, doh!