Gradle User Guide

Source: Internet
Author: User
  1. Download and install gradle 2.1.
    1. : Http://www.gradle.org/learn
    2. Prerequisites for installation: gradle requires JDK 1.6 or later (JRE can be verified using Java-version). gradle comes with the groove lib library, so you do not have to install groove, any installed groove will be ignored. Gradle uses JDK in the path. Of course, you can set java_home to point to your JDK installation path.
    3. The gradle release is a zip compressed file. The complete release includes:
      1. Binary files
      2. User Guide (HTML and PDF)
      3. DSL Reference Guide
      4. API documentation (Javadoc and yydoc ).
      5. A large number of examples, including examples referenced in the user guide, some complete and more complex builds, you can build your own.
    4. Create a new environment variable gradle_home with the value of gradle's installation directory. Add/bin to the PATH variable.
    5. You can run gradle-V to verify the installation. The output shows the gradle version and local environment variable configuration (groove version, JVM version, and so on ).
    6. You can run gradle by setting JVM environment variables. Of course, you can also use gradle_opts or java_opts, or both. Java_opts environment variables are shared by many Java applications by convention. A typical example is to set the memory options of the HTTP Proxy java_opts and gradle_opts. You can also set the start options of gradle and gradlew scripts for these variables.

    Note: These cannot be used to set command line JVM options.

  2. Build script Basics
    1. All resumes in gradle are built on two foundations: Project projects and task tasks.

      Each gradle build consists of one or more projects. What a project represents depends on how you use gradle. For example, a project may represent a jar lib library or a Web application; it may also represent a zip distribution extracted from jars of other projects; a project must represent a build; it may mean one thing to do. For example, deploy your application to the workbench or production environment. If this looks a bit fuzzy, don't worry, gradle build-by-Convention supports adding more specific definitions for a project.

      Each project consists of one or more tasks. A task represents an atomic block for some build work. This may be to compile some classes, create a jar, generate javadoc, or publish the archive to the repository.

      Now let's take a look at defining some simple tasks in a build project. The subsequent sections will show how to process multiple projects and more about work projects and tasks.

    2. Hello World

      Run the gradle command to build the gradle. This command looks for the build. gradle file in the current directory. We usually call this file a build script. Although it is strictly a build configuration script, as we will see later, this script defines a project and its documentation.

      Let's try to create a build Script: Build. gradle

      In a shell command line, move to the directory where build. gradle is located and execute the build Script: gradle-Q hello.

      Execution of a build script:

      There is a shorthand method to define a task, such as Hello above. This shorthand method looks more concise:

      A closure execution script named hello is defined above. This task definition style will be used throughout the user guide.

      The gradle build script gives you all the power of groovy. As an appealer, first look at this:

      Or

      As follows:

      As you think, you can declare tasks dependent on other tasks.

      Insert: run the command line to create the echo.> build. gradle file.

      Question: How to output Chinese characters in the command line

      Add a dependency. This dependency does not exist (not must be defined before dependency *)

      Task taskx's dependency Declaration on task is important for multi-project construction before task definition. Task dependencies will be discussed in more detail in 15.4 adding task dependencies.

      Note: you cannot use the shortcut key to point to an undefined task.

      Groovy not only defines what a task does, for example, it can also dynamically create a task.

      Once a task is created, it can be accessed through APIS. For example, you can dynamically add dependencies for a task at runtime. Ant does not support this.

      Alternatively, you can add actions to an existing task.

      Note: The Task Name is case-insensitive (when executed ).

      Dolast and dofirst can be called and executed multiple times. They add an action at the beginning or end of the task action list, which is executed sequentially in the action list. <Operator is an alias of dolast.

      As you noted, there is a convenient symbol in the example to access an existing task. Each task can be viewed as an attribute of the build script.

      This makes the code very readable, especially when using the tasks provided by the plug-in, such as compile compilation tasks.

      You can add your own attributes to a task. For example, add a property name myproperty, set Ext. myproperty's default value. After the property is complete, it can read and set the value like a predefined task.

      Note: ext here should come with gradle. The specified attribute cannot be found after it is removed.

      Additional attributes are not limited to tasks. You can read more about them: 13.4.2 "Additional attributes ".

      The ant task is the first-class citizen of gradle. It relies on groovy to provide excellent Jicheng for ant tasks. Using gradle is more powerful and convenient than the original build. xml. In the following example, you can learn how to execute ant tasks and access ant attributes.

      Note: ant has not been done, skipped

      You can perform more operations on the ant build script ,??Use ant from gradle

      Later, you will see that these methods can be shared with the construction of sub-projects in the project. If your building logic is complex, gradle provides you with other convenient ways to organize it .? Chapter? 17 ,?Using Ant from gradle

???? Gradle allows you to define one or more default build tasks.

????

This is equivalent to running clean and run. Each sub-project built in multiple projects can have a specific default task. If a sub-project does not specify a default task, use the default task of the Parent Project (if any ).

Configure Dag

We will describe in detail in Chapter 56 "establish lifecycle", which has the configuration stage and execution stage. After the configuration phase, gradle should know all tasks executed. Gradle provides a hook to use this information. A Use Case checks that the task to be released is in the middle of the execution task. Based on this, you can assign different values to variables.

In the following example, The result variables for executing the assignment and release tasks are different values.

It is important that whenready affects the release of a task before executing the Task Command. These tasks make publishing a task not the first task (the task is passed to the gradle command ).

In this chapter, let's just take a look at the task. This is not the end of the task introduction. If you want to enter more details, Chapter 15th, and more tasks.

Gradle User Guide

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.