我的Gradle使用实践(一)

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

一. build.gradle – plugin

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

/*
 * 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里的内容:

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使用实践(一)》

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

二. build.gradle – repositories

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仓库:
maven {
        url "http://repo.mycompany.com/maven2"
    }

url也可以是本地路径

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

三. build.gradle – dependencies

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

compile 'group:name:version'

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

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

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

《我的Gradle使用实践(一)》

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

compile 'org.json:json:20160810'

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

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会去下载依赖缓存到本地,以后的项目用到相同的依赖就不用再下载了。

关于Gradle依赖管理的详细内容可查看
官方文档

    原文作者:csJd
    原文地址: https://www.jianshu.com/p/77c0fffd31b3
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