Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Gradle project creation method

  • Ant tools: Introduced in 2000, Ant is an XML-based build tool that requires manual scripting to build projects

  • Maven tools: Introduced in 2007 to add dependency management to Ant

  • Gradle tools: Launched in 2012, adding DSL custom extensions to Maven

  • Gradle is built using Groovy scripts by default. Since Gradle 4.0, Gradle has officially supported Kotlin language builds

  • Gradle is powerful enough to build any project, such as JavaEE, Android, and the front end, with automated builds and fast delivery.

  1. Create a Gradle program

【File】→【New】→【Project】→【Gradle】

Name →【Next】

  1. Configure the Gradle program

  • If Gradle version 4.0 or below is used, the application file in build.gradle. KTS is prone to error. The Tasks folder in the Gradle Projects window on the right does not contain the Application folder
  • Gradle builds slowly if you download Gradle from gradle.org/. Gradle builds slowly if you download Gradle from gradle.org/.
  • Gradle 4.1 corresponds to JDK version 1.8

The default IDEA uses groovy scripts to build projects. Rename the build.gradle file in the project to build.gradle.kts.

MainClassName corresponds to the absolute path of the program entry class

  1. Create function classes in Gradle programs

  1. Run the Gradle program

  • Java code coexists with Kotlin code
  1. Configure the build.gradle. KTS file

The kotlin-stdlib.jar package is a repository for Kotlin, which is repositories for the application mainClassName, which is a repository for the application

  1. Create a Boy. Kt

The task of Gradle

  • Gradle’s own domain objects are projects and tasks
  • A Project provides a container and context for tasks to execute
  • Project is an interface in Gradle’s API. Gradle works by inserting code into a Project as tasks in Gradle scripts. When a Project executes these tasks, Gradle starts working
  1. Create a task in build.gradle.kts

The task() method takes two arguments. The first argument is the name of the task, set to Helloworld, and the second argument is a closure that Kotlin wrote to implement the task, printing a “Hello World!! !” string

  • Dependency management means that if one thing is not done, the next thing cannot be started. There is a dependency between the two things
  • Dependents are implemented in a dependsOn() method

  • Gradle task lifecycle is divided into “scan time” and “run time”. The characteristic of a scan time task is that all tasks are executed in the same order as the task code in the build.gradle. KTS file. If you want to perform the specified task, you need to use the run-time task to do so without performing other unrelated tasks. Runtime tasks are executed only during the run phase, not during the scan phase

  • Gradle tasks are scan-time tasks by default. If you want to use run-time tasks, you need to add the doFirst() higher-order function

  • The task at scan time is generally used to declare variables in the program, while the task at run time is mainly used to execute the business logic in the program

  • Tasks can declare a task set, and a task set can put all individual tasks in a single set, making it easy to manage multiple tasks

  • Properties is a Map of all the default properties in a project

  • Inputs incremental update API: inputs, outputs
  • When the source code is compiled, the input is the code in the SRC directory and the output is the compiled class file. If the input or output changes, the updateTask task is executed

Gradle extension and dependency management

  • Adding a dependency automatically adds the dependency package it depends on to the project

  • Maven Center mvnrepository.com/

  • Testcompile declares test-time dependencies. Test-time dependencies are only used during the test phase, not when the project is packaged to go live, so that the size of the project is smaller

  • Extend the Copy task to Copy files from the SRC directory to the temp directory

  • The from() method is used to declare the source directory, and the into() method is used to declare the destination directory, which is automatically created if the destination directory does not exist

  • Extend the Delete task to Delete the temp directory

  • Jar extension task, class file generated Jar package and stored in the temp directory

  • The include() method is mainly used to set the file type to class, generate the my.jar package from the class files in the out/production/classes directory, and save the package in the temp directory

  • Gradle invokes external commands to manipulate Java bytecode files

To run the main() method in Hello, double-click on compileJava in the Other folder at the bottom of the Gradle Projects window. You can see a hello.class bytecode generated in the project’s build/classes/ Java /main directory

Copy the generated hello. class file to the current project directory, in the same directory as the build.gradle. KTS file, and then simply modify the contents of the build.gradle. KTS file to run the bytecode file

Javaexec means to execute a Java command, main means the name of the class to execute Hello, classpath means the directory where the bytecode file is located, and the argument “.” in the classpass() method means that the bytecode file is located in the current project directory


Once studied Kotlin notes, transplantation.