BigO
BigO

Reputation: 11

Gradle Exec task fails running sed

my build script has the following task

task editProjectArtificat (type:Exec) {
executable "sed"
args  "-e '/myInsertionMatchingPattern/r " + projectDir.toString() + "/scripts/install/myTextFileToInsert' < " + projectDir.toString() + "/build/scripts/MyOriginalInputFile > " + projectDir.toString() + "/build/scripts/MyChangedOutputFile"

}

gradle build fails when the above task executes with this error message

sed: 1: " '/myInsertionPattern/r ...": invalid command code

FAILURE: Build failed with an exception.

However, when I change the gradle.build script to make the task look like this

task editProjectArtificat (type:Exec) {
executable "sed"
args  "-e /myInsertionMatchingPattern/r " + projectDir.toString() + "/scripts/install/myTextFileToInsert < " + projectDir.toString() + "/build/scripts/MyOriginalInputFile > " + projectDir.toString() + "/build/scripts/MyChangedOutputFile"

}

Now that both of the "'" removed in the "arg" line, we no longer get gradle build errors; however, sed does not produce "MyChangedOutputFile" file as expected when gradle build is done.

Typing sed command with both "'" on a shell produces the expected output? sed fails when the "'" are removed on the shell. my understanding sed needs "'" around the matching pattern and commands.

Upvotes: 1

Views: 2659

Answers (1)

meuh
meuh

Reputation: 12255

I don't know gradle, but it seems like args needs a list with each argument separated. However, you are using redirection (< and >) and that has to be done by the shell, so you shouldn't be executing sed but bash. You want to have something like bash -c "sed -e '/.../r ...' <... >..." so something like this might work:

task editProjectArtificat (type:Exec) {
 executable "bash"
 args  "-c", "sed -e '/myInsertionMatchingPattern/r " + projectDir.toString() + "/scripts/install/myTextFileToInsert' < " + projectDir.toString() + "/build/scripts/MyOriginalInputFile > " + projectDir.toString() + "/build/scripts/MyChangedOutputFile"
}

Upvotes: 2

Related Questions