【项目笔记】Gradle学习3

一、Gradle Groovy 快速入门

构建一个 Groovy 项目,需要使用 Groovy 插件。该插件扩展了 Java 插件,对项目增加了 Groovy 的编译功能。项目可以包含 Groovy 源码,Java 源码,或者两者都包含。

一个基本的 Groovy 项目

使用 Groovy 插件

apply plugin: 'groovy'   

要使用 groovy 编译任务,还必须声明要使用的 Groovy 版本以及从哪里获取 Groovy 库。可以通过在 groovy 配置中添加依赖来完成。compile 配置继承了这个依赖,从而在编译 Groovy和 Java 源代码时,groovy 库也会被包含在类路径中。

  • 例子:使用 Maven 中央仓库中的 Groovy 2.2.0 版本。
repositories {
    mavenCentral()
}
dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.2.0'
}  
  • 构建文件
apply plugin: 'eclipse'
apply plugin: 'groovy'
repositories {
    mavenCentral()
}
dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.2.0'
    testCompile 'junit:junit:4.11'
}  

运行 gradle build 将会对你的项目进行编译,测试和打成 jar 包。
一个 Groovy 项目也 是一个 Java 项目, 由于 Groovy 工程也是一个 Java 工程,因此你能用 Java 做的事情 Groovy 也能做。

二、Gradle Web 工程构建

Gradle 为 Web 开发提供了两个主要插件,War plugin 和 Jetty plugin。

  • 其中 War plugin 继承自 Java plugin,可以用来打 war 包。
  • jetty plugin 继承自 War plugin 作为工程部署的容器。

打 War 包

apply plugin: 'war'

由于继承自 Java 插件,当你执行 gradle build 时,将会编译、测试、打包你的工程。Gradle 会在 src/main/webapp 下寻找 Web 工程文件。编译后的 classes 文件以及运行时依赖也都会被包含在 War 包中。

Web 工程启动

apply plugin: 'jetty'

由于 Jetty plugin 继承自 War plugin。调用 gradle jettyRun 将会把你的工程启动部署到 jetty 容器中。调用 gradle jettyRunWar 会打包并启动部署到 jetty 容器中。

待添加:使用哪个 URL,配置端口,使用源文件的地方,可编辑你的文件,以及重新加载的内容。

三、Gradle 命令行的基本使用

多任务调用

可以以列表的形式在命令行中一次调用多个任务。例如 gradle compile test 命令会依次调用,并且每个任务仅会被调用一次(由于每个任务仅会被调用一次,所以调用 gradle test test 与调用 gradle test 效果是相同的)。compile 和 test 任务以及它们的依赖任务。无论它们是否被包含在脚本中:即无论是以命令行的形式定义的任务还是依赖于其它任务都会被调用执行。

task compile << {
    println 'compiling source'
}
task compileTest(dependsOn: compile) << {
    println 'compiling unit tests'
}
task test(dependsOn: [compile, compileTest]) << {
    println 'running unit tests'
}
task dist(dependsOn: [compile, test]) << {
    println 'building the distribution'
}

dist 和 test 都依赖于 compile,只用当 compile 被调用之后才会调用 gradle dist test 任务。
在这里插入图片描述

\> gradle dist test
:compile
compiling source
:compileTest
compiling unit tests
:test
running unit tests
:dist
building the distribution
BUILD SUCCESSFUL
Total time: 1 secs

排除任务

用命令行选项 -x 来排除某些任务

\> gradle dist -x test
:compile
compiling source
:dist
building the distribution
BUILD SUCCESSFUL
Total time: 1 secs

test 任务并没有被调用,即使他是 dist 任务的依赖。同时 test 任务的依赖任务 compileTest 也没有被调用,而像 compile 被 test 和其它任务同时依赖的任务仍然会被调用。

失败后继续执行

默认情况下只要有任务调用失败 Gradle 就是中断执行。这可能会使调用过程更快,但那些后面隐藏的错误不会被发现。所以你可以使用–continue 在一次调用中尽可能多的发现所有问题。

采用了–continue 选项,Gralde会调用每一个任务以及它们依赖的任务。而不是一旦出现错误就会中断执行。所有错误信息都会在最后被列出来。

一旦某个任务执行失败,那么所有依赖于该任务的子任务都不会被调用。例如由于 test 任务依赖于 complie 任务,所以如果 compile 调用出错,test 便不会被直接或间接调用。

简化任务名

当你试图调用某个任务的时候,无需输入任务的全名。只需提供足够的可以唯一区分出该任务的字符即可。

  • 上面例子,用 gradle di 来直接调用 dist 任务。
  • 你也可以用驼峰命名的任务中每个单词的首字母进行调用。例如,可以执行 gradle compTest or even gradle cT 来调用 compileTest 任务。

选择构建位置

调用 gradle 时,默认情况下总是会构建当前目录下的文件,可以使用-b 参数选择构建的文件,并且当你使用此参数时settings.gradle 将不会生效

  • 选择文件构建
    subdir/myproject.gradle
task hello << {
    println "using build file '$buildFile.name' in '$buildFile.parentFile.name'."
}

gradle -q -b subdir/myproject.gradle hello 的输出结果:

Output of gradle -q -b subdir/myproject.gradle hello
\> gradle -q -b subdir/myproject.gradle hello
using build file 'myproject.gradle' in 'subdir'.
  • 选择构建目录
    gradle -q -p subdir hello 的输出结果
\> gradle -q -p subdir hello
using build file 'build.gradle' in 'subdir'.

获取构建信息

命令行的基本使用

四、Gradle 使用 Gradle 图形用户界面

图形用户界面

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值