如何用Maven属性替换web.xml中的值? [英] How to replace a value in web.xml with a Maven property?

查看:475
本文介绍了如何用Maven属性替换web.xml中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Maven项目,该项目将一些测试文件下载到其构建目录./target/files中.这些文件随后应可用于servlet,我可以通过将完整路径硬编码为servlet的<init-param>来轻松实现:

I have a Maven project that downloads some test files into its build directory ./target/files. These files should then be available to a servlet, which I can easily achieve by hardcoding the full path as an <init-param> of the servlet:

<servlet>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>my.package.TestServlet</servlet-class>
    <init-param>
        <param-name>filepath</param-name>
        <param-value>/home/user/testproject/target/files</param-value>
    </init-param>
</servlet>

如何避免对完整路径进行硬编码,而是使用动态参数替换?我尝试了以下操作,但没有成功:

How can I avoid hardcoding the full path and use a dynamic parameter replacement instead? I tried the following, but it did not work:

<param-value>${project.build.directory}/files</param-value>

推荐答案

添加到您的pom部分:

Add to your pom section:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <webResources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/webapp</directory>
                <includes>
                    <include>**/web.xml</include>
                </includes>
            </resource>
        </webResources>
        <warSourceDirectory>src/main/webapp</warSourceDirectory>
        <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
    </configuration>
</plugin>

有关更多详细信息,请参见行家:自定义Web应用程序项目的web.xml

See Maven: Customize web.xml of web-app project for more details

这篇关于如何用Maven属性替换web.xml中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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