14

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

比如href="myapp/css/default.min.css"变成href="myapp-0.2.8/css/default.min.css"

我正在使用 maven maven-replacer-plugin 并且对于一个文件来说一切正常:

工作示例

使用文件标签进行单个文件替换。

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

不工作的例子

根据使用指南,我应该能够使用多个文件includes:include

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

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

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

4

7 回答 7

15

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>
于 2014-06-25T07:23:55.927 回答
8

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.

于 2013-03-02T13:04:41.557 回答
6

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)

于 2013-03-01T12:35:36.243 回答
5

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>
于 2015-07-30T12:18:52.533 回答
4

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

于 2013-04-12T09:19:57.770 回答
3

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>
于 2019-03-24T21:39:38.487 回答
0

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>
于 2017-02-22T09:47:00.923 回答