A quick start to gradle a modern and efficient java building tool

  • 2021-12-05 06:23:15
  • OfStack

Like Maven1, Gradle only provides a framework for building projects, and what really works is Plugin. By default, Gradle provides us with many commonly used Plugin, including Plugin for building Java projects, War, Ear and so on. Different from Maven, Gradle does not provide built-in project life cycle management, but java Plugin adds many Task to Project, and these Task are executed in turn, creating a project construction cycle like Maven for us. For more information about Maven, readers can visit Maven official website, or refer to Maven learning series articles written by the author.

Now we are all talking about domain-driven design, and the domain objects of Gradle itself mainly include Project and Task. Project provides the execution context for Task, and all Plugin either add Property for configuration to Project or add a different Task to Project. An Task represents a logically independent execution process, such as compiling Java source code, copying files, packaging Jar files, and even executing a system command or calling Ant. In addition, 1 Task can read and set Property of Project to perform specific operations.

Let's look at the simplest Task and create an build. gradle file that reads as follows:


task helloWorld << {
   println "Hello World!"
}

Here's " < < "It means adding execution code to helloWorld-it is actually groovy code. Gradle provides us with a complete set of DSL, so in many cases, the code we write seems to have broken away from groovy, but it is still the executed groovy at the bottom. For example, the above task keyword is actually a method in groovy, and the content between curly braces indicates a closure passed to the task () method. Except" < < "In addition, there are many ways we can define an Task, which we will cover in future articles in this series.

Execute in the same directory as build. gradle:


gradle helloWorld

The command line output is as follows:


:helloWorld
Hello World!

BUILD SUCCESSFUL

Total time: 2.544 secs

By default, Gradle uses the build. gradle file in the current directory as the project's build file. In the above example, we created an Task named helloWorld, which we specified to execute when executing the gradle command. Here, helloWorld is an DefaultTask object, which is the default type for defining an Task, although we can explicitly declare the type of Task, or even customize an Task (we'll talk about it in a later article in this series).

For example, we can define an Task for file copying:


task copyFile(type: Copy) {
   from 'xml'
   into 'destination'
}

The above copyFile copies everything in the xml folder into the destination folder. Both folders here are relative to the current Project, which is the directory where the build. gradle files are located.

There can be dependencies between Task, for example, taskA depends on taskB, so when taskA is executed, Gradle will execute taskB first and then taskA. One way to declare an Task dependency is when defining an Task:


task taskA(dependsOn: taskB) {
   //do something
}

By default, Gradle provides us with several commonly used Task, such as viewing Properties of Project, displaying all Task defined in current Project, and so on. You can view all Task in Project with the command 1:


gradle tasks

The output is as follows:

:tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Build Setup tasks
-----------------
setupBuild - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]

Help tasks
----------
dependencies - Displays all dependencies declared in root project 'gradle-blog'.
dependencyInsight - Displays the insight into a specific dependency in root project 'gradle-blog'.
help - Displays a help message
projects - Displays the sub-projects of root project 'gradle-blog'.
properties - Displays the properties of root project 'gradle-blog'.
tasks - Displays the tasks runnable from root project 'gradle-blog'.

Other tasks
-----------
copyFile
helloWorld

To see all tasks and more detail, run with --all.

BUILD SUCCESSFUL

Total time: 2.845 secs

As you can see, in addition to our own defined copyFile and helloWorld, Gradle also provides us with Task such as dependencies, projects and properties by default. dependencies is used to display dependency information for Project, projects is used to display all Project, including root Project and child Project, and properties is used to display all Property contained in one Project.

By default, Gradle has added many Property to Project, and we can call the following command to view it:


gradle properties

The output is as follows:

:properties

------------------------------------------------------------
Root project
------------------------------------------------------------

allprojects: [root project 'gradle-blog']
ant: org.gradle.api.internal.project.DefaultAntBuilder@1342097

buildDir: /home/davenkin/Desktop/gradle-blog/build
buildFile: /home/davenkin/Desktop/gradle-blog/build.gradle
...
configurations: []
convention: org.gradle.api.internal.plugins.DefaultConvention@11492ed
copyFile: task ':copyFile'
...
ext: org.gradle.api.internal.plugins.DefaultExtraPropertiesExtension@1b5d53a
extensions: org.gradle.api.internal.plugins.DefaultConvention@11492ed
...
helloWorld: task ':helloWorld'
...
plugins: [org.gradle.api.plugins.HelpTasksPlugin@7359f7]
project: root project 'gradle-blog'
...
properties: {...}
repositories: []

tasks: [task ':copyFile', task ':helloWorld']
version: unspecified

BUILD SUCCESSFUL

Total time: 2.667 secs

In the above Property, allprojects represents all Project, which contains only one root Project. In multi-project construction, it will contain multiple Project; buildDir represents the output directory of the build results; Our own definitions of helloWorld and copyFile have also become Property in Project. Additionally, Project includes DefaultAntBuilder (Property named ant) for executing the Ant command and the description attribute description of Project.

In the next article, we will talk about several ways to create Task.

Download the Github sample code for this series of articles as follows:


git clone https://github.com/davenkin/gradle-learning.git

Related articles: