How to replace text in files using Gradle/Groovy functionality
Asked Answered
L

1

6

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?

Loredo answered 24/4, 2015 at 7:58 Comment(0)
C
6

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')
   }
}                                                                                                                                                                             
Chapen answered 24/4, 2015 at 9:48 Comment(1)
BTW, I had to replace rootDir with projectDir to make it work in multi-project setups as wellLoredo

© 2022 - 2025 — McMap. All rights reserved.