使用 Grunt.js Copy 将所有文件从目录复制到另一个目录

作为构建过程的一部分,我试图将一个目录中的所有文件复制到另一个目录。对于我明确指定的单个文件,它工作得很好,但是当我试图复制整个目录时,它会做一些奇怪的事情,比如复制整个目录结构(或者什么都不复制)。下面是我的 GruntFile.js 中相关的部分:

copy: {
myvoice: {
files: [
{ src:"src/html/index.html", dest:"dist/myvoice/index.html" },
{ src:"src/html/css/style.css", dest:"dist/myvoice/css/style.css" },
{ src:"src/html/js/require.js", dest:"dist/myvoice/js/require.js" },
{ src:"build/myvoice/main.js", dest:"dist/myvoice/js/main.js" },
{ src:"src/html/css/fonts/*", dest:"dist/myvoice/css/fonts/" }
]
}
},

尤其是最后一句我没法开始工作的台词:

      { src:"src/html/css/fonts/*", dest:"dist/myvoice/css/fonts/" }
84231 次浏览

This task will maintain folder structure if you specify a file glob. What you want is the flatten option which will remove the structure.

{
expand: true,
flatten: true,
src: ['src/html/css/fonts/**'],
dest: 'dist/myvoice/css/fonts/',
filter: 'isFile'
}

Find the rest of the available options in the Github repo. Hope this helps.

The flatten: true option as in this answer might work for some cases, but it seems to me that the more common requirement (as in my case) is to copy a folder and its sub-folder structure, as-is, to dest. It seems that in most cases if you have sub-folders, they are probably being referenced that way in code. The key to doing this is the cwd option, which will preserve folder structure relative to the specified working directory:

copy: {
files: {
cwd: 'path/to/files',  // set working folder / root to copy
src: '**/*',           // copy all files and subfolders
dest: 'dist/files',    // destination folder
expand: true           // required when using cwd
}
}

I would like to add that changing the format of the glob in src will modify how the copy works.

As pointed out by bmoeskau above, the following will copy everything inside dist/ and move it to path/to/dir (overwriting the destination if it already exists).

copy: {
files: {
expand: true,
dest: 'path/to/dir',
cwd: 'dist/',
src: '**'
}
}

Note however, that:

copy: {
files: {
expand: true,
dest: 'path/to/dir',
cwd: 'dist/',
src: '*'
}
}

Will only copy files inside dist/ as well as directories, but will not copy the contents of those directories to the destination.

Also, the following with src: '*/*' will only copy directories with contents inside dist/. That is, files just inside dist/ will not be copied.

copy: {
files: {
expand: true,
dest: 'path/to/dir',
cwd: 'dist/',
src: '*/*'
}
}

Finally, same as above, but src: '**/**' will copy only files inside dist/ as well as files inside dist/ subdirectories to path/to/dir. So there will be no folders inside the destination.

copy: {
files: {
expand: true,
dest: 'path/to/dir',
cwd: 'dist/',
src: '*/*',
flatten: true,
filter: 'isFile'
}
}

Had to use egdy instead curly braces for the files segment (in Coffeescript)...

copy: {
files: [
cwd: 'path/to/files'
src: '**/*'
dest: 'dist/files'
expand: true
]
}

If you are developing with angular yeoman , then this is the better way to copy with grunt. expand: true is required when using cwd. <%= yeoman.app %> is just the app route ('.').

 {
expand: true,
cwd: '<%= yeoman.app %>/data',
dest: '<%= yeoman.dist %>/data',
src: ['**']
}