phisch
phisch

Reputation: 4731

maven-replacer-plugin and multiple files

I wrote a Java Web Application where I replace URLs to static content at build time to add version information, primarely for caching.

For example, href="myapp/css/default.min.css" is turned into href="myapp-0.2.8/css/default.min.css"

I am using the maven maven-replacer-plugin and things work fine for one single file:

Working Example

Using the file-Tag for single file replacements.

    <plugin>
      <groupId>com.google.code.maven-replacer-plugin</groupId>
      <artifactId>replacer</artifactId>
      <version>1.5.2</version>
      <executions>
        <execution>
          <phase>prepare-package</phase>
          <goals>
            <goal>replace</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
       <ignoreMissingFile>false</ignoreMissingFile>
       <file>${project.build.directory}/myApp/index.jsp</file>
        <replacements>
          <replacement>
            <token>%PROJECT_VERSION%</token>
            <value>${project.version}</value>
          </replacement>
        </replacements>
      </configuration>
    </plugin>

Maven Debug Output shows this for the working example.

    [DEBUG] Configuring mojo 'com.google.code.maven-replacer-plugin:replacer:1.5.2:replace' with basic configurator -->
    [DEBUG]   (s) basedir = .
    [DEBUG]   (s) commentsEnabled = true
    [DEBUG]   (s) encoding = UTF-8
    [DEBUG]   (s) file = /Users/phisch/Development/MyApp/Workspace/MyApp-WebApp/target/myApp/index.jsp
    [DEBUG]   (s) ignoreErrors = false
    [DEBUG]   (s) ignoreMissingFile = false
    [DEBUG]   (s) preserveDir = true
    [DEBUG]   (s) quiet = false
    [DEBUG]   (s) token = %PROJECT_VERSION%
    [DEBUG]   (s) value = 0.3
    [DEBUG]   (s) replacements = [com.google.code.maven_replacer_plugin.Replacement@3bccdcbd]
    [DEBUG]   (s) skip = false
    [DEBUG]   (s) unescape = false
    [DEBUG] -- end configuration --
    [DEBUG] Replacement run on /Users/phisch/Development/MyApp/Workspace/MyApp-WebApp/target/myApp/index.jsp and writing to /Users/phisch/Development/MyApp/Workspace/MyApp-WebApp/target/myApp/index.jsp with encoding UTF-8
    [INFO] Replacement run on 1 file.

Not Working Example

According to the Usage Guide I should be able to use multiple files with includes:include

But the following pom.xml configurations does nothing (Note the include-Tags startin at line 15)

    <plugin>
      <groupId>com.google.code.maven-replacer-plugin</groupId>
      <artifactId>replacer</artifactId>
      <version>1.5.2</version>
      <executions>
        <execution>
          <phase>prepare-package</phase>
          <goals>
            <goal>replace</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <ignoreMissingFile>false</ignoreMissingFile>
        <includes>
          <include>${project.build.directory}/myApp/index.jsp</include>
        </includes>
        <replacements>
          <replacement>
            <token>%PROJECT_VERSION%</token>
            <value>${project.version}</value>
          </replacement>
        </replacements>
      </configuration>
    </plugin>

The Debug output is as follows. The file exists.

    DEBUG] Configuring mojo 'com.google.code.maven-replacer-plugin:replacer:1.5.2:replace' with basic configurator -->
    [DEBUG]   (s) basedir = .
    [DEBUG]   (s) commentsEnabled = true
    [DEBUG]   (s) encoding = UTF-8
    [DEBUG]   (s) ignoreErrors = false
    [DEBUG]   (s) ignoreMissingFile = false
    [DEBUG]   (s) includes = [/Users/phisch/Development/MyApp/Workspace/MyApp-WebApp/target/myApp/index.jsp]
    [DEBUG]   (s) preserveDir = true
    [DEBUG]   (s) quiet = false
    [DEBUG]   (s) token = %PROJECT_VERSION%
    [DEBUG]   (s) value = 0.3
    [DEBUG]   (s) token = %MyApp_PROJECT_VERSION%
    [DEBUG]   (s) value = 0.3 (Build: 20130301-1130)
    [DEBUG]   (s) replacements = [com.google.code.maven_replacer_plugin.Replacement@235d4338, com.google.code.maven_replacer_plugin.Replacement@3fe823ab]
    [DEBUG]   (s) skip = false
    [DEBUG]   (s) unescape = false
    [DEBUG] -- end configuration --
    [INFO] Replacement run on 0 file.

How can I replace the same token/value pairs in multiple files?

Upvotes: 16

Views: 53315

Answers (7)

Francesco Iannazzo
Francesco Iannazzo

Reputation: 626

