maven-replacer-plugin和多个文件

phi*_*sch 10 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>
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)

不工作的例子

根据使用指南,我应该可以使用多个文件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>
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)

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

小智 9

includes标签与1.5.2版本的作品,以及,你只需要指定basedir标记之前includes,并把文件路径(不包括文件名)的basedir价值,只是文件名作为include标记值.所以在你的情况下这样的东西应该工作:

<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>
Run Code Online (Sandbox Code Playgroud)


phi*_*sch 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>
Run Code Online (Sandbox Code Playgroud)

我还删除了ben建议的ignoreMissingFile.


ben*_*n75 6

maven-replacer-plugin doc:

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

所以我建议在使用时删除此参数 <includes>

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


小智 5

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

<basedir>${basedir}</basedir>
Run Code Online (Sandbox Code Playgroud)