Maven war插件未按预期过滤

rea*_*ice 4 filtering maven maven-war-plugin

我目前正在使用maven 3.1.1和maven war plugin 2.4(http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html)建立一个web项目.特别是,我正在尝试使用maven war插件复制和过滤资源,这种方式我过去已经完成并且有效.以下是相关的pom配置:

WAR插件配置

...
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1.1</version>
    <configuration>
        ...
        <webResources>
            <resource>
                <directory>src/main/webapp/META-INF</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
                <targetPath>META-INF</targetPath>
            </resource>
            ...
    </configuration>
</plugin>
...
Run Code Online (Sandbox Code Playgroud)

激活的配置文件定义:

...
<profiles>
<profile>
    <id>dummyDev</id>
    <build>
        <filters>
            <filter>filters/development.properties</filter>
        </filters>
    </build>
</profile>
...
Run Code Online (Sandbox Code Playgroud)

META-INF/context.xml的:

<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="${tomcat.context.reloadable}">
</Context>
Run Code Online (Sandbox Code Playgroud)

最后是"development.properties":

tomcat.context.reloadable=true
Run Code Online (Sandbox Code Playgroud)

context.xml被复制到WAR中,但不会被过滤.如果我尝试在配置文件中直接写入属性(即不引用属性文件):它仅在我引用属性文件时才起作用.看起来在读取属性文件时出现了问题.此外,在"打包"后,maven没有告诉我在控制台中找不到属性文件(因此maven找到了文件).

最后我尝试使用maven war插件版本2.1.1(和版本2.3)来实现它的工作:我尝试了更多次只是改变了maven war插件的版本,所以我很确定问题就是这样.

我错过了什么吗?这是maven war插件的已知错误吗?我是否正在尝试以正确的方式达到结果(过滤该文件)?谢谢.

rea*_*ice 9

好吧,我搜索了maven war plugin jira,并且说这实际上是一个bug,在2.4版本中引入(更多内容见http://jira.codehaus.org/browse/MWAR-301).

为方便起见,我引用了jira问题中指出的解决方法:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        ...
        <filters>
            <filter>${project.basedir}/src/main/filters/my-filter.properties</filter>
        </filters>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

尝试在我的皮肤上,它的工作原理.

无论如何,我注意到如果你在这个位置指定一个属性源文件(这是pom级别的war插件声明,而不是配置文件级别),这将始终被读取,即对于你指定的每个构建配置文件.除非你为每个构建配置文件重新声明war插件,并在每个配置文件中指定一组不同的属性文件(一个非常糟糕的解决方案,这里只指出这是错误的方式,为了完整性,显然恕我直言),此解决方案仅限于必须始终读取给定属性文件的情况,无论指定的构建配置文件是什么.