I used the plugin in version 1.5.3 and all works fine

  <plugin>
            <groupId>com.google.code.maven-replacer-plugin</groupId>
            <artifactId>replacer</artifactId>
            <version>1.5.3</version>
            <executions>
                <execution>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>replace</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <basedir>ENTER_YOUR_BASEDIR</basedir>
                <includes>
                    <include>**/*.js</include>
                </includes>
                <replacements>
                    <replacement>
                        <token>WHAT_TO_REPLACE</token>
                        <value>VALUE</value>
                    </replacement>
                </replacements>
            </configuration>
        </plugin>

Upvotes: 4

terrasson marc
terrasson marc

Reputation: 107

I tried all answers here but no one worked for me. I manage to work around this issue by making multiple "single replacement" executions of the plugin

<plugin>
            <groupId>com.google.code.maven-replacer-plugin</groupId>
            <artifactId>replacer</artifactId>
            <inherited>false</inherited>
            <executions>
                <execution>
                    <id>replace-xxx.properties</id>
                    <phase>install</phase>
                    <goals>
                        <goal>replace</goal>
                    </goals>
                    <inherited>false</inherited>
                    <configuration>
                        <file>target/xxx.properties</file>
                        <replacements>
                            <replacement>
                                <token>$${dev.mail.server.address}</token>
                                <value>xxx</value>
                            </replacement>
                            <replacement>
                                <token>$${dev.mail.server.port}</token>
                                <value>yyyy</value>
                            </replacement>
                            <replacement>
                                <token>${dev.</token>
                                <value>${</value>
                            </replacement>
                        </replacements>
                        <regex>false</regex>
                    </configuration>
                </execution>
                <execution>
                    <id>replace-zzz-config.properties</id>
                    <phase>install</phase>
                    <goals>
                        <goal>replace</goal>
                    </goals>
                    <inherited>false</inherited>
                    <configuration>
                        <file>target/zzz-config.properties</file>
                        <replacements>
                            <replacement>
                                <token>$${dev.hazelcast.client.group.name}</token>
                                <value>ttt</value>
                            </replacement>
                            <replacement>
                                <token>${dev.</token>
                                <value>${</value>
                            </replacement>
                        </replacements>
                        <regex>false</regex>
                    </configuration>
                </execution>
                <execution>
                    <id>replace-aaa-security.properties</id>
                    <phase>install</phase>
                    <goals>
                        <goal>replace</goal>
                    </goals>
                    <inherited>false</inherited>
                    <configuration>
                        <file>target/aaa-security.properties</file>
                        <replacements>
                            <replacement>
                                <token>${dev.</token>
                                <value>${</value>
                            </replacement>
                        </replacements>
                        <regex>false</regex>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Upvotes: 0

Lawrence
Lawrence

Reputation: 1056

I had the same problem with 1.5.2 and reverted to

<filesToinclude>file1, file2</filesToInclude>

however I can imagine one would not like to add a dozen files manually...

Upvotes: 5

Michael
Michael

Reputation: 53

The solution for the plugin version 1.5.2 by mk7 works for me. I added a basedir-Tag (i didn't had one) before the include-Tag in the plugin configuration.

<basedir>${basedir}</basedir>

Upvotes: 5

mk7
mk7

Reputation: 215

The includes tag works with version 1.5.2 as well, you just have to specify the basedir tag before includes, and put the filepath (excluding the filename) as the basedir value and just the filename as the include tag value. So in your case something like this should work:

<plugin>
      <groupId>com.google.code.maven-replacer-plugin</groupId>
      <artifactId>replacer</artifactId>
      <version>1.5.2</version>
      <executions>
        <execution>
          <phase>prepare-package</phase>
          <goals>
            <goal>replace</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <basedir>${project.build.directory}/myApp</basedir>
        <includes>
          <include>index.jsp</include>
        </includes>
        <replacements>
          <replacement>
            <token>%PROJECT_VERSION%</token>
            <value>${project.version}</value>
          </replacement>
        </replacements>
      </configuration>
    </plugin>

Upvotes: 16

ben75
ben75

Reputation: 28706

From the maven-replacer-plugin doc :

ignoreMissingFile: Set to true to not fail build if the file is not found. First checks if file exists and exits without attempting to replace anything. Only usable with file parameter.

So I suggest to remove this parameter when using the <includes>

EDIT: use maven-replacer-plugin version 1.5.1 since version 1.5.2 seems buggy regarding this feature (thanks to phisch for this precision)

Upvotes: 6

phisch
phisch

Reputation: 4731

This seems to be a bug in the latest 1.5.2 Version.

As soon as I change the version on bugfix level down to 1.5.1, the Not Working Example works just as expected and all tokens are replaced by their values.

<plugin>
  <groupId>com.google.code.maven-replacer-plugin</groupId>
  <artifactId>replacer</artifactId>
  <version>1.5.1</version>
  <executions>
    <execution>
      <phase>prepare-package</phase>
      <goals>
        <goal>replace</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <includes>
      <include>${project.build.directory}/myApp/index.jsp</include>
    </includes>
    <replacements>
      <replacement>
        <token>%PROJECT_VERSION%</token>
        <value>${project.version}</value>
      </replacement>
    </replacements>
  </configuration>
</plugin>

I also removed the ignoreMissingFile as suggested by ben.

Upvotes: 8

Related Questions