Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace text in files using Gradle/Groovy functionality

I am trying to work around the problem described in GRADLE-2293 where generated files are always updated because a timestamp is written to the Eclipse files located in directory .settings by the Gradle plugin which generates the Eclipse project files.

The files contain a header like this which I would like to remove

#
#Fri Mar 27 10:26:55 CET 2015

Currently I am using an Exec task to use the external application sed to cut out lines starting with '#':

task adjustEclipseSettingsFile(type: Exec) {
    executable 'sed'
    args '-i','-e','s/^#.*//g','.settings/org.eclipse.jdt.core.prefs'
}
eclipseJdt.finalizedBy adjustEclipseSettingsFile

however this adds a dependency on operating system binaries that I would like to avoid.

How can I do this simple removal of lines starting with '#' in a Gradle task without calling external tools?

like image 816
centic Avatar asked Apr 24 '15 07:04

centic


1 Answers

There are really many ways of doing it, the one with ant is probably most reliable:

task removeLines << {
   ant.replaceregexp(match:'^#.*', replace:'', flags:'g', byline:true) {
      fileset(dir: project.projectDir, includes: 'lol')
   }
}                                                                                                                                                                             
like image 68
Opal Avatar answered Sep 22 '22 16:09

Opal