2

I'm working on a project with Maven and using the maven-replacer-plugin to replace all my css/javascript files for the minified version.

I want to replace this :

<!-- JSMIN js/script.min.js -->
    <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script src="js/jquery.placeholder.min.js"></script>
    <script src="js/smoothscroll.js" async=""></script>
    <script src="js/javascript.js"></script>
    <script src="js/swfobject.js"></script>
    <script src="js/player.js"></script>
<!-- END JSMIN -->

with this:

<script src="js/script.js"></script>

Similar for the css's.

The plugin says that it has applied the regex to my 4 html files which is correct but i see no changes on those files.

Here is the configuration for the plugin

<plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>maven-replacer-plugin</artifactId>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <goals>
                <goal>replace</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <basedir>${project.basedir}/src/main/webapp/</basedir>
        <outputDir>test</outputDir>
        <includes>
            <include>**/*.html</include>
        </includes>
        <regexFlags>
            <regexFlag>CASE_INSENSITIVE</regexFlag>
            <regexFlag>MULTILINE</regexFlag>
            <regexFlag>DOTALL</regexFlag>
        </regexFlags>
        <replacements>
            <replacement>
                <token>&lt;!-- JSMIN (.*) --&gt;(.*?(\r))+.*?&lt;!-- END JSMIN --&gt;</token>
                <value>&lt;script src="$1"&gt;&lt;/script&gt;</value>
            </replacement>
            <replacement>
                <token>&lt;!-- CSSMIN (.*) --&gt;(.*?(\r))+.*?&lt;!-- END CSSMIN --&gt;</token>
                <value>&lt;link href="$1" rel="stylesheet" type="text/css"/&gt;</value>
            </replacement>
        </replacements>
    </configuration>
</plugin>

The regex seems correct to me and my co-workers...any help? Thanks in advance

Pedro Garcia Mota
  • 928
  • 2
  • 10
  • 20
  • Any particular reason not using the yui-compressor plugin for that purpose (http://alchim.sourceforge.net/yuicompressor-maven-plugin/index.html)? – khmarbaise Jul 19 '12 at 10:31
  • I do use another plugin for the minify part, but we need to have the 2 versions of the code, the original html with all the links and the deployment version with the minified files, on the packaging phase we use the replacer to produce a smaller tar.gz but keep the original code intact – Pedro Garcia Mota Jul 19 '12 at 10:47

2 Answers2

6

http://code.google.com/p/maven-replacer-plugin/wiki/UsageWithOtherPlugins

There needs to be a hooking spot for running replacements on files before they are packaged into a war. Make sure you add this before the maven-replacer-plugin if they both execute in the same phase (i.e. prepare-package). Make sure you use version 2.0.1 or below of the war plugin since the commonly used 2.1.0+ copies resources twice which overrides replacements. Use the following war plugin configuration to allow the files to be modified before being packaged.

Levente
  • 76
  • 1
  • 2
0

I have copy pasted your code in a test pom and it works fine.

Are you sure you checking the correct files? the updated files are in /src/main/webapp/test

M.

poussma
  • 7,033
  • 3
  • 43
  • 68
  • We've managed to solve the problem, i'll post our solution later since i can't auto-answer right now. Basically we've declared all the js/css in a single line on the html and change the regex to <![CDATA[.*]]> Maybe the problem was the \r since we develop in linux,windows and mac. Thanks for the help anyways – Pedro Garcia Mota Jul 19 '12 at 13:34