Buildscript code block in gradle

Source: Internet
Author: User

This code is often seen in the build. gradle file when you write the gradle script:

Build. gradle
123456789
buildScript {     repositories {         mavenCentral()}}repositories {     mavenCentral()}

It is easy to wonder why repositories should declare twice? What is the difference between the declaration in the buildscript code block and the declaration in the lower half?

In fact, the answer is very simple. The declaration in buildscript is the resource required by the gradle script. Resources that can be declared include dependencies, third-party plug-ins, and Maven repository addresses. The dependency, repository address, and other information directly declared in the build. gradle file are resources required by the project.

Gradle is compiled by the groovy language and supports groovy syntax. It can flexibly use existing ant plug-ins and JVM-based class libraries, this is also why it is more powerful than Maven, ant, and other build scripts. Although gradle supports out-of-the-box use, if you want to use third-party plug-ins and class libraries in scripts, You need to manually add references to these plug-ins and class libraries. These plug-ins and class libraries do not directly serve projects, but support the running of other build scripts. Therefore, you should place the references in the buildscript code block. When executing a script, gradle will first execute the content in the buildscript code block before executing the remaining build script.

For example, we want to write a task to parse the CSV file and output its content. Although we can use gradle to write code for parsing CSV files, Apache has a library that implements a library for parsing CSV files for our direct use. If you want to use this library, you must add a reference to this library in the gradle. Build File.

Build. gradle
1234567891011121314151617181920212223
buildscript {    repositories {        mavenLocal()        mavenCentral()    }    dependencies {        classpath ‘org.apache.commons:commons-csv:1.0‘    }}import org.apache.commons.csv.*task printCSV() {    doLast {        def records = CSVFormat.EXCEL.parse(new FileReader(‘config/sample.csv‘))        for (item in records) {            print item.get(0) + ‘ ‘            println item.get(1)        }    }}

The use of repositories and dependencies in the buildscript code block is almost the same as that in the build. gradle file directly. The only difference is that you can use classpath to declare dependencies in the buildscript code block. The classpath statement indicates that class loader can use the dependencies you provide when executing other build scripts. This is exactly the purpose of using the buildscript code block.

If you need to use this class library in your project, you need to define the dependencies code block outside the buildscript code block. The following code appears in build. gradle:

Build. gradle
12345678910111213141516171819202122232425262728293031323334
repositories {    mavenLocal()    mavenCentral()}dependencies {    compile ‘org.springframework.ws:spring-ws-core:2.2.0.RELEASE‘,            ‘org.apache.commons:commons-csv:1.0‘}buildscript {    repositories {        mavenLocal()        mavenCentral()    }    dependencies {        classpath ‘org.apache.commons:commons-csv:1.0‘    }}import org.apache.commons.csv.*task printCSV() {    doLast {        def records = CSVFormat.EXCEL.parse(new FileReader(‘config/sample.csv‘))        for (item in records) {            print item.get(0) + ‘ ‘            println item.get(1)        }    }}

For more information, see http://chimera.labs.oreilly.com/books/1234000001741/ch04.html#_buildscript_dependencies.

Buildscript code block in gradle

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.