java - WAR打包前使用Maven Replacer Plugin

标签 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。一切似乎都在工作,除了修改发生在构建 war 文件之后

最佳答案

这是正常的:maven-war-plugin有一个默认值 default-war在绑定(bind)到 package 的所有其他插件之前执行的执行阶段。一个直接的解决方案是绑定(bind) maven-replacer-pluginprepare-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将包含要过滤的公共(public)属性
  • 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>

关于java - WAR打包前使用Maven Replacer Plugin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36198710/

相关文章:

java - 我应该将单元测试期间创建的测试数据文件放在哪里?

java - org.apache.cxf.binding.soap.SoapHeader 无法转换为 org.apache.cxf.binding.soap.SoapHeader

java - 是否有可能从 Eclipse 构建过程中获取准确的 Maven 命令?

Eclipse:令人困惑的添加到构建路径选项

java - 为类(class)成员创建eclipse模板

java - 即使尺寸匹配,Jama 矩阵尺寸也必须同意误差

具有 int 节点的数学树的 Java 结构

java - 使用 Spring 在 Maven 中查找 .html 资源时出现问题

java - 在java中,当函数完成时或在某个特定时间后(即使还没有完成)退出函数

java - 开发应用程序监控仪表板的建议。类似于 Nagios 的东西