Gradle脚本常用功能总结

本文介绍了Gradle脚本常用功能实现,包括任务的定义、添加依赖、指定任务执行顺序、复制文件、复制并重命名文件、从FTP下载文件、解压Zip文件、操作数据库文件等功能。

本文首发:http://yuweiguocn.github.io/

《望庐山瀑布》
日照香炉生紫烟,遥看瀑布挂前川。
飞流直下三千尺,疑是银河落九天。
—唐,李白

定义任务

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

当任务为动态创建时,我们无法使用上面的方法添加依赖,指定执行顺序,可以使用下面的方法进行处理:

tasks.whenTaskAdded { task ->
    if (task.name == "tinkerPatchRelease") {
        myTask.dependsOn task
        task.mustRunAfter myFirstTask
    }
}

指定任务执行顺序

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

执行命令:

./gradlew myTask

执行结果:

:app:myFirstTask
my first task is execuated
:app:mySecondTask
my second task is execuated
:app:myTask
my task is execuated

复制文件

task copyTask(type: Copy){
    from FilePath
    into DistDirPath
}

复制并重命名文件

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
}

操作数据库文件

我们可以使用ATTACH语句附加额外的数据库到当前数据库,使用REPLACE语句复制表记录到当前数据库表中,如果出现重复记录会被替换,而不是报错。如果使用INSERT INTO语句出现重复记录时会报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"
        //附加数据库
        sql.execute("ATTACH DATABASE ? AS "+attach_db,[dbPath])
        //从附加的数据库中的表中复制记录到当前数据库的表中
        sql.execute("REPLACE INTO MyTable  SELECT * FROM "+attach_db+".MyTable")
        sql.execute("DETACH DATABASE ?",[attach_db])
        
        sql.close()
        
    }
}

你可能感兴趣的:(Gradle脚本常用功能总结)