美文网首页
Gradle下载远程服务器上的文件

Gradle下载远程服务器上的文件

作者: 想看烟花么 | 来源:发表于2022-07-14 17:15 被阅读0次

    心路历程:
    //https://stackoverflow.com/questions/35050610/can-a-gradle-copy-task-be-used-to-copy-a-file-from-a-url
    //https://plugins.gradle.org/plugin/org.ysb33r.vfs

    Issue:

    HEAD method failed for "https://hostname/repository/download/AndroidProjects/UPP/app/build/outputs/mapping/mapping.txt" with HTTP status 401.

    Resolve:

    //https://stackoverflow.com/questions/24401930/copying-a-folder-with-apache-vfs2-causing-error-caused-by-org-apache-commons-vf

    Work well.

    //in root build.gradle
    buildscript {
        repositories {
            jcenter()
            mavenCentral()
        }
        dependencies {
            //https://plugins.gradle.org/plugin/org.ysb33r.vfs
            classpath 'org.ysb33r.gradle:vfs-gradle-plugin:1.0.1'
            classpath 'commons-httpclient:commons-httpclient:3.1' // If you want http/https
        }
    }
    
    // in host app build.gradle
    plugins {
        ...
        id 'org.ysb33r.vfs'
    }
    
    task downloadMyFile {
        vfs {
            cp 'https://username:password@hostname/repository/download/MobileAndroid/app/build/outputs/mapping/mapping.txt', new File("${buildDir}/myDownLoadDir/README.md"),overwrite:true
        }
    }
    
    //setting.gradle
    pluginManagement {
        repositories {
            gradlePluginPortal()
            google()
            mavenCentral()
            jcenter() //must
        }
    }
    dependencyResolutionManagement {
        repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
        repositories {
            google()
            mavenCentral()
        }
    }
    ....
    

    另一种方式:

    task downloadMYFile(type: Exec) {
        try {
            def storeDir = "$buildDir\\a"
            mkdir(storeDir)
            workingDir "$storeDir"
    //specified the ‘workingDir’ first ,it will skip or resolve permiss denied issue.
            def command = "curl -O https://username:password@domain.com:443/devops-tools/latest/manifest.json"
            def platform = System.getProperty("os.name").toLowerCase(Locale.ROOT)
            println "curl start $platform==>$command"
            if (platform.contains("windows")) {
                commandLine 'cmd', '/c', "$command"
            } else {
                commandLine 'bash', '/c', "$command"
            }
            println "curl end==> ${out.toString()}"
        } catch (Exception e) {
            println "curl exception"
            println "${e.getMessage()}"
            e.printStackTrace()
        }
    }
    

    以上方式目前实验下来只支持文件下载,并不支持文件夹下所有东西下载,后面研究了再更新

    -----------------------------End-----------------------------

    我也是有底线的,感谢您的耐心阅读,欢迎支持与点赞。

    相关文章

      网友评论

          本文标题:Gradle下载远程服务器上的文件

          本文链接:https://www.haomeiwen.com/subject/xxlfirtx.html