centic
centic

Reputation: 15872

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?

Upvotes: 6

Views: 4110

Answers (1)

Opal
Opal

Reputation: 84784

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')
   }
}                                                                                                                                                                             

Upvotes: 6

Related Questions