使用 Maven 构建 WAR 文件时重命名静态文件
Renaming static files when building WAR file using Maven
我想在使用 maven-war-plugin 和版本号构建我的 WAR 文件时重命名静态文件。例如,在我的项目中我有一个文件:
src/main/webapp/css/my.css
我希望它在 WAR 文件中显示为:
css/my-versionNumber.css
已经有人问过同样的问题(参见 Rename static files on maven war build),但公认的答案是重命名目录。我不想重命名目录,我想重命名实际文件。
这可能吗?
你需要定义
<properties>
<css.version>1.1.0</css.version>
</properties>
之后用EL调用
css/${css.version}.css
OK,找到方法了,原理来自:Automatic update of generated css files via m2e。
正在重命名文件
详细信息我选择使用Maven AntRun Plugin, because it allows to rename several files using a replacement pattern, see 。
替代解决方案是使用:
maven-assembly-plugin
:但不支持重命名文件集,仅支持重命名单个文件(参见file assembly descriptor and comments of this answer: )
maven-resources-plugin
:它允许复制资源,而不是重命名它们(参见copy resources mojo and comments of this question: Renaming resources in Maven)
使重命名的文件可用于 maven-war-plugin
复制文件:这个想法是将重命名的文件复制到一个临时目录中,由 maven-war-plugin
添加到 WAR 文件作为网络资源。 maven-war-plugin
在 packaging
阶段构建 WAR,因此我们需要在此之前复制重命名的文件。
防止maven-war-plugin
管理被maven-antrun-plugin
重命名的文件:这是通过使用参数warSourceExcludes
.
使用 m2e-wtp
使其在 Eclipse 中运行
更改生命周期映射:问题是m2e
默认情况下不会执行POM文件中定义的所有生命周期(查看生命周期executed/ignored,来自Eclipse,转到您的项目属性,然后 Maven
> Lifecycle Mapping
)。所以你需要使用假插件org.eclipse.m2e.lifecycle-mapping
来添加maven-antrun-plugin
生命周期,见life cycle mapping documentation。
更改maven-antrun-plugin
输出目录:问题是m2e-wtp
在启动任何生命周期之前获取其网络资源,因此,在maven-antrun-plugin
可以重命名之前文件。作为解决方法,我们创建一个配置文件,仅在项目由 m2e
构建时激活,以更改用于设置 maven-antrun-plugin
输出目录的 属性,直接写入 m2e-wtp
网络资源。
就是这样! POM 片段:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<properties>
<!-- properties used to rename css files with version number. -->
<css.version>${project.version}</css.version>
<!-- Temp directory where to copy renamed files, later added by maven-war-plugin as web-resources. -->
<rename.tmp.directory>${project.build.directory}/rename_tmp</rename.tmp.directory>
</properties>
<!-- There is a problem when running the webapp from Eclipse: m2e-wtp acquires
the web-resources before any lifecycle can be launched, so, before
maven-antrun-plugin can rename the files. We define a profile so that
maven-antrun-plugin copies files directly into the m2e-wtp web-resources directory,
when running from Eclipse. -->
<profiles>
<profile>
<id>m2e</id>
<!-- This profile is only active when the property "m2e.version"
is set, which is the case when building in Eclipse with m2e,
see -->
<activation>
<property>
<name>m2e.version</name>
</property>
</activation>
<properties>
<rename.tmp.directory>${project.build.directory}/m2e-wtp/web-resources/</rename.tmp.directory>
</properties>
</profile>
</profiles>
...
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>run</goal>
</goals>
<id>rename-resources</id>
<!-- perform copy before the package phase,
when maven-war-plugin builds the WAR file -->
<phase>process-resources</phase>
<configuration>
<target>
<!-- copy renamed files. -->
<copy todir="${rename.tmp.directory}/css/">
<fileset dir="src/main/webapp/css/">
<include name="**/*.css" />
</fileset>
<!-- See other Mappers available at http://ant.apache.org/manual/Types/mapper.html -->
<mapper type="glob" from="*.css" to="*-${css.version}.css"/>
</copy>
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<!-- We do no let the maven-war-plugin take care of files that will be renamed.
Paths defined relative to warSourceDirectory (default is ${basedir}/src/main/webapp) -->
<warSourceExcludes>css/</warSourceExcludes>
<webResources>
<!-- include the resources renamed by maven-antrun-plugin,
at the root of the WAR file -->
<resource>
<directory>${rename.tmp.directory}</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
...
</plugins>
<!-- When running server from Eclipse, we need to tell m2e to execute
maven-antrun-plugin to rename files, by default it doesn't. We need to modify the life cycle mapping. -->
<pluginManagement>
<plugins>
<!-- This plugin is not a real one, it is only used by m2e to obtain
config information. This is why it needs to be put in the section
pluginManagement, otherwise Maven would try to download it. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<versionRange>[1.0.0,)</versionRange>
<goals>
<goal>run</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute>
<!-- set to true, otherwise changes are not seen,
e.g., to a css file, and you would need to perform
a project update each time. -->
<runOnIncremental>true</runOnIncremental>
</execute >
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
...
</build>
...
</project>
我想在使用 maven-war-plugin 和版本号构建我的 WAR 文件时重命名静态文件。例如,在我的项目中我有一个文件:
src/main/webapp/css/my.css
我希望它在 WAR 文件中显示为:
css/my-versionNumber.css
已经有人问过同样的问题(参见 Rename static files on maven war build),但公认的答案是重命名目录。我不想重命名目录,我想重命名实际文件。
这可能吗?
你需要定义
<properties>
<css.version>1.1.0</css.version>
</properties>
之后用EL调用 css/${css.version}.css
OK,找到方法了,原理来自:Automatic update of generated css files via m2e。
正在重命名文件
详细信息我选择使用Maven AntRun Plugin, because it allows to rename several files using a replacement pattern, see 。
替代解决方案是使用:
maven-assembly-plugin
:但不支持重命名文件集,仅支持重命名单个文件(参见file assembly descriptor and comments of this answer: )maven-resources-plugin
:它允许复制资源,而不是重命名它们(参见copy resources mojo and comments of this question: Renaming resources in Maven)
使重命名的文件可用于
maven-war-plugin
复制文件:这个想法是将重命名的文件复制到一个临时目录中,由
maven-war-plugin
添加到 WAR 文件作为网络资源。maven-war-plugin
在packaging
阶段构建 WAR,因此我们需要在此之前复制重命名的文件。防止
maven-war-plugin
管理被maven-antrun-plugin
重命名的文件:这是通过使用参数warSourceExcludes
.
使用 m2e-wtp
使其在 Eclipse 中运行更改生命周期映射:问题是
m2e
默认情况下不会执行POM文件中定义的所有生命周期(查看生命周期executed/ignored,来自Eclipse,转到您的项目属性,然后Maven
>Lifecycle Mapping
)。所以你需要使用假插件org.eclipse.m2e.lifecycle-mapping
来添加maven-antrun-plugin
生命周期,见life cycle mapping documentation。更改
maven-antrun-plugin
输出目录:问题是m2e-wtp
在启动任何生命周期之前获取其网络资源,因此,在maven-antrun-plugin
可以重命名之前文件。作为解决方法,我们创建一个配置文件,仅在项目由m2e
构建时激活,以更改用于设置maven-antrun-plugin
输出目录的 属性,直接写入m2e-wtp
网络资源。
就是这样! POM 片段:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<properties>
<!-- properties used to rename css files with version number. -->
<css.version>${project.version}</css.version>
<!-- Temp directory where to copy renamed files, later added by maven-war-plugin as web-resources. -->
<rename.tmp.directory>${project.build.directory}/rename_tmp</rename.tmp.directory>
</properties>
<!-- There is a problem when running the webapp from Eclipse: m2e-wtp acquires
the web-resources before any lifecycle can be launched, so, before
maven-antrun-plugin can rename the files. We define a profile so that
maven-antrun-plugin copies files directly into the m2e-wtp web-resources directory,
when running from Eclipse. -->
<profiles>
<profile>
<id>m2e</id>
<!-- This profile is only active when the property "m2e.version"
is set, which is the case when building in Eclipse with m2e,
see -->
<activation>
<property>
<name>m2e.version</name>
</property>
</activation>
<properties>
<rename.tmp.directory>${project.build.directory}/m2e-wtp/web-resources/</rename.tmp.directory>
</properties>
</profile>
</profiles>
...
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>run</goal>
</goals>
<id>rename-resources</id>
<!-- perform copy before the package phase,
when maven-war-plugin builds the WAR file -->
<phase>process-resources</phase>
<configuration>
<target>
<!-- copy renamed files. -->
<copy todir="${rename.tmp.directory}/css/">
<fileset dir="src/main/webapp/css/">
<include name="**/*.css" />
</fileset>
<!-- See other Mappers available at http://ant.apache.org/manual/Types/mapper.html -->
<mapper type="glob" from="*.css" to="*-${css.version}.css"/>
</copy>
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<!-- We do no let the maven-war-plugin take care of files that will be renamed.
Paths defined relative to warSourceDirectory (default is ${basedir}/src/main/webapp) -->
<warSourceExcludes>css/</warSourceExcludes>
<webResources>
<!-- include the resources renamed by maven-antrun-plugin,
at the root of the WAR file -->
<resource>
<directory>${rename.tmp.directory}</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
...
</plugins>
<!-- When running server from Eclipse, we need to tell m2e to execute
maven-antrun-plugin to rename files, by default it doesn't. We need to modify the life cycle mapping. -->
<pluginManagement>
<plugins>
<!-- This plugin is not a real one, it is only used by m2e to obtain
config information. This is why it needs to be put in the section
pluginManagement, otherwise Maven would try to download it. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<versionRange>[1.0.0,)</versionRange>
<goals>
<goal>run</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute>
<!-- set to true, otherwise changes are not seen,
e.g., to a css file, and you would need to perform
a project update each time. -->
<runOnIncremental>true</runOnIncremental>
</execute >
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
...
</build>
...
</project>