我的 Gradle 使用实践(一)

上面已经成功执行了一个使用 Gradle 的 Java 项目,下面大概介绍一下原理,官方也给出了详细的 Getting Started

一. build.gradle - plugin

为什么 init 后能执行一些新的任务,这是由 build.gradle 文件决定的,我们打开这个文件可以看到:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
* This build file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* user guide available at https://docs.gradle.org/3.4.1/userguide/tutorial_java_projects.html
*/

// Apply the java plugin to add support for Java
apply plugin: 'java'

// Apply the application plugin to add support for building an application
apply plugin: 'application'

// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}

dependencies {
// This dependency is found on compile classpath of this component and consumers.
compile 'com.google.guava:guava:20.0'

// Use JUnit test framework
testCompile 'junit:junit:4.12'
}

// Define the main class for the application
mainClassName = 'App'

里面注释已经给得挺详细,之所以能够构建 java 项目,是因为 apply plugin: 'java' 这句话,也就是应用 Gradle 提供的 Java 插件,这个插件能提供构建 Java 项目的任务,如 compileJava, jar, build 等,想详细了解可查看 官方文档

同样的,apply plugin: application 是应用 Gradle 给出的运行 Java 应用的插件(官方文档),它提供了 run 任务,我们必须主动声明 Java 应用的主类,可以看到 build.gradle 里最后一句代码 mainClassName = 'App',是在声明这个 Java Application 的主类是 ./src/main/java/ 目录下的 App 类,我们修改 App.java 里的内容:

1
2
3
4
5
6
7
8
9
public class App {
public String getGreeting() {
return "Hello world, hello Gradle!";
}

public static void main(String[] args) {
System.out.println(new App().getGreeting());
}
}

再执行 gradle run 可以看到:

我们可以去编写我们的项目,然后 gradle run 就能运行了,正是 Gradle plugins 提供了这些功能,关于 plugin 的详细介绍可查阅 官方文档

二. build.gradle - repositories

1
2
3
4
5
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}

这里指定 Gradle 下载依赖的仓库,兼容 Maven 仓库,以上代码是使用jcenter仓库作为依赖下载仓库,常见的仓库有:

  • The Central Repository (Maven 中央仓库)
    repositories块中加入mavenCentral()可使用此仓库,此仓库国内下载较慢
  • Jcenter (JFrog 公司提供的仓库)
    repositories块中加入jcenter()可使用此仓库,新版 Gralde 和 Android Studio 默认仓库,国内下载比中央仓库快。
  • 自定义仓库
    Gradle 兼容 Maven 仓库和 Ivy 仓库,如在repositories块中加入一下内容就添加了一个自定义 url的 Maven 仓库:
1
2
3
maven {
url "http://repo.mycompany.com/maven2"
}

url也可以是本地路径。

关于 repositories 的更多内容可查阅 官方文档

三. build.gradle - dependencies

这就是 Gradle 项目管理依赖的地方,非常方便,添加某个依赖只需要在 dependencies 块中加入一行代码:

1
compile 'group:name:version'

build.gradle 中默认添加了 guava 作为依赖和 junit 作为测试时(执行 gradle test 任务时)的依赖

1
2
compile 'com.google.guava:guava:20.0'
testCompile 'junit:junit:4.12'

作为实践,我们现在要在项目中输出一个 Json 数组,需要 http://json.org 提供的 json.jar 依赖,我们直接去 Jcenter搜索依赖,如下:

点击搜索出的结果即进入 详细界面,可以看到其给出的 Gradle 依赖语句:

1
compile 'org.json:json:20160810'

我们把这个加到 dependencies 块中,就是添加了 json 依赖,不用我们自己再去官网下载 jar 文件加入 lib
编辑 App.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import org.json.JSONArray;
import org.json.JSONObject;

public class App {
public String getGreeting() {
return "Hello world, hello Gradle!";
}

public static void main(String[] args) {
System.out.println(new App().getGreeting());

JSONArray ja = new JSONArray();
for (int i = 0; i < 6; ++i){
JSONObject jo = new JSONObject();
jo.put("hello gradle!",i);
ja.put(jo);
}
System.out.println(ja.toString());
}
}

然后执行 gradle run 任务:

第一次执行 gradle 会去下载依赖缓存到本地,以后的项目用到相同的依赖就不用再下载了。
关于 Gradle 依赖管理的详细内容可查看 官方文档