Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 966
phisch Avatar asked Mar 01 '13 12:03

phisch


3 Answers

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>
like image 94
mk7 Avatar answered Nov 20 '22 17:11

mk7


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.

like image 8
phisch Avatar answered Nov 20 '22 19:11

phisch


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)

like image 6
ben75 Avatar answered Nov 20 '22 18:11

ben75