在 WAR 打包前使用 Maven Replacer Plugin [英] Use Maven Replacer Plugin Before WAR is Packaged

查看:77
本文介绍了在 WAR 打包前使用 Maven Replacer Plugin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 pom.xml :

<modelVersion>4.0.0</modelVersion><groupId>com.paper</groupId><artifactId>和</artifactId><version>1.0</version><包装>战争</包装><name>with</name><依赖项><依赖><groupId>javax</groupId><artifactId>javaee-web-api</artifactId><version>7.0</version><范围>提供</范围></依赖><!-- 应用程序--><依赖><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>5.0.7.Final</version></依赖><依赖><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><!-- <scope>test</scope>--></依赖><依赖><groupId>org.apache.struts</groupId><artifactId>struts-core</artifactId><version>1.3.10</version></依赖><依赖><groupId>org.apache.struts</groupId><artifactId>struts-extras</artifactId><version>1.3.10</version></依赖><!-- FIN申请--><!-- JSP TLD --><依赖><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></依赖><依赖><groupId>org.apache.struts</groupId><artifactId>struts-taglib</artifactId><version>1.3.10</version></依赖><依赖><groupId>org.apache.struts</groupId><artifactId>struts-el</artifactId><version>1.3.10</version></依赖><依赖><groupId>org.apache.struts</groupId><artifactId>struts-tiles</artifactId><version>1.3.10</version></依赖><!-- FIN JSP TLD --><!-- 伯特--><依赖><groupId>org.eclipse.birt.runtime</groupId><artifactId>org.eclipse.birt.runtime</artifactId><version>4.3.0</version><排除事项><排除><artifactId>org.apache.poi</artifactId><groupId>org.eclipse.birt.runtime</groupId></排除></排除项></依赖><依赖><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.9</version></依赖><!-- FIN Birt --><!-- 潜水员--><依赖><groupId>net.fckeditor</groupId><artifactId>java-core</artifactId><version>2.4.2</version></依赖><依赖><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.5.6</version></依赖><依赖><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.5.6</version></依赖><依赖><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></依赖><依赖><groupId>com.novell.ldap</groupId><artifactId>jldap</artifactId><version>2009-10-07</version></依赖><依赖><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.4</version><classifier>jdk15</classifier></依赖><依赖><groupId>javax.mail</groupId><artifactId>javax.mail-api</artifactId><version>1.5.5</version></依赖><依赖><groupId>com.lowagie</groupId><artifactId>itext</artifactId><version>4.2.2</version></依赖><依赖><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId><version>2.10.1</version></依赖><依赖><groupId>jaxen</groupId><artifactId>jaxen</artifactId><version>1.1.6</version></依赖></依赖项><!-- FIN 潜水员--><!-- 个人资料--><属性><majorVersion>2</majorVersion><minorVersion>50.3-0</minorVersion><applicationShortname>Lyreco</applicationShortname><applicationLongname>Lyreco</applicationLongname></属性><个人资料><个人资料><id>dev</id><激活><activeByDefault>true</activeByDefault></激活><属性><profileVersion>DEV</profileVersion><webXmlfolder>dev</webXmlfolder><mode>-开发</mode></属性></个人资料><个人资料><id>前体</id><属性><profileVersion>PREPROD</profileVersion><webXmlfolder>preprod</webXmlfolder><mode>-预生产</mode></属性></个人资料><个人资料><id>产品</id><属性><profileVersion>PROD</profileVersion><webXmlfolder>prod</webXmlfolder></属性></个人资料></个人资料><!-- FIN 配置文件 --><构建><finalName>${applicationShortname}_${majorVersion}_${minorVersion}_${profileVersion}</finalName><插件><!-- ANT 插件首次亮相--><插件><artifactId>maven-antrun-plugin</artifactId><version>1.7</version><执行><执行><phase>包</phase><配置><目标><移动file="${project.build.directory}/classes/log4j.${webXmlfolder}.properties"tofile="${project.build.directory}/classes/log4j.properties"覆盖=真"/><移动file="${project.build.directory}/classes/hibernate.cfg.${webXmlfolder}.xml"tofile="${project.build.directory}/classes/hibernate.cfg.xml"覆盖=真"/><移动file="${project.build.directory}/classes/com/paper/with/properties/enumerations.${webXmlfolder}.properties"tofile="${project.build.directory}/classes/com/paper/with/properties/enumerations.properties"覆盖=真"/><移动file="${project.build.directory}/classes/com/paper/with/properties/MessageResources.${webXmlfolder}.properties"tofile="${project.build.directory}/classes/com/paper/with/properties/MessageResources.properties"覆盖=真"/><移动file="${project.build.directory}/classes/com/paper/with/properties/Parametres.${webXmlfolder}.properties"tofile="${project.build.directory}/classes/com/paper/with/properties/Parametres.properties"覆盖=真"/><删除><fileset dir="${project.build.directory}/classes/"包括="**/hibernate.cfg.*.xml"/><fileset dir="${project.build.directory}/classes/"包括="**/log4j.*.properties"/><文件集dir="${project.build.directory}/classes/com/paper/with/properties/"include="**/enumerations.*.properties"/><文件集dir="${project.build.directory}/classes/com/paper/with/properties/"包括="**/MessageResources.*.properties"/><文件集dir="${project.build.directory}/classes/com/paper/with/properties/"include="**/Parametres.*.properties"/></删除></目标></配置><目标><目标>跑</目标></目标></执行></执行></插件><!-- ANT 插件 FIN --><插件><artifactId>maven-war-plugin</artifactId><version>2.2</version><执行><执行><id>default-war</id><phase>none</phase><目标><目标>战争</目标></目标></执行><执行><id>package-war</id><phase>包</phase><目标><目标>战争</目标></目标></执行></执行></插件><!-- Maven 替换插件首次亮相--><插件><groupId>com.google.code.maven-replacer-plugin</groupId><artifactId>replacer</artifactId><version>1.5.2</version><执行><执行><phase>包</phase><目标><目标>替换</目标></目标></执行></执行><配置><ignoreMissingFile>false</ignoreMissingFile><file>${project.build.directory}/${applicationShortname}_${majorVersion}_${minorVersion}_${profileVersion}/WEB-INF/web.xml</file><替代品><更换><token>@majorVersion@</token><value>${majorVersion}</value></替换><更换><token>@minorVersion@</token><value>${minorVersion}</value></替换><更换><token>@mode@</token><value>${mode}</value></替换></替代品></配置></插件><!-- Maven 替换插件 FIN --><插件><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><配置><来源>1.8</来源><目标>1.8</目标><存档><manifestEntries><Specification-Title>J2EE Servlet</Specification-Title><规范版本>2.5</规范版本><规范-供应商>Sun Microsystems</规范-供应商><Implementation-Title>${project.name}</Implementation-Title><Implementation-Version>${project.version}</Implementation-Version><实施供应商>${project.organization.name}</实施供应商><Built-By>me</Built-By><Implementation-URL>${project.url}</Implementation-URL></manifestEntries></存档><failOnMissingWebXml>false</failOnMissingWebXml></配置></插件><插件><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><version>2.6</version><执行><执行><阶段>验证</阶段><目标><目标>复制</目标></目标><配置><outputDirectory>${endorsed.dir}</outputDirectory><silent>true</silent><artifactItems><artifactItem><groupId>javax</groupId><artifactId>javaee-endorsed-api</artifactId><version>7.0</version><type>jar</type></artifactItem></artifactItems></配置></执行></执行></插件></plugins></build></项目>

