主にキャッシングのために、ビルド時に 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.
複数のファイルで同じトークン/値のペアを置き換えるにはどうすればよいですか?