Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven String Replace of Text Web Resources

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?

like image 907
Jaco van Niekerk Avatar asked Jun 10 '10 20:06

Jaco van Niekerk


2 Answers

I had the same problem and I was fiddling a lot with this issue, so I will answer although this question is pretty old. As stated by leandro and Phil, one can use the maven-replacer-plugin. But their solution didn't work for me. Enabling useCache caused an error which made it impossible to build the project. Additionally, I can't make the auto-clean thing to work properly. As I am not allowed yet to comment on the post, I will provide my full solution here:

First of all, configure the maven-replacer-plugin:

<plugin> 
  <groupId>com.google.code.maven-replacer-plugin</groupId>
  <artifactId>maven-replacer-plugin</artifactId>
  <version>1.3.7</version>
  <executions>
    <execution>
      <phase>prepare-package</phase>
      <goals>
        <goal>replace</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <includes>
      <include>target/${project.build.finalName}/static/**/*.css</include>
    </includes>
    <regex>false</regex>
    <token>someString</token>
    <value>replaceString</value>
  </configuration>
</plugin>

Before the actual war is built, we create an exploded war. This means, the whole content of the war file is stored in a sub directory (which is target/${project.build.finalName} by default). After that, the maven-replacer-plugin will change the contents of the files as we have specified. Finally, the .war will be packed in the package phase by the default-war job. To avoid the content of the exploded war folder being overridden, one has to set warSourceDirectory to the directory where the exploded war stuff is stored. The following configuration snippet will do this job:

<plugin>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.1.1</version>
  <executions>
    <execution>
      <id>prepare</id>
      <phase>prepare-package</phase>
      <goals>
        <goal>exploded</goal>
  </goals>
    </execution>
    <execution> 
      <id>default-war</id>
      <phase>package</phase>
      <goals>
        <goal>war</goal>
      </goals>
      <configuration>
        <warSourceDirectory>${project.build.directory}/${project.build.finalName}</warSourceDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

The package with the replaced content can be built using mvn clean package

like image 146
fixje Avatar answered Oct 21 '22 10:10

fixje


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.

like image 5
Pascal Thivent Avatar answered Oct 21 '22 10:10

Pascal Thivent