gradle - 使用介绍(一)

来源:互联网 发布:mac os怎么分区 编辑:程序博客网 时间:2024/05/21 11:11
Gradle 使用介绍
1 什么是Gradle
官网www.gradle.org上介绍Gradle是升级版(evolved)的自动化构建工具。它可以自动构建,测试,发布,部署,同时使更多的软件包或其他类型诸如生成静态网站,文档等项目自动化。
Gradle 将Ant的功能和伸缩性与Maven的依赖管理及约定结合成一种更加高效的方式去完成构建。它采用了Groovy 特定领域语言和诸多创新方法,提供了一种声明式的方式用合理的默认值来描述所有类型的构建工作。Gradle正迅速成为许多开源项目和前沿企业构建系统的选择,同时也在挑战遗留的自动化构建项目。

2 开始使用Gradle
自动化构建一个Java项目
平台:Ubuntu linux
标准的Gradle构建项目的目录格式如下:

首先编写HelloWorld.java程序
> mkdir -p src/main/java/org/gradle/example/simple> cd src/main/java/org/gradle/example/simple> touch HelloWorld.java
程序如下:
package org.gradle.example.simple;public class HelloWorld {public static String getResult() {return “hello, world";}}

回到和src同级的目录
> touch build.gradle
编辑build.gradle
apply plugin: 'java'
只有这么一句。

运行gradle命令
> gradle build
build.gradle 是默认执行的gradle文件。


其中冒号及其后动宾短语就是任务路径(task path)。这些动宾短语就是一个个的任务。


很神奇的是build.gradle中只有一句代码,就完成了这么多的task,那么这些任务从何而来呢?
Java plugin添加了一组tasks到我们的工程当中。


上面展示的目录结构中build/test-results和build/reports 必须得添加test代码才会生成。

在src/test/java/org/gradle/example/simple下新建HelloWorldTest.java
代码如下
package org.gradle.example.simple;import org.junit.Test;import static org.junit.Assert.assertEquals;public class HelloWorldTest {@Testpublic void testHelloWorld() throws Exception {assertEquals("Hello, World", HelloWorld.result());}}

回到和src同级的目录
编辑build.gradle 如下:
apply plugin: 'java'apply plugin: 'maven'version = '0.0.1'repositories {mavenCentral()}dependencies {testCompile 'junit:junit:4.11'}

其中添加了maven插件,
'junit:junit:4.11' 就是 group: 'junit', name: 'junit', version: '4.11'

Dependencies{} 是一个configuration block。它会在所有的Task执行之前运行,其作用就是准备好要用的变量和数据结构。

1)dependencies用于声明一组依赖,这些依赖会被组合为configuration;

2)configuration代表了一组构件及其依赖。

dependencies的语法结构如下:

dependencies {
configurationName dependencyNotation1, dependencyNotation2, ...
}

testCompile 就是configuration, 'junit:junit:4.11'则是dependencyNotation

java plugin添加了大量的dependecy configuration到我们的工程中。

Name

Extends

Used by tasks

Meaning

compile

-

compileJava

Compile time dependencies

runtime

compile

-

Runtime dependencies

testCompile

compile

compileTestJava

Additional dependencies for compiling tests.

testRuntime

runtime,testCompile

test

Additional dependencies for running tests only.

archives

-

uploadArchives

Artifacts(e.g.jars) produced by this project.

default

runtime

-

The default configuration used by a project dependency on this project. Contains the artifacts and dependencies required by this project at runtime.

compile
The dependencies required to compile the production source of the project.
runtime

The dependencies required by the production classes at runtime. By default, also includes the compile time dependencies.
testCompile

The dependencies required to compile the test source of the project. By default, also includes the compiled production classes and the compile time dependencies.
testRuntime
The dependencies required to run the tests. By default, also includes the compile, runtime and test compile dependencies.

之后我们运行

> gradle build

查看生成build/下的目录

> gradle test

会发现少执行了一部分的task,这说明build依赖test来执行构建任务的。

原创粉丝点击