在阅读或实践本文中的实例前,必须首先确保已将Gradle插件安装到Eclipse中。本教程的目标:
这是项目完成后的代码结构图:
打开 Eclipse,单击并选择菜单 File->New->Other 并选择 Gradle Project 如下图所示 -
点击下一步(Next)显示集成的一些说明,如下 -
点击下一步(Next),并填入要创建的项目的名称 HelloGradle 并点击完成(Finish),如下 -
在第一次运行时,Eclipse将下载Gradle相关软件或要求指定本机安装 Gradle 的路径。请参考Eclipse+Gradle集成教程。
默认情况下,Gradle软件将通过Eclipse下载到 C:/Users/{username}/.gradle。不过可以配置更改到其它位置,此配置在本指南的最后一个附录中有说明。
Gradle将自动创建项目所需要的文件结构。其结构类似于Maven项目。
注意,这是一个 gradle 项目定义的项目结构,怎么样,是不是有点熟悉?
build.gradle文件是配置项目中要使用的库的文件。它和Maven工程中的pom.xml相同。
打开build.gradle文件配置将要使用的库,默认生成的代码内容如下:
/* * This build file was auto generated by running the Gradle 'init' task * by 'Administrator' at '16-10-30 下午4:20' with Gradle 3.1 * * 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.1/userguide/tutorial_java_projects.html */ // Apply the java plugin to add support for Java apply plugin: 'java' // 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() } // In this section you declare the dependencies for your production and test code dependencies { // The production code uses the SLF4J logging API at compile time compile 'org.slf4j:slf4j-api:1.7.21' // Declare the dependency for your favourite test framework you want to use in your tests. // TestNG is also supported by the Gradle Test task. Just change the // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add // 'test.useTestNG()' to your build script. testCompile 'junit:junit:4.12' }
向上面的源代码文件中添加以下代码 -
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
添加后如下 -
注意:如果build.gradle文件更新,Eclipse目前不会自动更新类路径。要在项目上或在build.gradle文件中点击右键选择 Gradle/Refresh Gradle 来更新项目。
如果将新的库添加到 build.gradle 中声明,Gradle 将会把它们下载到本地计算机上。
接下来,创建一些 Java 类来测试使用下载的类库,这里创建一个简单的 CheckNumeric.java 类,如下所示 -
CheckNumeric.java 类的代码如下所示 -
package com.yiibai.hellogradle; import org.apache.commons.lang3.StringUtils; public class CheckNumeric { public static void main(String[] args) { String text1 = "a12340"; String text2 = "1234"; boolean result1 = StringUtils.isNumeric(text1); boolean result2 = StringUtils.isNumeric(text2); System.out.println(text1 + " is a numeric? " + result1); System.out.println(text2 + " is a numeric? " + result2); } }
运行 CheckNumeric 类应该会得到以下结果:
可以在项目中清楚地看到使用的库,它在硬盘上的位置如下图所示 -
上面的步骤中我们已经创建了项目,并且成功地运行了。该项目使用的StringUtils类,它是一个Apache类,而不是在JDK的标准库中的类。传统上做法,必须将这个类库复制到项目并声明类路径。但是,这里并不需要像传统方式那样复制和声明类路径。这些类库可以让Gradle来管理。 现在来看看Gradle是如何工作的,如下图中所示 -
上图显示了Gradle工作的整个过程,下面我们一步步来说明。
所以只需要在build.gradle文件中声明所有想要使用的库,这些库由Gradle自己管理。
你会不会有这样的一个问题:本地存储库在我电脑的什么位置?如果是按上面所有套路来创建工程,那么看下图就知道了 -
而上面配置中使用到的 commons-lang3 库的路径在C:/Users/Administrator/.gradle/caches/modules-2/files-2.1/org.apache.commons,如下图所示 -
默认情况下,Gradle软件将通过 Eclipse 下载到C:/Users/{username}/.gradle目录中。但是可以将配置更改其位置。例如想要把这个下载目录修改为 D:/worksp/gradle/Downloads,那么可以按照以下操作来配置。
在 Eclipse 菜单中,打开 Window -> References 选择目录 D:/worksp/gradle/Downloads,如下图所示 -
右键单击项目,然后选择 Gradle -> Refresh Gradle Project,Gradle将重新下载到刚刚设置的新文件夹。如下图所示 -
问题:在哪里查找信息groupId,artifactId和版本呢?
可以去网站: http://mvnrepository.com ,例如在我们上面示例使用的 common-lang3 ,可在网站中搜索找到打开URL:http://mvnrepository.com/artifact/org.apache.commons/commons-lang3
如下图中所示 -
可根据你想要的一个版本,找到 gradle 的相关信息,如下所示 -