我的输出控制台:

D:\workspace\with>mvn 全新安装[信息] 正在扫描项目...[信息][信息] ------------------------------------------------------------------------[信息] 使用 1.0 构建[信息] ------------------------------------------------------------------------[警告] 缺少 milyn:flute:jar:1.3 的 POM,没有可用的依赖信息[警告] 工件 com.lowagie:itext:jar:4.2.2 已重定位到 com.itextpdf:itextpdf:jar:5.5.6[信息][信息] --- maven-clean-plugin:2.5:clean (default-clean) @ with ---[信息] 删除 D:\workspace\with\target[信息][信息] --- maven-dependency-plugin:2.6:copy (default) @ with ---[信息][信息] --- maven-resources-plugin:2.6:resources (default-resources) @ with ---[警告] 使用平台编码(实际上是 Cp1252)复制过滤的资源,即构建依赖于平台![INFO] 复制 63 个资源[信息][信息] --- maven-compiler-plugin:3.1:compile (default-compile) @ with ---[信息] 检测到更改 - 重新编译模块![警告] 未设置文件编码,使用平台编码 Cp1252,即构建依赖于平台![信息] 将 1629 个源文件编译到 D:\workspace\with\target\classes[警告]/D:/workspace/with/src/main/java/com/paper/with/authentification/AuthentificationManager.java:[8,16] sun.misc.BASE64Encoder 是内部专有 API,可能会在未来版本中删除[警告]/D:/workspace/with/src/main/java/com/paper/with/presentation/acompte/RapprochementFormInitializer.java:[81,38] com.sun.corba.se.spi.extension.ZeroPortPolicy 是内部专有 API,可能会在未来版本中删除[警告]/D:/workspace/with/src/main/java/com/paper/with/presentation/retrocession/AffichageRetrocessionPrestationDispatchAction.java:[76,53] com.sun.corba.se.impl.protocol.giopmsgheaders.Message 是内部专有 API,可能是在未来版本中删除[警告]/D:/workspace/with/src/main/java/com/paper/with/service/importation/article/ImportationQuantiteArticle.java:[16,26] sun.misc.resources.Messages 是内部专有 API,可能会在未来版本中删除[警告]/D:/workspace/with/src/main/java/com/paper/with/authentification/AuthentificationManager.java:[156,29] sun.misc.BASE64Encoder 是内部专有 API,可能会在未来版本中删除[警告]/D:/workspace/with/src/main/java/com/paper/with/presentation/declaration/saisieCARealise/RechercheCARealiseDispatchAction.java:某些输入文件使用或覆盖已弃用的 API.[警告]/D:/workspace/with/src/main/java/com/paper/with/presentation/declaration/saisieCARealise/RechercheCARealiseDispatchAction.java:使用 -Xlint:deprecation 重新编译以获取详细信息.[警告]/D:/workspace/with/src/main/java/com/paper/with/persistence/hibernate/dao/HibernateDAO.java: 一些输入 f文件使用未经检查或不安全的操作.[警告]/D:/workspace/with/src/main/java/com/paper/with/persistence/hibernate/dao/HibernateDAO.java:重新编译with -Xlint:未选中详细信息.[信息][信息] --- maven-resources-plugin:2.6:testResources (default-testResources) @ with ---[警告] 使用平台编码(实际上是 Cp1252)复制过滤的资源,即构建依赖于平台![信息] 跳过不存在的资源目录 D:\workspace\with\src\test\resources[信息][信息] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ with ---[信息] 无需编译 - 所有类都是最新的[信息][信息] --- maven-surefire-plugin:2.12.4:test (default-test) @ with ---[信息] 没有要运行的测试.[信息][信息] --- maven-antrun-plugin:1.7:run (default) @ with ---[INFO] 执行任务主要的:[移动] 将 1 个文件移动到 D:\workspace\with\target\classes[移动] 将 1 个文件移动到 D:\workspace\with\target\classes[移动] 将 1 个文件移动到 D:\workspace\with\target\classes\com\paper\with\properties[移动] 将 1 个文件移动到 D:\workspace\with\target\classes\com\paper\with\properties[移动] 将 1 个文件移动到 D:\workspace\with\target\classes\com\paper\with\properties[INFO] 已执行的任务[信息][信息] --- maven-war-plugin:2.2:war (package-war) @ with ---[INFO] 打包 webapp[信息] 在 [D:\workspace\with\target\Lyreco_2_50.3-0_DEV] 中组装 webapp [with][INFO]处理战争项目[信息] 复制 webapp 资源 [D:\workspace\with\src\main\webapp][信息] Webapp 在 [16254 毫秒] 内组装[信息] 建筑战争:D:\workspace\with\target\Lyreco_2_50.3-0_DEV.war[INFO] WEB-INF\web.xml 已经添加,跳过[信息][信息] --- 替换器:1.5.2:替换(默认)@ with ---[信息] 替换在 1 个文件上运行.[信息][信息] --- maven-install-plugin:2.4:install (default-install) @ with ---[信息] 安装 D:\workspace\with\target\Lyreco_2_50.3-0_DEV.war 到 C:\Users\me\.m2\repository\com\paper\with\1.0\with-1.0.war[信息] 安装 D:\workspace\with\pom.xml 到 C:\Users\me\.m2\repository\com\paper\with\1.0\with-1.0.pom[信息] ------------------------------------------------------------------------[信息] 构建成功[信息] ------------------------------------------------------------------------[信息] 总时间:41.632 秒[INFO] 完成时间:2016-03-24T11:54:10+01:00[INFO] 最终内存:50M/765M[信息] ------------------------------------------------------------------------

