Chapter 5. The Gradle Wrapper 關於gradle wrapper

来源:http://www.cnblogs.com/muqi9527/archive/2016/05/26/5523324.html
-Advertisement-
Play Games

Most tools require installation on your computer before you can use them. If the installation is easy, you may think that’s fine. But it can be an unn ...


Most tools require installation on your computer before you can use them. If the installation is easy, you may think that’s fine. But it can be an unnecessary burden on the users of the build. Equally importantly, will the user install the right version of the tool for the build? What if they’re building an old version of the software?

//大多數工具需要在使用前安裝,如果安裝容易還好,但是如果因為安裝的麻煩而惹惱用戶就不太好了。而同樣重要的是用戶是否安裝的正確的版本,假如用戶正在使用舊版本軟體構建呢?

The Gradle Wrapper (henceforth referred to as the “Wrapper”) solves both these problems and is the preferred way of starting a Gradle build.

//Gradle Wrapper ,以後簡稱為Wrapper,解決了這兩個問題,這也是gradle 構建的優選方法

5.1. Executing a build with the Wrapper

//使用wrapper執行構建

If a Gradle project has set up the Wrapper (and we recommend all projects do so), you can execute the build using one of the following commands from the root of the project:

//如果gradle項目已經安裝了wrapper(我們建議所有的項目都這麼做),你可以在項目的根目錄使用下麵命令中的任何一個執行構建

  • ./gradlew <task> (on Unix-like platforms such as Linux and Mac OS X) //mac系統上

  • gradlew <task> (on Windows using the gradlew.bat batch file) //windows系統上

Each Wrapper is tied to a specific version of Gradle, so when you first run one of the commands above for a given Gradle version, it will download the corresponding Gradle distribution and use it to execute the build.

//每一個wrapper綁定到一個指定的gradle版本上,所以當你第一次對於給定的gradle版本運行上面命令中的一個時,它將會下載對應gradle發佈包,並使用它執行構建

IDEs

When importing a Gradle project via its wrapper, your IDE may ask to use the Gradle 'all' distribution. This is perfectly fine and helps the IDE provide code completion for the build files.

Not only does this mean that you don’t have to manually install Gradle yourself, but you are also sure to use the version of Gradle that the build is designed for. This makes your historical builds more reliable. Just use the appropriate syntax from above whenever you see a command line starting with gradle ... in the user guide, on Stack Overflow, in articles or wherever.

For completeness sake, and to ensure you don’t delete any important files, here are the files and directories in a Gradle project that make up the Wrapper:

//這是一些生成在gradle項目中的重要文件,這些文件用來組建wrapper,不要刪除

  • gradlew (Unix Shell script)

  • gradlew.bat (Windows batch file)

  • gradle/wrapper/gradle-wrapper.jar (Wrapper JAR)

  • gradle/wrapper/gradle-wrapper.properties (Wrapper properties)

 

If you’re wondering where the Gradle distributions are stored, you’ll find them in your user home directory under $USER_HOME/.gradle/wrapper/dists.

//gradle distribution包存儲在$USER_HOME/.gradle/wrapper/dists

5.2. Adding the Wrapper to a project

//給項目添加wrapper

The Wrapper is something you should check into version control. By distributing the Wrapper with your project, anyone can work with it without needing to install

//wrapper是在添加到版本庫中要檢查的內容,把wrapper發佈到項目中,任何人可以使用wrapper而不需要安裝gradle

Gradle beforehand. Even better, users of the build are guaranteed to use the version of Gradle that the build was designed to work with. Of course, this is also great forcontinuous integration servers (i.e. servers that regularly build your project) as it requires no configuration on the server.

//更好的是,構建的用戶可以保證使用到的就是gradle構建被設計的目標版本,當然對於持續集成伺服器來說這也很棒。

