views:

214

answers:

1

I have a Maven web application with text files in

src/main/webapp/textfilesdir

As I understand it, during the package phase this textfilesdir directory will be copied into the

target/project-1.0-SNAPSHOT

directory, which is then zipped up into a

target/project-1.0-SNAPSHOT.war

Problem

Now, I need to do a string replacement on the contents of the text files in target/project-1.0-SNAPSHOT/textfilesdir. This must then be done after the textfilesdir is copied into target/project-1.0-SNAPSHOT, but prior to the target/project-1.0-SNAPSHOT.war file being created. I believe this is all done during the package phase.

How can a plugin (potentially maven-antrun-plugin), plug into the package phase to do this.

The text files don't contain properties, like ${property-name} to filter on. String replacement is likely the only option.

Options

  1. Modify the text files after the copy into target/project-1.0-SNAPSHOT directory, yet prior to the WAR creation.

  2. After packaging, extract the text files from WAR, modify them, and add them back into the WAR.

I'm thinking there is another option here I'm missing. Thoughts anyone?

+1  A: 

Option 1 is not doable, prepare-package is too early, package is too late so I don't see where you could plug any custom work. Option 2 is doable but a painful IMO. So here are some more propositions (all based on AntRun and the ReplaceRegExp and/or Replace tasks).

Solution 1:

  1. Create a new folder where you put the text files that need to be processed.
  2. Bind the antrun plugin to prepare-package and configure it to process the files and put the processed files in some directory under target (e.g. target/textfilesdir).
  3. Configure the war plugin to include target/textfilesdir as a webResource. Refer to Adding and Filtering External Web Resources for the details.

Solution 2:

  1. Bind the antrun plugin to prepare-package and configure it to process the text files from src/main/webapp/textfilesdir and put the processed files in the target/project-1.0-SNAPSHOT.
  2. Configure the war plugin to exclude the previously processed files. Again, refer to Adding and Filtering External Web Resources for the details.

I think I'd go for the second solution.

Pascal Thivent
as usually, I'll suggest to use gmaven (http://docs.codehaus.org/display/GMAVEN/Executing+Groovy+Code) to embed groovy rather than ant, but otherwise I fully agree (+1)
seanizer
@seanizer: I need to think more Groovy :) It would certainly play nicely here.
Pascal Thivent
yup. while I'm not too crazy about building groovy apps (grails etc), I think groovy is absolutely underrated as a scripting language
seanizer
Agreed, option 2 sounds like the way to go. I was wondering if there might be another approach, but this is less pom configuration than I originally thought. Thanks!Ant to the rescue? :)
Jaco van Niekerk
Will check out gmaven too...
Jaco van Niekerk