我的问题是我修改了我的 web.xml.一切似乎都在工作,除了修改发生在在构建战争文件之后.

解决方案

这是正常的:maven-war-plugin 有一个默认的 default-war 执行是在绑定到 package 阶段的所有其他插件之前执行.一个直接的解决方案是将 maven-replacer-plugin 绑定到 prepare-package 阶段和 prepare-package> 在其配置中.

但我要强调的是你做错了™.

在 Maven 术语中,您需要在这里过滤 web.xml.maven-war-plugin 已经通过使用

配置它来支持

true

过滤意味着在构建时用一个值替换占位符.在这种情况下,我们要替换 @majorVersion@@minorVersion@@mode@.

因此创建一个文件src/main/filters/filter.properties,例如,内容

majorVersion=2次要版本=50.3-0模式=- 模式

然后你可以配置maven-war-plugin 包含以下内容:

<预><代码><配置><过滤器><filter>src/main/filters/filter.properties</filter></过滤器><filteringDeploymentDescriptors>true</filteringDeploymentDescriptors><网络资源><资源><目录>/path/to/other/files/to/filter</directory><包括><include>MessageResources.properties</include></包括><filtering>true</filtering><targetPath>[...]</targetPath></资源></webResources></配置>

这将替换 ${majorVersion}${minorVersion}${mode}web.xml 与属性文件中定义的值.请注意,这还添加了一个 webResource 以启用对 WAR 中嵌入的其他资源的过滤,并且该资源将放置在指定的 targetPath 下.

