A repository for notes and example script(s) for the the basic tutorial on the Gradle website.
The tutorial actually assumes you've already downloaded and installed Gradle -- so the first step for me was to grab Gradle.
Homebrew is your friend. Checkout this article for hints on using Homebrew to install Gradle.
Either Chocolatey, Scoop or just download the latest from the Gradle website.
The files in this repository were all created while following this tutorial on the offical Gradle website:
-
Create a folder for the tutorial, call it something like
/gradle-demo/
. -
Inside your new folder create a Gradle build file -
build.gradle
.
- Run the
gradle tasks
command to see the default tasks available from Gradle.
- Run the
gradle wrapper
command to automatically generate a Gradle wrapper file.
This is an interesting step because you are making it so that a future user/client of your project won't have to download and setup Gradle - they can simply use the generated Gradle wrapper (gradlew
or gradlew.bat
on Windows) on their platform of choice -- and Gradle will automagically be downloaded and configured. See the tutorial for more details on what folders and files are generated with this command.
-
Run the
gradlew properties
command to see the properties that are avaiable from Gradle. -
Add some properties to your
build.gradle
file and re-run thegradle properties
to see that your added properties are used -- in fact the description is used at the top of the output!
-
Create a folder inside your tutorial folder called
/src/
. -
Create a file inside your
src
folder called something likemyfile.txt
- add some text to it just for fun -- like "Hello, World!". -
Now, the fun part, you are going to create a
task
inside your build.gradle file that'll make use of the folder and file you just created -- to demonstrate some of the built in capabilities of Gradle (Core).
task copy(type: Copy) {
from 'src'
into 'dest'
}
-
Now, re-run Step 2 to list out all of the Gradle tasks, including your newly created copy task.
-
Finally, execute your copy task by running
./gradlew copy
(orgradlew copy
or Windows)
- At the top of your
build.gradle
you'll need to add a reference to a plugins
plugins {
id 'base'
}
- Then, below your first task, create a new one that makes use of the plugin your just added.
task zip(type: Zip) {
from 'src'
}
-
Now, re-run Step 2 again so you can confirm that you see your new task.
-
Finally, run your task by running
./gradlew zip
and note that in the build/distributions/ folder there is a zip file named after your project folder. -
Now, just so you can see some other capabilities of Gradle, you can use one of the built in tasks, called
Clean
to clean up../gradlew clean
Note, it'll remove the build folder that you just generate the zip file into.
- This time you're just going to create a simple ad hoc task in the same
build.gradle
file.
task hello {
doLast {
println 'Hello, World!'
}
}
- Now, to wrap up, you can run your newly created task by running
./gradlew hello
.
This is a very simple introduction to an interesting concept -- writing custom Gradle tasks.