Create a New Gradle build

Following this guide, you will create a simple Gradle project, invoke some basic Gradle commands, and see how Gradle manages projects.

What do you need?

  • 11 minutes to get started
  • Terminal or IDE applications
  • JDK 1.7 or later (for running Gradle only)
  • Graddle version 4.5 or above

Shell commands will be displayed for UNIx-based systems. Windows has a similar command for every command.

Initialize a project

Create a directory for your projects

Let's say I create an init directory on my desktopCopy the code

We can use Gradle init command to generate a simple project structure. We will explore all the things that arise to know exactly what happened.

The resulting basic structure is shown in figure 1

The command should say BUILD SUCCESSFUL and generate the following empty project. If not, make sure Gradle is installed correctly and the JAVA_HOME environment variable is set correctly.

Here is what Gradle generates for you.

. ├ ─ ─ build. Gradle 1 ├ ─ ─ gradle │ └ ─ ─ wrapper │ ├ ─ ─ gradle - wrapper. Jar 2 │ └ ─ ─ gradle - wrapper. The properties of 3 ├ ─ ─ gradlew 4 ├── Exercises. ├─ SettingsCopy the code
  1. A project configuration script that configures tasks in the current project (task)
  2. Executable fileJAR
  3. Gradle WrapperConfiguration properties
  4. For aUnixThe system ofGradle WrapperThe script
  5. Apply toWindowstheGradle WrapperThe script
  6. A setup configuration script that configures which projects participate in the build

Gradle Init can generate a variety of different types of projects and even know how to convert simple pom.xml files to Gradle.

We can end the tutorial here, but you may be wondering how to use Gradle in this project. Let’s do this.

Creating a Task

Gradle provides apis for creating and configuring tasks through Groovy or Kotlin-based DSLS. A Project contains A set of Tasks, each of which performs some basic operations.

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. This Copy task is very useful (see the documentation for details), but here, again, let’s keep it simple. Perform the following steps:

  1. Create a directory named SRC.

  2. Add a myfile.txt file to the SRC directory. The content is arbitrary (and can even be empty), but for convenience, a single line of Hello, World! To it.

  3. In the main build file build.gradle, define a Copy task with type Copy (note the uppercase letters) that copies the SRC directory to a new directory named dest. (You don’t have to create the dest directory – the task will be done for you).

task copy(type: Copy, group: "Custom", description: "Copies sources to the dest directory") {
    from "src"
    into "dest"
}
Copy the code

In this case, the group and the description can be anything you want. You can even ignore them, but doing so will also ignore them in the Tasks report, which will be used later.

Now, execute our copy task

Apply a plug-in

Gradle contains a number of plug-ins, and many more are available in the Gradle Plug-ins portal. One of the plug-ins included in the plug-in is the Base plug-in. Combined with the core type Zip of the call, you can create a Zip archive of the project with the configured name and location.

Use the syntax to add the base plugin to the plugins in the build.gradle file. Make sure plugins {} adds the block to the top of the file.

plugins {
    id "base"}... rest of the build file ...Copy the code

Now add a task to create a ZIP archive from the SRC directory.

task zip(type: Zip, group: "Archive", description: "Archives sources in a zip file") {
    from "src"
}
Copy the code

This base plugin will create an archive file called basic-Demo-1.0.zip in the Build/Iterate directory.

In this case, simply running the new ZIP task and viewing the resulting ZIP file is what you’d expect.

Explore and debug your build

Let’s see what else we can do with Gradle in our new project. A command line interface is also available!

Discover available taskstask

The Tasks command lists the Gradle tasks you can invoke, including the tasks added by the Base plug-in and the custom tasks you just addedCopy the code

Analyze and debug your build

Gradle also provides a rich, Web-based view of your builds, called build scans

There is a little bit of untranslated content here !!!!

Finding available properties

The properties command tells you all the properties about the project.

There are many attributes. Here are just a few of the attributes available:

You can change many properties. For example, you can try adding the following lines to the build.gradle file and then re-executing gradle Properties.

description = "A trivial Gradle build"
version = "1.0"
Copy the code

A,eclipsecreategradleproject

  1. Configure Gradle (your own local gradle version)

  1. Start creating the project

  1. Project structure drawing

Two, fragmentary knowledge

  1. Dependencies cannot appear with Spaces

  1. Dependency introduces three ways of writing it


Gradle

  1. Geek Academy Wiki
  2. w3cschool