本文介紹了Gradle腳本常用功能實(shí)現(xiàn),包括任務(wù)的定義、添加依賴、指定任務(wù)執(zhí)行順序、復(fù)制文件、復(fù)制并重命名文件、從FTP下載文件、解壓Zip文件、操作數(shù)據(jù)庫文件等功能。
本文首發(fā):http://yuweiguocn.github.io/
《望廬山瀑布》
日照香爐生紫煙,遙看瀑布掛前川。
飛流直下三千尺,疑是銀河落九天。
—唐,李白
定義任務(wù)
task myTask {
doFirst {
println "my task is execuated"
}
}
添加依賴
task myFirstTask {
doFirst {
println "my first task is execuated"
}
}
task mySecondTask {
doFirst {
println "my second task is execuated"
}
}
task myTask(dependsOn: [myFirstTask,mySecondTask]) {
doFirst {
println "my task is execuated"
}
}
添加依賴2
task myFirstTask {
doFirst {
println "my first task is execuated"
}
}
task mySecondTask {
doFirst {
println "my second task is execuated"
}
}
task myTask {
doFirst {
println "my task is execuated"
}
}
myTask.dependsOn myFirstTask
myTask.dependsOn mySecondTask
添加依賴3
當(dāng)任務(wù)為動(dòng)態(tài)創(chuàng)建時(shí),我們無法使用上面的方法添加依賴,指定執(zhí)行順序,可以使用下面的方法進(jìn)行處理:
tasks.whenTaskAdded { task ->
if (task.name == "tinkerPatchRelease") {
myTask.dependsOn task
task.mustRunAfter myFirstTask
}
}
指定任務(wù)執(zhí)行順序
task myFirstTask {
doFirst {
println "my first task is execuated"
}
}
task mySecondTask {
doFirst {
println "my second task is execuated"
}
}
task myTask(dependsOn: [myFirstTask,mySecondTask]) {
doFirst {
println "my task is execuated"
}
}
mySecondTask.mustRunAfter myFirstTask
執(zhí)行命令:
./gradlew myTask
執(zhí)行結(jié)果:
:app:myFirstTask
my first task is execuated
:app:mySecondTask
my second task is execuated
:app:myTask
my task is execuated
復(fù)制文件
task copyTask(type: Copy){
from FilePath
into DistDirPath
}
復(fù)制并重命名文件
task copyTask(type: Copy) {
from FilePath
into DistDirPath
rename { String fileName ->
newName
}
}
從FTP下載文件
def ftp_url = "*******"
def ftp_username = "*******"
def ftp_password = "*******"
def ftp_local_dir = "/***/***"
def ftp_dir = "/***/***/***/***"
configurations {
ftpAntTask
}
dependencies {
ftpAntTask("org.apache.ant:ant-commons-net:1.8.4") {
module("commons-net:commons-net:1.4.1") {
dependencies "oro:oro:2.0.8:jar"
}
}
}
task downLoadBaseApk {
doLast {
ant.taskdef(name: 'ftp',
classname: 'org.apache.tools.ant.taskdefs.optional.net.FTP',
classpath: configurations.ftpAntTask.asPath)
ant.ftp(server: ftp_url, userid: ftp_username, password: ftp_password, remoteDir: ftp_dir, verbose: "yes",passive: "yes",action: "get") {
fileset(dir: ftp_local_dir)
}
}
}
解壓Zip文件
task unzipHC(type: Copy) {
from zipTree(ZipFilePath)
into DistDirPath
}
操作數(shù)據(jù)庫文件
我們可以使用ATTACH語句附加額外的數(shù)據(jù)庫到當(dāng)前數(shù)據(jù)庫,使用REPLACE語句復(fù)制表記錄到當(dāng)前數(shù)據(jù)庫表中,如果出現(xiàn)重復(fù)記錄會(huì)被替換,而不是報(bào)錯(cuò)。如果使用INSERT INTO語句出現(xiàn)重復(fù)記錄時(shí)會(huì)報(bào)sql異常。
configurations {
sqllite
}
repositories {
mavenCentral()
}
dependencies {
sqllite 'org.xerial:sqlite-jdbc:3.8.9.1'
}
URLClassLoader loader = GroovyObject.class.classLoader
configurations.sqllite.each { File file ->
loader.addURL(file.toURL())
}
task exeSql {
doLast {
Sql sql = Sql.newInstance("jdbc:sqlite:app/src/main/res/raw/data.db", "org.sqlite.JDBC")
sql.eachRow("SELECT count(*) FROM MyTable") { row ->
println "-->MyTable count is " + row.toString()
}
sql.execute("DELETE FROM MyTable")
sql.withTransaction {
sql.execute("DELETE FROM MyTable")
}
def attach_db = "mydb"
//附加數(shù)據(jù)庫
sql.execute("ATTACH DATABASE ? AS "+attach_db,[dbPath])
//從附加的數(shù)據(jù)庫中的表中復(fù)制記錄到當(dāng)前數(shù)據(jù)庫的表中
sql.execute("REPLACE INTO MyTable SELECT * FROM "+attach_db+".MyTable")
sql.execute("DETACH DATABASE ?",[attach_db])
sql.close()
}
}