官术网_书友最值得收藏!

  • Mastering Gradle
  • Mainak Mitra
  • 270字
  • 2021-07-16 20:49:33

Our first script

In the last section, we learned how to install Gradle. Now it's time create our very first Gradle script. This script will print Hello Gradle- This is your first script on the console. Just open a text editor, type in the following three lines, and save the file as build.gradle.

task helloGradle << {
      println 'Hello Gradle- This is your first script'
}

Then execute the gradle helloGradle command as follows:

$ gradle helloGradle
:helloGradle
Hello Gradle- This is your first script
BUILD SUCCESSFUL
Total time: 4.808 secs

So, what have we done here?

  • We have a created a Gradle build script file called build.gradle. This is the default name given to a build file. You can give any name to the build file. However, to execute the script, you must use the -b option with your filename with the gradle command. Otherwise,the build will fail with the Task '%TASK_NAME%' not found in root project '%PROJECT_NAME'."gradle [-b <file name>] [task1 task2 ….. taskn] error.
  • Try the gradle -b <buildfile_name> helloGradle command and you should get the same output.
  • With the gradle command, we have executed a task called helloGradle, which prints a line in the console. So, the parameter we passed to the gradle command is the task name. You can execute one to any number of tasks with the Gradle command and these tasks will be executed in the same order as they appear in the command line.

    Tip

    There is a way to define the default task using the defaultTasks keyword, which will be executed by default, if user does not mention any specific task to execute on the build file. We'll discuss this more in Chapter 3, Managing Task.

The Gradle command initializes the script, reads all tasks mentioned on the command-line, and executes tasks. Moreover, if any task has multiple dependencies, then dependent tasks are executed in alphabetical order unless those tasks themselves enforce the order. You can find more about task ordering in Chapter 3, Managing Task.

Remember that each Gradle build consists of three components: projects, tasks, and properties. Each build has at least one project and one or more tasks. The name of the project is the parent directory name in which the build file exists.

主站蜘蛛池模板: 平乡县| 灵武市| 甘肃省| 瓮安县| 胶州市| 清河县| 内江市| 拉孜县| 渑池县| 黄陵县| 信阳市| 涞水县| 兴城市| 石林| 徐州市| 收藏| 锦屏县| 灵石县| 鞍山市| 武宣县| 崇义县| 黎川县| 于都县| 南通市| 大田县| 嘉荫县| 涞源县| 东光县| 涿州市| 年辖:市辖区| 鄂托克前旗| 集安市| 深州市| 峨山| 万盛区| 偏关县| 开江县| 油尖旺区| 德昌县| 达拉特旗| 恩平市|