<小时>

我注意到,在您当前的 POM 中,mode 的值取决于配置文件.在这种情况下,您可以创建 4 个过滤器文件:

  • src/main/filters/filter.properties 将包含要过滤的常用属性
  • src/main/filters/filter-dev.properties 将包含 dev 配置文件特定属性
  • src/main/filters/filter-preprod.properties 将包含 preprod 配置文件特定属性
  • src/main/filters/filter-prod.properties 将包含 prod 配置文件特定属性

然后,在您的 部分,您可以添加:

<id>[...]</id><!-- 其余配置不变--><属性><filterFile>src/main/filters/filter-[...].properties</filterFile></属性></个人资料>

最后用

定义maven-war-plugin<预><代码><配置><过滤器><filter>src/main/filters/filter.properties</filter><filter>${filterFile}</filter></过滤器><filteringDeploymentDescriptors>true</filteringDeploymentDescriptors></配置>

This is my pom.xml :

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.paper</groupId>
    <artifactId>with</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <name>with</name>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- Applicatif -->

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.7.Final</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <!-- <scope>test</scope> -->
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts-core</artifactId>
            <version>1.3.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts-extras</artifactId>
            <version>1.3.10</version>
        </dependency>

        <!-- FIN Applicatif -->

        <!-- JSP TLD -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts-taglib</artifactId>
            <version>1.3.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts-el</artifactId>
            <version>1.3.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts-tiles</artifactId>
            <version>1.3.10</version>
        </dependency>

        <!-- FIN JSP TLD -->

        <!-- Birt -->

        <dependency>
            <groupId>org.eclipse.birt.runtime</groupId>
            <artifactId>org.eclipse.birt.runtime</artifactId>
            <version>4.3.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>org.apache.poi</artifactId>
                    <groupId>org.eclipse.birt.runtime</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>

        <!-- FIN Birt -->


        <!-- Divers -->
        <dependency>
            <groupId>net.fckeditor</groupId>
            <artifactId>java-core</artifactId>
            <version>2.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.5.6</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.5.6</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>com.novell.ldap</groupId>
            <artifactId>jldap</artifactId>
            <version>2009-10-07</version>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.5.5</version>
        </dependency>
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>4.2.2</version>
        </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.1</version>
        </dependency>
        <dependency>
            <groupId>jaxen</groupId>
            <artifactId>jaxen</artifactId>
            <version>1.1.6</version>
        </dependency>

    </dependencies>

    <!-- FIN Divers -->

    <!-- Profiles -->

    <properties>
        <majorVersion>2</majorVersion>
        <minorVersion>50.3-0</minorVersion>
        <applicationShortname>Lyreco</applicationShortname>
        <applicationLongname>Lyreco</applicationLongname>
    </properties>

    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <profileVersion>DEV</profileVersion>
                <webXmlfolder>dev</webXmlfolder>
                <mode>- Developpements</mode>
            </properties>
        </profile>

        <profile>
            <id>preprod</id>
            <properties>
                <profileVersion>PREPROD</profileVersion>
                <webXmlfolder>preprod</webXmlfolder>
                <mode>- Pre-Production</mode>
            </properties>
        </profile>

        <profile>
            <id>prod</id>
            <properties>
                <profileVersion>PROD</profileVersion>
                <webXmlfolder>prod</webXmlfolder>
            </properties>
        </profile>
    </profiles>

    <!-- FIN Profiles -->

    <build>
        <finalName>${applicationShortname}_${majorVersion}_${minorVersion}_${profileVersion}</finalName>
        <plugins>

        <!-- ANT Plugin DEBUT -->
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <configuration>
                            <target>
                                <move
                                    file="${project.build.directory}/classes/log4j.${webXmlfolder}.properties"
                                    tofile="${project.build.directory}/classes/log4j.properties"
                                    overwrite="true" />

                                <move
                                    file="${project.build.directory}/classes/hibernate.cfg.${webXmlfolder}.xml"
                                    tofile="${project.build.directory}/classes/hibernate.cfg.xml"
                                    overwrite="true" />

                                <move
                                    file="${project.build.directory}/classes/com/paper/with/properties/enumerations.${webXmlfolder}.properties"
                                    tofile="${project.build.directory}/classes/com/paper/with/properties/enumerations.properties"
                                    overwrite="true" />

                                <move
                                    file="${project.build.directory}/classes/com/paper/with/properties/MessageResources.${webXmlfolder}.properties"
                                    tofile="${project.build.directory}/classes/com/paper/with/properties/MessageResources.properties"
                                    overwrite="true" />

                                <move
                                    file="${project.build.directory}/classes/com/paper/with/properties/Parametres.${webXmlfolder}.properties"
                                    tofile="${project.build.directory}/classes/com/paper/with/properties/Parametres.properties"
                                    overwrite="true" />

                                <delete>
                                    <fileset dir="${project.build.directory}/classes/"
                                        includes="**/hibernate.cfg.*.xml" />
                                    <fileset dir="${project.build.directory}/classes/"
                                        includes="**/log4j.*.properties" />
                                    <fileset
                                        dir="${project.build.directory}/classes/com/paper/with/properties/"
                                        includes="**/enumerations.*.properties" />
                                    <fileset
                                        dir="${project.build.directory}/classes/com/paper/with/properties/"
                                        includes="**/MessageResources.*.properties" />
                                    <fileset
                                        dir="${project.build.directory}/classes/com/paper/with/properties/"
                                        includes="**/Parametres.*.properties" />
                                </delete>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- ANT Plugin FIN -->

            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <id>default-war</id>
                        <phase>none</phase>
                        <goals>
                            <goal>war</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>package-war</id>
                        <phase>package</phase>
                        <goals>
                            <goal>war</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- Maven Replacer Plugin DEBUT -->
            <plugin>
                <groupId>com.google.code.maven-replacer-plugin</groupId>
                <artifactId>replacer</artifactId>
                <version>1.5.2</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>replace</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <ignoreMissingFile>false</ignoreMissingFile>
                    <file>${project.build.directory}/${applicationShortname}_${majorVersion}_${minorVersion}_${profileVersion}/WEB-INF/web.xml</file>
                    <replacements>
                        <replacement>
                            <token>@majorVersion@</token>
                            <value>${majorVersion}</value>
                        </replacement>
                        <replacement>
                            <token>@minorVersion@</token>
                            <value>${minorVersion}</value>
                        </replacement>
                        <replacement>
                            <token>@mode@</token>
                            <value>${mode}</value>
                        </replacement>
                    </replacements>
                </configuration>
            </plugin>
            <!-- Maven Replacer Plugin FIN -->

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <archive>
                        <manifestEntries>
                            <Specification-Title>J2EE Servlet</Specification-Title>
                            <Specification-Version>2.5</Specification-Version>
                            <Specification-Vendor>Sun Microsystems</Specification-Vendor>
                            <Implementation-Title>${project.name}</Implementation-Title>
                            <Implementation-Version>${project.version}</Implementation-Version>
                            <Implementation-Vendor>${project.organization.name}</Implementation-Vendor>
                            <Built-By>me</Built-By>
                            <Implementation-URL>${project.url}</Implementation-URL>
                        </manifestEntries>
                    </archive>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

