如何复制与摇篮副本任务多个目的地?(How to copy to multiple destinat

2019-07-04 00:08发布

我想通过一个摇篮任务拷贝一个文件到多个目的地。 我发现,在其他网站以下,但在运行这个任务,我得到一个错误。

def filesToCopy = copySpec{
    from 'somefile.jar'
    rename {String fileName -> 'anotherfile.jar'}
}

task copyFile(type:Copy) {
    with filesToCopy  {
      into 'dest1/'
    }
    with filesToCopy  {
      into 'dest2/'
    }
}

错误

法无签名:org.gradle.api.internal.file.copy.CopySpecImpl.call()是适用于参数类型

有没有一种方法来复制到多个目的地,在一个摇篮的任务吗?

Answer 1:

如果你真的想他们一个任务,你做这样的事情:

def filesToCopy = copySpec {
  from 'someFile.jar'
  rename { 'anotherfile.jar' }
}

task copyFiles << {
  ['dest1', 'dest2'].each { dest ->
    copy {
      with filesToCopy
      into dest
    }
  }
}


Answer 2:

另一种方式

task myCustomTask << {

    copy {
        from 'sourcePath/folderA'
        into 'targetPath/folderA'
    }

    copy {
        from 'sourcePath/folderB'
        into 'targetPath/folderB'
    }

    copy {
        from 'sourcePath/fileA.java','sourcePath/fileB.java'
        into 'targetPath/folderC'
    }
}


Answer 3:

没有没有办法做到这一点大气压。 我会为每一个目标目录下创建单独的任务的gradle

def filesToCopy = copySpec{
    from 'somefile.jar'
    rename {String fileName -> 'anotherfile.jar'}
}

task copyFileDest1(type:Copy) {
    with filesToCopy
    into 'dest1/'
}

task filesToCopyDest2(type:Copy)  {
    with filesToCopy
    into 'dest2/'
}


Answer 4:

有了一个共同的目标基本路径

如果你的目标路径都有一个共同的路径前缀( dest_base ),那么你可以使用这样的事情:

def filesToCopy = copySpec {
    from 'somefile.jar'
    rename { String fileName -> 'anotherfile.jar' }
}

task copyFile(type: Copy) {
    into 'dest_base'
    into('dest1') {
      with filesToCopy
    }
    into('dest2') {
      with filesToCopy
    }
}

相比于使用其他答案copy方法,这种方法还保留了摇篮的UP-TO-DATE检查。

上面的代码会导致这样的输出:

dest_base/
├── dest1
│   └── anotherfile.jar
└── dest2
    └── anotherfile.jar


Answer 5:

这里是没有copySpec一般的网页摘要摇篮4.1。 正如指出的,关键是使用碱成和相对使用到闭包内(例如,从关闭)。

task multiIntoCopy(type: Copy){
    into(projectDir) // copy into relative to this

    from("foo"){
        into("copied/foo") // will be projectDir/copied/foo
        // just standard copy stuff
        rename("a.txt", "x.txt")
    }

    from("bar"){
        into("copied/aswell/bar") //  projectDir/copied/copied/aswell/bar
    }

    from("baz") // baz folder content will get copied into projectDir

    //from("/bar"){ // this will mess things up, be very careful with the paths
    //    into("copied/aswell/bar")
    //}
}


文章来源: How to copy to multiple destinations with Gradle copy task?
标签: gradle