2016-08-23 57 views
6

我有這樣的目錄結構:複製整個目錄的搖籃

file1.txt 
file2.txt 
dir1/ 
    file3.txt 
    file4.txt 

我想用Gradle to copy是整個結構到另一個目錄。我嘗試這樣做:

task mytest << { 
    copy { 
     from "file1.txt" 
     from "file2.txt" 
     from "dir1" 

     into "mytest" 
    } 
} 

但是這導致了以下內容:

mytest/ 
    file1.txt 
    file2.txt 
    file3.txt 
    file4.txt 

看到,從dir1副本中dir1複製的文件,而我想複製dir1本身

是否可以直接用Gradle copy做到這一點?

到目前爲止,我只能夠拿出這樣的解決方案:

task mytest << { 
    copy { 
     from "file1.txt" 
     from "file2.txt" 

     into "mytest" 
    } 

    copy { 
     from "dir1" 
     into "mytest/dir1" 
    } 
} 

對於我的簡單的例子,沒有太多吧,但在我的實際情況下,有很多目錄我要複製,我不想重複這麼多。

回答

13

您可以使用.的目錄路徑和include來指定,要複製的文件和目錄:

copy { 
    from '.' 
    into 'mytest' 
    include 'file*.txt' 
    include 'dir1/**' 
} 

如果同時frominto是目錄,你會用完整副本結束目標目錄中的源目錄。

+0

與此問題是如果我有這個文件夾中的其他文件,我*不*要複製。我可以使用'excludes'參數,但這會很容易出錯,因爲將來可能會添加其他子目錄。 – Kip

+0

「包含」爲你工作嗎?我已經擴大了答案,以顯示如何指定要複製的文件/目錄。 –

+0

非常感謝! – Kip

0

我知道這有點晚,但我嘗試了上面的@Andrew解決方案,它複製了目錄中的所有內容。 「。」現在不需要代表一個直接的gradle。 所以我做了一些研究 發現this

,並創建了下面的代碼(以跟上時代的檢查),在此基礎上:

任務resourcesCopy(){

doLast { 
    copy { 
     from "src/main/resources" 
     into "./target/dist/WEB-INF/classes" 
    } 
    copy { 
     from "GeoIP2.conf" 
     into "./target/dist/WEB-INF" 
    } 
} 

}

-1

也許還有幫助:使用fileTree遞歸複製整個目錄,例如,

task mytest << { 
    copy { 
     from fileTree('.') 
     into "mytest" 
    } 
}