I have a Maven web application with text files in
src/main/webapp/textfilesdir
As I understand it, during the package phase this te
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
I had a problem with the phases, using prepare-package not copied the file to war, my solution was this:
Add this configuration in maven-war-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
<executions>
<configuration>
<webResources>
<resource>
<directory>${basedir}/WebContent?</directory> <excludes>
<!--Exclude the file because it is copied using the maven replacer plugin -->
<exclude>/style.scss</exclude>
</excludes>
</resource>
</webResources>
</configuration>
</plugin>
Add the configuration to replacer
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.1</version>
<executions>
<execution>
<id>scss-replacement</id> <phase>prepare-package</phase> <goals>
<goal>replace</goal>
</goals> <configuration>
<file>WebContent?/css/style.scss</file>
<!-- My local file(WebContent?/css/style.scss) have a param
$url: "http://localhost:8080/interface";
--> <!-- regex with match to $url: "http://localhost:8080/interface"; -->
<token>\$url:.</token>
<!-- Replace to -->
<value>\$url: "www.myapplication.com.br/css/style.css";</value>
<outputFile>target/app/css/style.scss</outputFile>
</configuration>
</execution>
<executions>
<plugin>
The output File not overwrite the file, so i put the configuration maven-war-plugin to exclude the file style.scss. bye!
Another solution from me, sorry for being late to the party but still might be useful for some people. Solution above simply didn't work for me because I had overlays in place and therefore using the replacer plugin was hard in the main source. Hence, I used maven-war-plugin to generate the temporary directory for me in the prepare-package phase and have replacer plugin to manipulate it and release the war from that directory so that nothing else overrides with it.
<plugin>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<id>prepare</id>
<phase>prepare-package</phase>
<goals>
<goal>exploded</goal>
</goals>
<configuration>
<webappDirectory>${project.build.directory}/${project.build.finalName}-patched/</webappDirectory>
</configuration>
</execution>
<execution>
<id>default-war</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
<configuration>
<useCache>true</useCache>
<warSourceDirectory>${project.build.directory}/${project.build.finalName}-patched/</warSourceDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>${project.build.directory}/${project.build.finalName}-patched/fileToBeChanged.txt</file>
<token>ValueToChange</token>
<value>ValueToReplace</value>
</configuration>
</plugin>
You can use 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>
<file>target/${project.artifactId}-${project.version}/WEB-IN/site.xml</file>
<replacements>
<replacement>
<token>ear.version</token>
<value>${ear.version}-${maven.build.timestamp}</value>
</replacement>
</replacements>
</configuration>
</plugin>
But you need two more tricks. One is adding the explode goal to the war plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
.....
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
</executions>
</plugin>
And finally to need to call mvn clean before the package. You can do it from your pom:
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
If you are using a version of the maven-war-plugin newer than 2.0.1, you will need to include true in the configuration of the maven-war-plugin, otherwise the changes to your file(s) will be splotted when the war plugin copies your webapp over a second time. If you are using Hudson/Jenkins, you will have to use a version newer than 2.0.1.
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:
prepare-package
and configure it to process the files and put the processed files in some directory under target
(e.g. target/textfilesdir
).target/textfilesdir
as a webResource
. Refer to Adding and Filtering External Web Resources for the details.Solution 2:
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
.I think I'd go for the second solution.