在包装WAR之前使用Maven Replacer插件

时间:2016-03-24 11:06:46

标签: java maven

这是我的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>

我的输出控制台:

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] ------------------------------------------------------------------------

我的问题是我修改了web.xml。一切似乎都在起作用,但修改发生在 AFTER 之后。

2 个答案:

答案 0 :(得分:2)

这是正常的:maven-war-plugin执行默认default-war,在所有其他插件绑定到package阶段之前执行。直接解决方案是将maven-replacer-plugin绑定到prepare-package阶段,并在其配置中使用<phase>prepare-package</phase>

但我会强调你做错了。

在Maven术语中,您希望在此处过滤web.xml。使用

配置maven-war-plugin already supports that
<filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>

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

例如,使用内容

创建一个文件src/main/filters/filter.properties
majorVersion=2
minorVersion=50.3-0
mode=- Mode

然后你可以configure the maven-war-plugin使用以下内容:

<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>

这将使用属性文件中定义的值替换${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个人资料特定属性

然后,在<profile>部分中,您可以添加:

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

最后用

定义maven-war-plugin
<configuration>
  <filters>
    <filter>src/main/filters/filter.properties</filter>
    <filter>${filterFile}</filter>
  </filters>
  <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
</configuration>

答案 1 :(得分:0)

您应该将replacer-plugin绑定到prepare-package阶段,因为生命周期插件总是在特定阶段首先出现。