My output console:

D:\workspace\with>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building with 1.0
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for milyn:flute:jar:1.3 is missing, no dependency information available
[WARNING] The artifact com.lowagie:itext:jar:4.2.2 has been relocated to com.itextpdf:itextpdf:jar:5.5.6
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ with ---
[INFO] Deleting D:\workspace\with\target
[INFO]
[INFO] --- maven-dependency-plugin:2.6:copy (default) @ with ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ with ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 63 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ with ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1629 source files to D:\workspace\with\target\classes
[WARNING] /D:/workspace/with/src/main/java/com/paper/with/authentification/AuthentificationManager.java:[8,16] sun.
misc.BASE64Encoder is internal proprietary API and may be removed in a future release
[WARNING] /D:/workspace/with/src/main/java/com/paper/with/presentation/acompte/RapprochementFormInitializer.java:[8
1,38] com.sun.corba.se.spi.extension.ZeroPortPolicy is internal proprietary API and may be removed in a future release
[WARNING] /D:/workspace/with/src/main/java/com/paper/with/presentation/retrocession/AffichageRetrocessionPrestation
DispatchAction.java:[76,53] com.sun.corba.se.impl.protocol.giopmsgheaders.Message is internal proprietary API and may be
 removed in a future release
[WARNING] /D:/workspace/with/src/main/java/com/paper/with/service/importation/article/ImportationQuantiteArticle.ja
va:[16,26] sun.misc.resources.Messages is internal proprietary API and may be removed in a future release
[WARNING] /D:/workspace/with/src/main/java/com/paper/with/authentification/AuthentificationManager.java:[156,29] su
n.misc.BASE64Encoder is internal proprietary API and may be removed in a future release
[WARNING] /D:/workspace/with/src/main/java/com/paper/with/presentation/declaration/saisieCARealise/RechercheCAReali
seDispatchAction.java: Some input files use or override a deprecated API.
[WARNING] /D:/workspace/with/src/main/java/com/paper/with/presentation/declaration/saisieCARealise/RechercheCAReali
seDispatchAction.java: Recompile with -Xlint:deprecation for details.
[WARNING] /D:/workspace/with/src/main/java/com/paper/with/persistence/hibernate/dao/HibernateDAO.java: Some input f
iles use unchecked or unsafe operations.
[WARNING] /D:/workspace/with/src/main/java/com/paper/with/persistence/hibernate/dao/HibernateDAO.java: Recompile wi
th -Xlint:unchecked for details.
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ with ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\workspace\with\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ with ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ with ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-antrun-plugin:1.7:run (default) @ with ---
[INFO] Executing tasks

