Learn Java

Hello World

Task & Project

Each project is made up of one or more tasks. A task represents some atomic piece of work which a build performs. This might be compiling some classes, creating a JAR, generating Javadoc, or publishing some archives to a repository.

Hello World

The example below can be found in hello_world.

build.gradle:

task hello {
    doLast {
        println 'Hello world!'
    }
}

Use below command to execute task hello in quiet mode -- log errors only:

gradle -q hello

Result:

Hello world!

Shortcut

build2.gradle:

task hello << {
    println 'Hello world!'
}

Run command gradle -b build2.gradle -q hello, and see the same result. -b flag is used to specify the build file.

Groovy

The build files contain Groovy code.

More

do_xxx.gradle:

task hello << {
    println 'Hello Earth'
}
hello.doFirst {
    println 'Hello Venus'
}
hello.doLast {
    println 'Hello Mars'
}
hello << {
    println 'Hello Jupiter'
}

Run command gradle -q -b do_xxx.gradle hello to see the result:

Hello Venus
Hello Earth
Hello Mars
Hello Jupiter