Gradle 入门

What?

Gradle 是java 领域继 Ant , Maven 之后第三代项目自动化构建工具,采用Groovy 编写,支持Maven,Lvy 仓库。

How?

1,安装gradle

在安装gradle 之前,首先需要电脑上已经安装jdk,如果没有安装,先安装JDK,http://www.oracle.com/technetwork/java/javase/downloads/index.html

安装好JDK之后,下载gradle , https://gradle.org/install/,解压到目录,然后将更目录加入环境变量。

新建环境变量 GRADLE_HOME 值为 gradle 根目录

将%GRADLE_HOME%\bin;加入path变量。

 

2,编写构建描述文件 build.gradle

和Ant 的build.xml ,Maven的pom.xml 一样,gradle 也有一个项目构建的配置文件,采用Groovy 语言格式。

新建一个文本文件,命名为 build.gradle

内容:

1 task helloworld << {
2    println 'hello'
3 }

然后在当前目录执行

gradle -q helloworld

会看到有hello 打印,我们第一个gradle 任务执行成功了~

 

3,Gradle 任务

和Maven 的构建目标(goals) 类似,Gradle 的构建过程也是完成一项项的任务。

如何定义任务呢?用task 关键字定义任务

1 task hello {
2    doLast {
3       println 'hello world'
4    }
5 }

也可以用<< 代替 doLast

1 task hello << {
2       println 'hello world'
3    
4 }

 

4,任务的依赖关系

1 task hello << {
2     println 'Hello world!'
3 }
4 task intro(dependsOn: hello) << {
5     println "I'm Gradle"
6 }

5,懒依赖

1 task taskX(dependsOn: 'taskY') << {
2     println 'taskX'
3 }
4 task taskY << {
5     println 'taskY'
6 }

6,支持命名空间

1 task hello
2 
3 println hello.name
4 println project.hello.name

7,另一种方式添加依赖

1 task taskY << {
2    println 'taskY'
3 }
4 task taskX << {
5    println 'taskX'
6 }
7 taskY.dependsOn taskX

8,通过闭包添加依赖

 1 task taskX << {
 2    println 'taskX'
 3 }
 4 
 5 taskX.dependsOn {
 6    tasks.findAll { 
 7      task -> task.name.startsWith('lib') 
 8    }
 9 }
10 task lib1 << {
11    println 'lib1'
12 }
13 task lib2 << {
14    println 'lib2'
15 }
16 task notALib << {
17    println 'notALib'
18 }

9,任务描述

1 task copy(type: Copy) {
2    description 'Copies the resource directory to the target directory.'
3    from 'resources'
4    into 'target'
5    include('**/*.txt', '**/*.xml', '**/*.properties')
6    println("description applied")
7 }

10,抛出 StopExecutionException 异常后,任务将终止

 1 task compile << {
 2     println 'We are doing the compile.'
 3 }
 4 
 5 compile.doFirst {
 6     // Here you would put arbitrary conditions in real life.
 7     // But this is used in an integration test so we want defined behavior.
 8     if (true) { throw new StopExecutionException() }
 9 }
10 task myTask(dependsOn: 'compile') << {
11    println 'I am not affected'
12 }

11,声明依赖关系

 1 apply plugin: 'java'
 2 
 3 repositories {
 4    mavenCentral()
 5 }
 6 
 7 dependencies {
 8    compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
 9    testCompile group: 'junit', name: 'junit', version: '4.+'
10 }

12,定义存储库

Maven 中央仓库

1 repositories {
2    mavenCentral()
3 }

自定义远程仓库

1 repositories {
2    maven {
3       url "http://repo.mycompany.com/maven2"
4    }
5 }

 

13,定义外部依赖

1 dependencies {
2    compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
3 }

 14,发布文件

1 apply plugin: 'maven'
2 
3 uploadArchives {
4    repositories {
5       mavenDeployer {
6          repository(url: "file://localhost/tmp/myRepo/")
7       }
8    }
9 }

 15,Gradle 插件

插件只是一组任务,几乎所有的任务,如编译任务,设置域对象,设置源文件等都由插件处理。

gradle 插件接口是 Project.apply()

加载脚本插件:

1 apply from: 'other.gradle'

加载二进制插件:

使用插件ID,或全限定名标识插件

1 apply plugin: JavaPlugin
1 plugins {
2    id 'java'
3 }
1 plugins {
2    id "com.yiibai.bintray" version "0.1.0"
3 }

16,编写自定义插件

编写一个Hello 插件,它将一个hello 任务添加到项目中

1 apply plugin: HelloPlugin
2 
3 class HelloPlugin implements Plugin<Project> {
4    void apply(Project project) {
5       project.task('hello') << {
6          println "Hello from the HelloPlugin."
7       }
8    }
9 }

 

End

 

转载于:https://www.cnblogs.com/t0000/articles/9428631.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值