简体   繁体   中英

Change file extensions in gradle

This seems so simple, yet I cannot find a single source that provides a working solution.

I have a project like this:

src
 -main
  -resources
   -bpmns
    -nnn.bpmn

and I want gradle to rename all files that end with bpmn to bpmn20.xml , leaving all files that already have bpmn20.xml extensions untouched.

This is my approach:

task renameBPMN(type: Copy) {
    outputs.upToDateWhen { false } //to pevent gradle from not executing this task
    from('src/main/resources/bpmn/')
    into('src/main/resources/bpmn/')
    rename ('/^.*\\.(bpmn)$/', '$1.bpmn20.xml')
}

According to some regex testers online, the regex should match nnn.bpmn , gradle however does nothing at all.

Any ideas anyone?

This is what he needs or actually this script would do it.

Here it'll change a file: filenamebpmn or filename.bpmn to filenamebpmn20.xml or filename.bpmn20.xml respectively. It'll ignore all other files whether it's a filenamebpmn.xml or filename.bpmn.xml or any other file.

$ cat build.gradle

task renameBPMN() << {

    fileTree("bpmnfolder").matching { include "*bpmn" }.each { fileName ->
      println "Will be changed to new name --- " + fileName
      file("${fileName}").renameTo("${fileName}20.xml")
    }
}

$ ls -l bpmnfolder

total 3
-rw-r--r-- 1 c400093 Domain Users 3 Aug  6 15:14 1bpmn
-rw-r--r-- 1 c400093 Domain Users 3 Aug  6 14:50 2bpmn
-rw-r--r-- 1 c400093 Domain Users 6 Aug  6 14:50 3bpmn.xml

$ /cygdrive/c/gradle-2.0/bin/gradle renameBPMN

:renameBPMN
Will be changed to new name --- C:\cygwin\home\c400093\giga\goga\giga\bpmnfolder\1bpmn
Will be changed to new name --- C:\cygwin\home\c400093\giga\goga\giga\bpmnfolder\2bpmn

BUILD SUCCESSFUL

Total time: 3.153 secs

$ ls -l bpmnfolder

total 3
-rw-r--r-- 1 c400093 Domain Users 3 Aug  6 15:14 1bpmn20.xml
-rw-r--r-- 1 c400093 Domain Users 3 Aug  6 14:50 2bpmn20.xml
-rw-r--r-- 1 c400093 Domain Users 6 Aug  6 14:50 3bpmn.xml

$

AND

If you want to include filenames which end with bpmn as extension ie filename.bpmn or filenamebpmn, then use the following code.

$ ls -l bpmnfolder

total 5
-rw-r--r-- 1 c400093 Domain Users 3 Aug  6 15:39 1bpmn
-rw-r--r-- 1 c400093 Domain Users 3 Aug  6 15:39 2bpmn
-rw-r--r-- 1 c400093 Domain Users 6 Aug  6 15:39 3bpmn.xml
-rw-r--r-- 1 c400093 Domain Users 8 Aug  6 15:39 4.bpmn
-rw-r--r-- 1 c400093 Domain Users 3 Aug  6 15:39 5.bpmn.xml

$ cat build.gradle

task renameBPMN() << {

    fileTree("bpmnfolder").matching { include "*bpmn" include "*.bpmn" }.each { fileName ->
      println "Will be changed to new name --- " + fileName
      file("${fileName}").renameTo("${fileName}20.xml")
    }
}

$ /cygdrive/c/gradle-2.0/bin/gradle renameBPMN

:renameBPMN
Will be changed to new name --- C:\cygwin\home\c400093\giga\goga\giga\bpmnfolder\1bpmn
Will be changed to new name --- C:\cygwin\home\c400093\giga\goga\giga\bpmnfolder\2bpmn
Will be changed to new name --- C:\cygwin\home\c400093\giga\goga\giga\bpmnfolder\4.bpmn

BUILD SUCCESSFUL

Total time: 4.271 secs

$ ls -l bpmnfolder

total 5
-rw-r--r-- 1 c400093 Domain Users 3 Aug  6 15:39 1bpmn20.xml
-rw-r--r-- 1 c400093 Domain Users 3 Aug  6 15:39 2bpmn20.xml
-rw-r--r-- 1 c400093 Domain Users 6 Aug  6 15:39 3bpmn.xml
-rw-r--r-- 1 c400093 Domain Users 8 Aug  6 15:39 4.bpmn20.xml
-rw-r--r-- 1 c400093 Domain Users 3 Aug  6 15:39 5.bpmn.xml

$

Modify your task to:

task rename(type: Copy) {
    outputs.upToDateWhen { false } //to pevent gradle from not executing this task
    from('src/main/resources/bpmn/')
    into('src/main/resources/bpmn/')
    exclude { resource -> !resource.file.name.endsWith('.bpmn')}
    rename '(.*).bpmn', '$1.bpmn20.xml'
}

According to the gradle documentation, AbstractCopyTask::rename() 's regular expression does not need delimiters:

rename '(.*)_OEM_BLUE_(.*)', '$1$2'

Try replacing:

rename ('/^.*\\.(bpmn)$/', '$1.bpmn20.xml')

With:

rename ('^.*\\.(bpmn)$', '$1.bpmn20.xml')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM