【问题标题】:How to upload folder contents to FTP server using Gradle如何使用 Gradle 将文件夹内容上传到 FTP 服务器
【发布时间】:2014-06-05 08:00:20
【问题描述】:

我是 gradle 新手,我不知道如何将我的 /bin 文件夹内容上传到 FTP 服务器。试图在互联网上找到解决方案,但他们对我没有帮助。

我的 build.gradle 文件如下:

apply plugin: 'java'

sourceCompatibility = 1.6
version = '1.0'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.apache.httpcomponents:httpclient:4.3.3'
    compile fileTree(dir: 'lib', include: '*.jar')
    compile 'org.apache.directory.studio:org.dom4j.dom4j:1.6.1'
    compile 'jaxen:jaxen:1.1.4'

    testCompile group: 'junit', name: 'junit', version: '4.11'
}

build.doLast {
    copy {
        into 'bin'
        from 'build/libs'
    }
}

现在我想编写将 /bin 文件夹内容上传到 FTP 服务器的任务。 任何帮助,将不胜感激。提前致谢

【问题讨论】:

    标签: gradle build.gradle


    【解决方案1】:

    ftpAntTask 使用起来相当简单。

    buildscript {
        repositories {
            mavenCentral()
        }
    }
    repositories{
        mavenCentral()
    }
    
    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 ftp << {
        ant {
            taskdef(name: 'ftp',
                    classname: 'org.apache.tools.ant.taskdefs.optional.net.FTP',
                    classpath: configurations.ftpAntTask.asPath)
            ftp(server: "(removed)", userid: "(removed)", password: "(removed)", remoteDir: "(removed)") {
                fileset(dir: "(removed)") {
                    include(name: "(removed)")
                }
            }
        }
    }
    

    (这个例子来自How to FTP a file from an Android Gradle build?

    【讨论】:

      【解决方案2】:

      使用apache commons net 库。有一个 exampleFTPClient 用法。

      您还需要为构建脚本本身配置依赖项。下面的代码做到了:

      import org.apache.commons.net.ftp.FTPClient
      
      buildscript {
          repositories {
              mavenCentral()
          }
      
          dependencies {
              classpath 'commons-net:commons-net:3.3'
          }
      }
      
      task upload << {
           def ftp = new FTPClient()
           //following logic..
      }
      

      【讨论】:

      • 例子是480行... 10以内能搞定吗?
      • @Noumenon 这个例子的大部分只是......也许 10 行太少了,但少于 30 行是可能的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-25
      • 2014-10-17
      • 2015-03-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-14
      • 2016-01-07
      相关资源
      最近更新 更多