Creating New Gradle Builds
Following this guide, you’ll create a trivial Gradle project, invoke some of the basic Gradle commands, and get a sense of how Gradle manages the project.
What you’ll need
- About 11 minutes
- A terminal or IDE application
- A Java Development Kit (JDK), version 1.7 or better (only necessary to run Gradle)
- A Gradle distribution, version 4.6 or better
| Shell commands will shown for Unix-based systems. Windows has analogous commands for each. |
Initialize a project
First, let’s create a new directory where our project will go.
❯ mkdir basic-demo ❯ cd basic-demo
Now we can use Gradle’s
init command to generate a simple project. We will explore everything that is generated so you know exactly what’s going on.❯ gradle init Starting a Gradle Daemon (subsequent builds will be faster) BUILD SUCCESSFUL in 3s 2 actionable tasks: 2 executed
The command should show "BUILD SUCCESSFUL" and generate the following "empty" project. If it doesn’t, please ensure that Gradle is installed properly, and that you have the
JAVA_HOME environment variable set correctly.
This is what Gradle generated for you.
. ├── build.gradle ├── gradle │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle
| Project configuration script for configuring tasks in the current project | |
| Gradle Wrapper executable JAR | |
| Gradle Wrapper configuration properties | |
| Gradle Wrapper script for Unix-based systems | |
| Gradle Wrapper script for Windows | |
| Settings configuration script for configuring which Projects participate in the build |
gradle init can generate various different types of projects, and even knows how to translate simple pom.xml files to Gradle. |
Boom! Roasted. We could just end the guide here, but chances are you want to know how to use Gradle in this project. Let’s do that.
Create a task
Gradle comes with a library of tasks that you can configure in your own projects. For example, there is a core type called
Copy, which copies files from one location to another. The Copy task is very useful (see the documentation for details), but here, once again, let’s keep it simple. Perform the following steps:- Create a directory called
src. - Add a file called
myfile.txtin thesrcdirectory. The contents are arbitrary (it can even be empty), but for convenience add the single lineHello, World!to it. - Define a task called
copyof typeCopy(note the capital letter) in the main build file,build.gradle, that copies thesrcdirectory to a new directory calleddest. (You don’t have to create thedestdirectory — the task will do it for you.)
task copy(type: Copy, group: "Custom", description: "Copies sources to the dest directory") {
from "src"
into "dest"
}
Here,
group and description can be anything you want. You can even omit them, but doing so will also omit them from the tasksreport, used later.
Now execute your new
copy task:❯ ./gradlew copy :copy BUILD SUCCESSFUL in 0s 1 actionable task: 1 executed
Verify that it worked as expected by checking that there is now a file called
myfile.txt in the dest directory, and that its contents match the contents of the same one in the src directory.Apply a plugin
Gradle includes a range of plugins, and many, many more are available at the Gradle plugin portal. One of the plugins included with the distribution is the
base plugin. Combined with a core type called Zip, you can create a zip archive of your project with a configured name and location.
Add the
base plugin to your build.gradle file using the plugins syntax. Be sure to add the plugins {} block at the top of the file.plugins {
id "base"
}
... rest of the build file ...
Now add a task that creates a zip archive from the
src directory.task zip(type: Zip, group: "Archive", description: "Archives sources in a zip file") {
from "src"
}
The
base plugin works with the settings to create an archive file called basic-demo-1.0.zip in the build/distributions folder.
In this case, simply run the new
zip task and see that the generated zip file is where you expect.❯ ./gradlew zip :zip BUILD SUCCESSFUL in 0s 1 actionable task: 1 executed
Explore and debug your build
Let’s see what else we can do with Gradle in our new project. A full reference to the command-line interface is available as well.
Discover available tasks
The
tasks command lists Gradle tasks that you can invoke, including those added by the base plugin, and custom tasks you just added as well.❯ ./gradlew tasks > Task :tasks ------------------------------------------------------------ All tasks runnable from root project ------------------------------------------------------------ Archive tasks ------------- zip - Archives sources in a zip file Build tasks ----------- assemble - Assembles the outputs of this project. build - Assembles and tests this project. clean - Deletes the build directory. Build Setup tasks ----------------- init - Initializes a new Gradle build. wrapper - Generates Gradle wrapper files. Custom tasks ------------ copy - Simply copies sources to a the build directory Help tasks ---------- buildEnvironment - Displays all buildscript dependencies declared in root project 'basic-demo'. components - Displays the components produced by root project 'basic-demo'. [incubating] dependencies - Displays all dependencies declared in root project 'basic-demo'. dependencyInsight - Displays the insight into a specific dependency in root project 'basic-demo'. dependentComponents - Displays the dependent components of components in root project 'basic-demo'. [incubating] help - Displays a help message. model - Displays the configuration model of root project 'basic-demo'. [incubating] projects - Displays the sub-projects of root project 'basic-demo'. properties - Displays the properties of root project 'basic-demo'. tasks - Displays the tasks runnable from root project 'basic-demo'. Verification tasks ------------------ check - Runs all checks. Rules ----- Pattern: clean<TaskName>: Cleans the output files of a task. Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration. Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration. To see all tasks and more detail, run gradlew tasks --all To see more detail about a task, run gradlew help --task <task> BUILD SUCCESSFUL in 0s 1 actionable task: 1 executed
No comments:
Post a Comment