轉自:https://blog.csdn.net/qitongce/article/details/72466318?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
1-Gradle入門介紹
在閱讀或實踐本文中的實例前,必須首先確保已將Gradle
插件安裝到Eclipse
中。如果沒有,可以點擊下面的鏈接查看Gradle
安裝說明:
- http://www.yiibai.com/gradle/how-install-gradle-windows.html
本教程的目標:
這是項目完成后的代碼結構圖:
2-創建Gradle項目
打開 Eclipse,單擊並選擇菜單 File
->New
->Other
並選擇 Gradle Project
如下圖所示 -
點擊下一步(Next)顯示集成的一些說明,如下 -
點擊下一步(Next),並填入要創建的項目的名稱 HelloGradle
並點擊完成(Finish),如下 -
在第一次運行時,Eclipse
將下載Gradle
相關軟件或要求指定本機安裝 Gradle
的路徑。請參考Eclipse+Gradle集成教程。
默認情況下,
Gradle
軟件將通過Eclipse
下載到C:/Users/{username}/.gradle
。不過可以配置更改到其它位置,此配置在本指南的最后一個附錄中有說明。
Gradle
將自動創建項目所需要的文件結構。其結構類似於Maven項目。
注意,這是一個 gradle
項目定義的項目結構,怎么樣,是不是有點熟悉?
- src/main/java
- 文件夾包含所有java源文件。
- src/test/java
- 文件夾包含所有java測試用例。
- build.gradle
- 文件包含項目構建所使用的腳本。
- settings.gradle
- 文件將包含必要的一些設置,例如,任務或項目之間的依懶關系等。
3- 配置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'
-
}
- 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
向上面的源代碼文件中添加以下代碼 -
-
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
-
-
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
- 1
- 2
- 3
添加后如下 -
注意:如果
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);
-
-
}
-
-
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
運行 CheckNumeric
類應該會得到以下結果:
可以在項目中清楚地看到使用的庫,它在硬盤上的位置如下圖所示 -
4-解釋Gradle的操作原理
上面的步驟中我們已經創建了項目,並且成功地運行了。該項目使用的StringUtils
類,它是一個Apache類,而不是在JDK的標准庫中的類。傳統上做法,必須將這個類庫復制到項目並聲明類路徑。但是,這里並不需要像傳統方式那樣復制和聲明類路徑。這些類庫可以讓Gradle
來管理。 現在來看看Gradle
是如何工作的,如下圖中所示 -
上圖顯示了Gradle工作的整個過程,下面我們一步步來說明。
- 在build.gradle
中聲明了該項目依賴於common-lang3
庫版本3.0
。
- 當使用Gradle工具刷新項目時,Gradle將檢查指定的依賴庫是否在計算機上有本地存儲庫。 如果沒有,Gradle將從互聯網的存儲庫中下載到本地。
- 最后,Gradle將自動聲明Classpath
。
所以只需要在build.gradle
文件中聲明所有想要使用的庫,這些庫由Gradle
自己管理。
5-查看本地存儲庫
你會不會有這樣的一個問題:本地存儲庫在我電腦的什么位置?如果是按上面所有套路來創建工程,那么看下圖就知道了 -
而上面配置中使用到的 commons-lang3
庫的路徑在 C:\Users\Administrator\.gradle\caches\modules-2\files-2.1\org.apache.commons
,如下圖所示 -
6- Gradle位置配置
默認情況下,Gradle
軟件將通過 Eclipse
下載到C:/Users/{username}/.gradle
目錄中。但是可以將配置更改其位置。例如想要把這個下載目錄修改為 D:\worksp\gradle\Downloads
,那么可以按照以下操作來配置。
在 Eclipse 菜單中,打開 Window
-> References
選擇目錄 D:\worksp\gradle\Downloads
,如下圖所示 -
右鍵單擊項目,然后選擇 Gradle
-> Refresh Gradle Project
,Gradle
將重新下載到剛剛設置的新文件夾。如下圖所示 -
7-在網絡查看Gradle存儲庫
問題:在哪里查找信息groupId,artifactId和版本呢?
可以去網站: http://mvnrepository.com ,例如在我們上面示例使用的 common-lang3 ,可在網站中搜索找到打開URL:http://mvnrepository.com/artifact/org.apache.commons/commons-lang3
如下圖中所示 -
可根據你想要的一個版本,找到 gradle 的相關信息,如下所示 -