zoukankan      html  css  js  c++  java
  • Gradle之FTP文件下载

    Gradle之FTP文件下载

    1、背景

    项目上需要使用本地web,所以我们直接将web直接放入assets资源文件夹下。但是随着开发进行web包越来越大;所以我们想着从版本库里面去掉web将其忽略掉,使用gradle命令下载web。由于web包是放在局域网服务器上的,服务器支持FTP文件协议,所以使用FTP插件下载文件。

    2、FTP

    FTP官网文档

    Gradle官网文档
    gradle配置

    3、FTP工具

    ftp-util.gradle

    configurations {
        ftpAntTask
    }
    
    dependencies {
        ftpAntTask("org.apache.ant:ant-commons-net:1.9.2") {
            module("commons-net:commons-net:3.3")
        }
    }
    
    void downloadFtpFiles(String serverIp, String userId, String password, String url, localDir) {
        ant.taskdef(
                name: 'ftp',
                classname: 'org.apache.tools.ant.taskdefs.optional.net.FTP',
                classpath: configurations.ftpAntTask.asPath
        )
    
        //指定ftp地址及账户信息
        ant.ftp(action: 'get', server: serverIp, userid: userId, password: password, remotedir: url) {
            // 删除老文件
            delete(new File(localDir))
    
            //指定下载文件位置
            fileset(dir: new File(localDir)) {
                include(name: "**")
            }
        }
    }
    
    //导出函数
    ext {
        downloadFtpFiles = this.&downloadFtpFiles
    }
    

    使用方式:

    导入方式:将ftp-util.gradle文件放在根目录下然后使用一下代码导入groovy代码

    apply from: "${project.rootProject.file('ftp-util.gradle')}"
    

    下载任务

    task downloadFiles() {
        //清理缓存
        dependsOn 'clean'
        def dir = buildDir.getPath() + '/dir'
        doLast {
            //获取最新版本信息
            ant {
                println 'web-ftp:serverIp:' + serverIp
                println 'web-ftp:userId:' + userId
                println 'web-ftp:password:' + password
                println 'web-ftp:dir:' + dir
    
                println 'web-ftp:sync start'
    
                def url = webUrl + '/latest'
    
                //调用FTP插件下载文件函数
                downloadFtpFiles(serverIp, userId, password, url, dir)
                println 'web-ftp:sync end'
            }
        }
    }
    

    调用task方式

    ./gradlew downloadFiles
    
  • 相关阅读:
    如何处理消息堆积
    如何避免消息的重复发送
    内存泄漏和内存溢出的关系
    数据挖掘
    servlet
    数据驱动安全需三大核心新技术
    JS 入门经典 第三章 判断、循环和函数
    JS 高级程序设计 第三章
    JS入门经典
    JS高级程序设计1-2章
  • 原文地址:https://www.cnblogs.com/hejing-michael/p/gradle-zhiftp-wen-jian-xia-zai.html
Copyright © 2011-2022 走看看