2015年1月18日

Gradle(2)–– 專案與任務



前言

在Gradle中,專案和任務是重要基本慨念,專案(projects)可以說是一個目的或是專案,例如: 開發一個函式庫、網頁應用程式或是複製某個檔案到目錄中等等。任務(task)可以是專案的最小單位元,也就是專案可以由很多任務所完成,任務區塊中有很多動作(action)所組成,這些動作可以自行定義。

Project 屬性


屬性 說明
name 唯讀,需要透settings.gradle設定
parent 唯讀
version 專案版本
description 專案說明

任務


(1)基本

我們由上面可以知道任務是構成專案的主要元素,我們開始示範一個簡單的範例,來做解說,在gradle專案裡面預設專案檔名為build.gradle,寫完建構腳本後,需要執行建構指令,在執行指令時,只要輸入first任務就會執行腳本內定義的任務並執行裡面的動作。如果當你儲存成不同檔名需要加上 --build-file的參數來指定檔名。

預設執行檔名: build.gradle

project.description = 'first project'
task first << {
    println 'Running first task for project ' + project.description
}

程式說明 : 屬性為 description是用來說明這個專案的註解,print函式是顯示console的訊息
image


這些動作是比較常用到的方法:
動作 說明
doFirst 會被先執行
doLast 會最後直行
<< 附加元素
defaultTasks
範例一、doLast和doFirst範例
// defaultTasks用來定義將會被執行的任務,只有first區塊會被執行
// 會先執行doFirst,在執行doLast

defaultTasks "first"
version= "1.0"
description = " test "
task first {
    doLast{
        print "1 last execute "
    }
    doFirst{
        print "1 first execute "
    }
   
}
task secound {
    doLast{
        print "2 last execute "
    }
   
    doFirst{
        print "2 first execute "
    }
   
}

結果: 1 first execute 1 last execute



(2) 任務的寫法差異

project.description = 'first project'
task first  {
    doFirst{
        println 'Running first task for project '
    }
}
task second  {
    doLast { Task task ->
        println 'test '+${task.name} +' for project'
    }
}
task third << {
    th_task ->
        println 'test'+th_task.name  +' for project'
   
}
任務first的寫法是可以定義動作(action)內容,使用doFirst為動作串列的開始資料。
任務second把宣告task物件當做方法來使用
任務third會使用"<<"符號,首先說明符號意義,它會將動作區塊附加到third任務的後面。

(3)任務順序
當任務有相關係時,例如Task1 需要先執行,才能做Task2
Task1 –> Task2

檔名 : dependTask.gradle
說明: 在Gradle語言中可以使用dependsOn方法定義任務間的相依,Gradle是使用lazy的載入方式,也就是我們可以把相依的任務定義在後面。
image
範例二、第一個任務需要先執行第二個才會被執行,利用dependsOn
task first {
    doLast{
        print "1 last execute "
    }
    doFirst{
        print "1 first execute "
    }
}
task secound {
    doLast{
        print "2 last execute "
    }
    doFirst{
        print "2 first execute "
    }
}

first.dependsOn secound

結果: :secound
2 first execute 2 last execute :first
1 first execute 1 last execut
範例三、
task first {
    doLast{
        print "task 1 last execute "
    }
   
    doFirst{
        print "task 1 first execute "
    }
   
}
task secound {
    doLast{
        print "task 2 last execute "
    }
   
    doFirst{
        print "task 2 first execute "
    }
   
}
task third {
    doLast{
        print "task 3 last execute "
    }
}
task tool {
    println "loading a tool! "
}
first.dependsOn secound
secound.dependsOn third
third.mustRunAfter tool
結果
loading a tool!
:third
task 3 last execute :secound
task 2 first execute task 2 last execute :first
task 1 first execute task 1 last execute


(4) 設定預設任務(setting default task)
我們可以預設腳本執行的任務順序,方便我們規劃執行的方式

defaultTasks 'first', 'second'
task first {
    doLast{
        print "It is 1 "
    }
}
task second {
    print "It is 2 "
}
說明 : defaultTasks 是用來使用預設任務的執行。
image

沒有留言:

張貼留言