maven-replacer-plugin和多个文件

时间:2013-03-01 12:27:00

标签: maven maven-replacer-plugin

我编写了一个Java Web应用程序,我在构建时将URL替换为静态内容,以添加版本信息,主要用于缓存。

例如,href="myapp/css/default.min.css"变为href="myapp-0.2.8/css/default.min.css"

我正在使用maven maven-replacer-plugin,一个文件的工作正常:

工作示例

使用file-Tag进行单个文件替换。

    <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在工作示例中显示了这一点。

    [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.

不工作示例

根据Usage Guide,我应该可以使用includes:include

的多个文件

但是以下pom.xml配置什么都不做(注意第15行的include-Tags startin)

    <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>

Debug输出如下。该文件存在。

    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.

如何在多个文件中替换相同的标记/值对?

7 个答案:

答案 0 :(得分:9)

includes标记也适用于版本1.5.2,您只需在basedir之前指定includes标记,并将文件路径(不包括文件名)作为{ {1}}值,只有文件名为basedir标记值。所以在你的情况下,这样的事情应该有效:

include

答案 1 :(得分:7)

这似乎是最新1.5.2版本中的一个错误。

只要将bugfix级别的版本更改为1.5.1, Not Working Example 就会按预期工作,并且所有令牌都会被其值替换。

<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>

我还根据ben。

的建议删除了ignoreMissingFile

答案 2 :(得分:6)

来自maven-replacer-plugin doc

  

ignoreMissingFile :如果找不到文件,则设置为true以使构建失败。首先检查文件是否存在并退出而不尝试替换任何内容。 仅适用于文件参数。

所以我建议在使用<includes>

时删除此参数

编辑:使用maven-replacer-plugin版本1.5.1,因为版本1.5.2似乎有关此功能的错误(感谢phisch的这种精度)

答案 3 :(得分:4)

我在使用1.5.2时遇到了同样的问题并恢复了

<filesToinclude>file1, file2</filesToInclude>

但是我可以想象一个人不想手动添加十几个文件......

答案 4 :(得分:2)

mk7的插件版本1.5.2的解决方案适合我。 我在插件配置中的include-Tag之前添加了一个basedir-Tag(我没有一个)。

<basedir>${basedir}</basedir>

答案 5 :(得分:0)

我在这里尝试了所有答案,但没有人为我工作。 我设法通过对插件进行多次“单次替换”执行来解决这个问题

<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>

答案 6 :(得分:0)

我在1.5.3版中使用了插件,并且一切正常

  <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>
相关问题