main:
     [move] Moving 1 file to D:\workspace\with\target\classes
     [move] Moving 1 file to D:\workspace\with\target\classes
     [move] Moving 1 file to D:\workspace\with\target\classes\com\paper\with\properties
     [move] Moving 1 file to D:\workspace\with\target\classes\com\paper\with\properties
     [move] Moving 1 file to D:\workspace\with\target\classes\com\paper\with\properties
[INFO] Executed tasks
[INFO]
[INFO] --- maven-war-plugin:2.2:war (package-war) @ with ---
[INFO] Packaging webapp
[INFO] Assembling webapp [with] in [D:\workspace\with\target\Lyreco_2_50.3-0_DEV]
[INFO] Processing war project
[INFO] Copying webapp resources [D:\workspace\with\src\main\webapp]
[INFO] Webapp assembled in [16254 msecs]
[INFO] Building war: D:\workspace\with\target\Lyreco_2_50.3-0_DEV.war
[INFO] WEB-INF\web.xml already added, skipping
[INFO]
[INFO] --- replacer:1.5.2:replace (default) @ with ---
[INFO] Replacement run on 1 file.
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ with ---
[INFO] Installing D:\workspace\with\target\Lyreco_2_50.3-0_DEV.war to C:\Users\me\.m2\repository\com\paper
\with\1.0\with-1.0.war
[INFO] Installing D:\workspace\with\pom.xml to C:\Users\me\.m2\repository\com\paper\with\1.0\with-1.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 41.632 s
[INFO] Finished at: 2016-03-24T11:54:10+01:00
[INFO] Final Memory: 50M/765M
[INFO] ------------------------------------------------------------------------