You install the Wrapper into your project by running the wrapper task. (This task is always available, even if you don't add it to your build). To specify a Gradle version

//通過運行wrapper task來安裝wrapper,這個任務總是可獲得的,儘管你沒有把它添加到構建步驟中。

use--gradle-version on the command-line. You can also set the URL to download Gradle from directly via --gradle-distribution-url. If no version or distribution

//用--gradle--version來指定gradle版本,你也可以通過--gradle-distribution-url選項設置直接下載gradle的url

URL is specified, the Wrapper will be configured to use the gradle version the wrapper task is executed with. So if you run the wrapper task with Gradle 2.4, then the

//如果沒有指定wrapper的版本或者url,wrapper會下載執行wrapper任務的gradle的版本,所以,如果你使用gradle 2.4運行wrapper任務,wrapper的預設配置就是2.4

Wrapper configuration will default to version 2.4.

Example 5.1. Running the Wrapper task

Output of gradle wrapper --gradle-version 2.0

> gradle wrapper --gradle-version 2.0
:wrapper

BUILD SUCCESSFUL

Total time: 1 secs

The Wrapper can be further customized by adding and configuring a Wrapper task in your build script, and then executing it.

//wrapper可以在build腳本中添加wrapper 任務來深度定製化,然後執行它。

Example 5.2. Wrapper task

build.gradle

task wrapper(type: Wrapper) {
    gradleVersion = '2.0'
}

After such an execution you find the following new or updated files in your project directory (in case the default configuration of the Wrapper task is used).

//在執行完上面的腳本,你會發現在項目中生成了下麵這些新的或者修改的文件(以防使用預設配置)

Example 5.3. Wrapper generated files

Build layout

simple/
  gradlew
  gradlew.bat
  gradle/wrapper/
    gradle-wrapper.jar
    gradle-wrapper.properties

All of these files should be submitted to your version control system. This only needs to be done once. After these files have been added to the project, the project

//所有的這些文件將會提交到版本庫中。所以這個任務只需要執行一次。當這些文件添加到項目中後,

should then be built with the added gradlew command. The gradlew command can be used exactly the same way as the gradle command.

//項目之後會使用gradlew命令來構建,gradlew命令用法可gradle一樣

If you want to switch to a new version of Gradle you don't need to rerun the wrapper task. It is good enough to change the respective entry in thegradle-wrapper.properties file, but if you want to take advantage of new functionality in the Gradle wrapper, then you would need to regenerate the wrapper files.

//如果你想切換到gradle的新版本,只需要修改gradle-wrapper.properties 文件

5.3. Configuration

If you run Gradle with gradlew, the Wrapper checks if a Gradle distribution for the Wrapper is available. If so, it delegates to the gradle command of this distribution with all the arguments passed originally to the gradlew command. If it didn't find a Gradle distribution, it will download it first.

When you configure the Wrapper task, you can specify the Gradle version you wish to use. The gradlew command will download the appropriate distribution from the Gradle repository. Alternatively, you can specify the download URL of the Gradle distribution. The gradlew command will use this URL to download the distribution. If you specified neither a Gradle version nor download URL, the gradlew command will download whichever version of Gradle was used to generate the Wrapper files.

For the details on how to configure the Wrapper, see the Wrapper class in the API documentation.

If you don't want any download to happen when your project is built via gradlew, simply add the Gradle distribution zip to your version control at the location specified by your Wrapper configuration. A relative URL is supported - you can specify a distribution file relative to the location of gradle-wrapper.properties file.

If you build via the Wrapper, any existing Gradle distribution installed on the machine is ignored.

5.4. Verification of downloaded Gradle distributions

The Gradle Wrapper allows for verification of the downloaded Gradle distribution via SHA-256 hash sum comparison. This increases security against targeted attacks by preventing a man-in-the-middle attacker from tampering with the downloaded Gradle distribution.

To enable this feature you'll want to first calculate the SHA-256 hash of a known Gradle distribution. You can generate a SHA-256 hash from Linux and OSX or Windows (via Cygwin) with the shasum command.

Example 5.4. Generating a SHA-256 hash

> shasum -a 256 gradle-2.4-all.zip
371cb9fbebbe9880d147f59bab36d61eee122854ef8c9ee1ecf12b82368bcf10  gradle-2.4-all.zip

Add the returned hash sum to the gradle-wrapper.properties using the distributionSha256Sum property.

Example 5.5. Configuring SHA-256 checksum verification

gradle-wrapper.properties

distributionSha256Sum=371cb9fbebbe9880d147f59bab36d61eee122854ef8c9ee1ecf12b82368bcf10

5.5. Unix file permissions

The Wrapper task adds appropriate file permissions to allow the execution of the gradlew *NIX command. Subversion preserves this file permission. We are not sure how other version control systems deal with this. What should always work is to execute “sh gradlew”.


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 獲取SQL執行計劃的常見幾種方法 一、獲取庫緩衝區中的執行計劃 1. 查詢v$sql動態性能視圖,找到要查詢的SQL語句的sql_id。 2. 調用dbms_xplan包的display_cursor方法,查看該語句執行時的執行計劃。 例如: SELECT dname FROM emp, dept ...
  • 利用PL/SQL可以進行模塊化程式設計。 在一個PL/SQL塊中,可以定義若幹個子程式。 把一些功能相對獨立、需要經常執行的代碼定義為一個子程式,在需要時根據子程式的名字進行調用。這樣不僅便於程式設計和編碼,而且利於程式的調試。PL/SQL有兩種形式的子程式,即過程和函數。 在子程式中也可以定義變數 ...
  • 談完並行執行的原理,咱們再來談談優化,到底並行執行能給我們帶來哪些好處,我們又應該註意什麼呢,下麵展開. Amdahl’s Law 再談並行優化前我想有必要談談阿姆達爾定律,可惜老爺子去年已經駕鶴先去了. 其中P:可以並行的百分比 N:演算法並行計算使用的”CPU” 這裡我們舉個簡單的例子,我們來做一 ...
  • 編寫PL/SQL塊的主要目的是對資料庫進行訪問,因此,在PL/SQL塊中可以包含SELECT語句、DML語句,還可以包含DCL語句。需要註意的是,在PL/SQL塊中不能直接包含DDL語句,如果要利用PL/SQL塊完成諸如創建表、修改表結構等操作,需要通過其他方法。通過SQL語句以及流控制語句,可以編 ...
  • 1.下載 mysql-mysql-5.1.55-win32.zip 2. 解壓縮到任何一個目錄,最好目錄名稱不要有空格; 例如:C:\mysql 3. 刪除Embedded,include,lib,mysql-test 這四個目錄。 註:這四個目錄占了300多M,刪除後,只有不到100M。 3,在c... ...
  • 1 NSURLConnettion NSURLConnettion是 Core Foundation/CFNetwork框架 API 之上的一個抽象. NSURLConnettion是用來指代 Foundation 框架中的一系列組件: NSURLRequest,NSURLResponse,NSUR ...
  • 1.效果圖 2.源代碼 MainActivity.java public class MainActivity extends AppCompatActivity { public enum ChartType { LINE_CHART, COLUMN_CHART, PIE_CHART, BUBBL... ...
  • BaseControl按鈕合集 效果 源碼 https://github.com/YouXianMing/Animations 說明 本人一共封裝了3種按鈕的基類控制項,以上是一個示例演示,演示如何通過繼承來實現想要的效果. ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...