Gradle 1.12 User Guide translation-Chapter 2. Gradle plug-in, gradle1.12

Source: Internet
Author: User

Gradle 1.12 User Guide translation-Chapter 2. Gradle plug-in, gradle1.12

Last night, I only watched the last few episodes of the TV series "those in a hurry" with my girlfriend. Therefore, the fifth chapter of the "Gradle plug-in user guide" of the Android document was not translated much. So today, I cannot translate the fifth chapter. I will try to see Chapter 1.12 of Gradle 21st user guide translated a few weeks ago.

For translation of other chapters posted on the blog, see:

Http://blog.csdn.net/column/details/gradle-translation.html

Please pay attention to the address on Github for translation projects:

Https://github.com/msdx/gradledoc/tree/1.12.

To view the bilingual documents directly, visit:

Http://gradledoc.qiniudn.com/1.12/userguide/userguide.html.

In addition, Android mobile users can browse documents through a program I wrote. The address of the Android system with the cache function is as follows:

Http://www.wandoujia.com/apps/com.githang.gradledoc

It is not easy to translate. For more information, see the source of this article on the CSDN blog:

Http://blog.csdn.net/maosidiaoxian/article/details/41977599

For my translations of Gradle, refer to the project on Github and documents on the http://gradledoc.qiniudn.com. If any translation error is found, it will be updated in the above two places first. Due to time and energy problems, the translations published in the blog are basically not modified simultaneously.


Chapter 2. Gradle plug-in

Gradle provides some small but useful functions at its core for automation in the real world. All useful functions, such as compiling Java code, are implemented throughPlug-ins. The plug-in adds a new task (for exampleJavaCompile), Domain object (for exampleSourceSet), Convention (for example, the main Java source code is located insrc/main/java), As well as the extended core object and Other plug-in objects.

In this chapter, we will discuss how to use plug-ins and concepts related to terms and plug-ins.

21.1. application plug-ins

All plug-ins are considered to beApplication, ThroughProject.apply()Method.

Example 21.1. application plug-in

build.gradle

apply plugin: 'java'

Plug-ins all have a short name that represents their own .. In the preceding example, we use a short name.javaApplyJavaPlugin.

We can also use the following syntax:

Example 21.2. type the application plug-in

build.gradle

apply plugin: org.gradle.api.plugins.JavaPlugin

Because Gradle is imported by default (see Appendix E,Existing IDE support and how to cope with the unsupported situation), You can also write as follows:

Example 21.3. type the application plug-in

build.gradle

apply plugin: JavaPlugin

The plug-in application isIdempotent. That is to say, a plug-in can be applied multiple times. If the plug-in has been applied before, no further application will have any effect.

A plug-in is implemented by anyPluginSimple class of the interface. Gradle provides core plug-ins as part of its own package, so you need to simply apply the plug-in above. However, for third-party plug-ins, You need to configure the plug-ins to make them available in the build class path. For more information about how to perform this operation, see section 59.5 "build external dependencies of scripts ".

For more information about writing your own plug-ins, see section 58,Compile custom plug-ins.

21.2. What have the plug-ins done?

Apply the plug-in to the project to allow the plug-in to expand the project's functions. What it can do is:

  • Add tasks to projects (such as compilation and testing)
  • Use useful default settings to pre-configure added tasks.
  • Add dependency configurations to the project (see section 8.3 "dependency configuration ").
  • Add new attributes and methods for existing types through extension.

Let's take a look:

Example 21.4. Add a task using a plug-in

build.gradle

apply plugin: 'java'task show << {    println relativePath(compileJava.destinationDir)    println relativePath(processResources.destinationDir)}

gradle -q showOutput result

> gradle -q showbuild/classes/mainbuild/resources/main

The Java Plug-in has been added to the project.compileJavaTask andprocessResourcesAnddestinationDirAttribute.

21.3. Conventions

Plug-ins can pre-configure projects in a smart way to support better conventions than configurations. Gradle provides mechanisms and comprehensive support for this, and it is powerful-however-a key factor in the concise build script.

In the preceding example, the Java Plug-In adds a task namedcompileJava, There isdestinationDir(That is, the place where the compiled Java code is stored ). By default, this property points tobuild/classes/main. This is throughReasonable defaultIs better than the configuration example.

We can simply change this attribute by giving it a new value.

Example 21.5. Change the default settings of the plug-in

build.gradle

apply plugin: 'java'compileJava.destinationDir = file("$buildDir/output/classes")task show << {    println relativePath(compileJava.destinationDir)}

gradle -q showOutput result

> gradle -q showbuild/output/classes

However,compileJavaA task is probably not the only task that needs to know where the class file is.

Added the Java Plug-inSource setsConcept (seeSourceSet) To describe all aspects of the source file set, one of which is the place where these class files should be written during compilation. Java Plug-incompileJavaTaskdestinationDirThis attribute is mapped to the source file set.

We can use this source code set to modify the location of the class file to be written.

Example 21.6. Agreed object in the plug-in

build.gradle

apply plugin: 'java'sourceSets.main.output.classesDir = file("$buildDir/output/classes")task show << {    println relativePath(compileJava.destinationDir)}

gradle -q showOutput result

> gradle -q showbuild/output/classes

In the preceding example, we apply the Java Plug-in and perform the following operations:

  • A new domain object type is added:SourceSet
  • The default (general) attribute ConfigurationmainSource code set
  • Configure tasks that support using these attributes to perform work

All of this happensapply plugin: "java"This step is in progress. In the above example, after the agreed configuration is executed,ModifiedClass file location. In the preceding example, you can note that,compileJava.destinationDirTo reflect the configuration modification.

Consider another job that consumes files. If this task usessourceSets.main.output.classesDirThen the value of this position is modified, no matter when it is modified, it will be updated at the same time.compileJavaTask and this consumer task.

The property of this configuration object to reflect the value of the task of another object in all time (or even when it is changed) is called"Ing conventions". It allows Gradle to achieve a simple configuration mode by better conventions than configuration and reasonable default values. In addition, if the default rule needs to be modified, it does not need to be completely reconfigured. Without this, in the above example, we will have to reconfigure every object that needs to use the class file.

21.4.

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.