My problem is I have modify my web.xml. Everything appears to be working, except that the modification takes place AFTER the war file is built.

解决方案

This is normal: the maven-war-plugin has a default default-war execution that is executed before all other plugins bound to the package phase. A direct solution would be to bind maven-replacer-plugin to the prepare-package phase with <phase>prepare-package</phase> in its configuration.

But I'll emphasize that you're doing it wrong™.

In Maven terms, what you want here is to filter the web.xml. The maven-war-plugin already supports that by configuring it with

<filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>

Filtering means replacing at build-time a placeholder by a value. In this case, we want to replace @majorVersion@, @minorVersion@ and @mode@.

So create a file src/main/filters/filter.properties for example, with the content

majorVersion=2
minorVersion=50.3-0
mode=- Mode

Then you can configure the maven-war-plugin with the following:

<configuration>
  <filters>
    <filter>src/main/filters/filter.properties</filter>
  </filters>
  <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
  <webResources>
    <resource>
      <directory>/path/to/other/files/to/filter</directory>
      <includes>
        <include>MessageResources.properties</include>
      </includes>
      <filtering>true</filtering>
      <targetPath>[...]</targetPath>
    </resource>
  </webResources>
</configuration>

This will replace every occurence of ${majorVersion}, ${minorVersion} and ${mode} in the web.xml with the values defined in the properties file. Note that this also adds a webResource to enable filtering on another resources embedded in the WAR, and the resource will be placed under the specified targetPath.


I noted that, in your current POM, the value of mode depends on the profile. In this case, you can create 4 filters file:

  • src/main/filters/filter.properties will contain the common properties to filter
  • src/main/filters/filter-dev.properties will contain the dev profile specific property
  • src/main/filters/filter-preprod.properties will contain the preprod profile specific property
  • src/main/filters/filter-prod.properties will contain the prod profile specific property

Then, in your <profile> section, you can add:

<profile>
  <id>[...]</id>
  <!-- rest of configuration untouched -->
  <properties>
    <filterFile>src/main/filters/filter-[...].properties</filterFile>
  </properties>
</profile>

and finally define the maven-war-plugin with

<configuration>
  <filters>
    <filter>src/main/filters/filter.properties</filter>
    <filter>${filterFile}</filter>
  </filters>
  <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
</configuration>

这篇关于在 WAR 打包前使用 Maven Replacer Plugin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