14

主にキャッシングのために、ビルド時に URL を静的コンテンツに置き換えてバージョン情報を追加する Java Web アプリケーションを作成しました。

たとえばhref="myapp/css/default.min.css"href="myapp-0.2.8/css/default.min.css"

私はmaven maven-replacer-pluginを使用していますが、1つのファイルで問題なく動作します:

実施例

単一ファイルの置換に 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.

動作しない例

使用ガイドによると、複数のファイルを使用できるはずです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

この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>
于 2014-06-25T07:23:55.927 に答える
8

これは、最新の 1.5.2 バージョンのバグのようです。

バグ修正レベルでバージョンを 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>

ベンが提案したように、ignoreMissingFileも削除しました。

于 2013-03-02T13:04:41.557 に答える
6

maven-replacer-plugin docから:

ignoreMissingFile:ファイルが見つからない場合にビルドに失敗しないようにtrueに設定します。まず、ファイルが存在するかどうかを確認し、何も置き換えようとせずに終了します。ファイルパラメータでのみ使用できます。

したがって、を使用する場合は、このパラメータを削除することをお勧めします<includes>

編集:バージョン1.5.2はこの機能に関してバグがあるように見えるので、maven-replacer-pluginバージョン1.5.1を使用してください(この精度についてはphischに感謝します)

于 2013-03-01T12:35:36.243 に答える
4

1.5.2でも同じ問題があり、元に戻しました

<filesToinclude>file1, file2</filesToInclude>

ただし、1ダースのファイルを手動で追加したくないと思うことは想像できます...

于 2013-04-12T09:19:57.770 に答える