使用Maven war插件的Web资源过滤在使用m2e的Eclipse中不起作用 [英] Web resources filtering with Maven war plugin does not work in Eclipse with m2e

查看:179
本文介绍了使用Maven war插件的Web资源过滤在使用m2e的Eclipse中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Maven过滤来过滤一个Spring配置文件。我的POM配置如下:

  ... 
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-war-plugin< / artifactId>
< version> 2.2< / version>
< configuration>
< webResources>
< resource>
< filtering> true< / filtering>
< targetPath> WEB-INF / context< / targetPath>
< directory> src / main / webapp / WEB-INF / context< / directory>
< includes>
< include> applicationContext.xml< / include>
< / includes>
< / resource>
< / webResources>
< / configuration>
< / plugin>
...

 <个人资料> 
<个人资料>
< id> desarrollo< / id>
< activate>
< activeByDefault> true< / activeByDefault>
< / activation>
< build>
< filters>
< filter> src / main / properties / dev.properties< / filter>
< / filters>
< / build>
< / profile>
<个人资料>
< id> pruebas< / id>
< build>
< filters>
< filter> src / main / properties / test.properties< / filter>
< / filters>
< / build>
< / profile>
...

直接调用Maven时效果很好。



不幸的是,当使用Eclipse WTP和m2e在Tomcat 6中部署webapp时,它总是选择未过滤版本的applicationContext.xml。 (文件夹target / m2e-wtp / web-resources / WEB-INF / context中的文件applicationContext.xml从不被过滤)



我找不到任何有用的关于这个问题的文件。我不确定是否在m2e中实现。



我的配置有问题或者这是未实现的功能?

解决方案

嗯,最后我得到了。



首先,我做了什么khmarbaise指出。我将applicationContext.xml移动到资源文件夹。战争插件webResources旨在与外部资源协同工作,并且目标文件夹本身的文件过滤不是最佳做法。
我更新了POM以反映新配置

 < resources> 
< resource>
< directory> src / main / resources< / directory>
< filtering> true< / filtering>
< / resource>
< / resources>

 < plugin> 
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-war-plugin< / artifactId>
< version> 2.2< / version>
< configuration>
< webResources>
< resource>
< filtering> true< / filtering>
< targetPath> WEB-INF / context< / targetPath>
< directory> src / main / resources / WEB-INF / context< / directory>
< includes>
< include> applicationContext.xml< / include>
< / includes>
< / resource>
< / webResources>
< / configuration>
< / plugin>

所以,他的一半信用。
但这还不够,它还是没有起作用。
我意识到Maven / m2e确实过滤了我的文件,但没有得到我定义的属性文件。经过一些测试,我发现 m2e忽略了配置文件激活部分中的 activeByDefault 选项



所以,我将默认配置文件添加到项目Maven配置中,然后工作




I'm trying to filter a Spring configuration file using Maven filtering. My POM is configured like this:

        ...
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-war-plugin</artifactId>
          <version>2.2</version>
          <configuration>
            <webResources>
              <resource>
                <filtering>true</filtering>
                <targetPath>WEB-INF/context</targetPath>
                <directory>src/main/webapp/WEB-INF/context</directory>
                <includes>
                  <include>applicationContext.xml</include>
                </includes>
              </resource>
            </webResources>
          </configuration>
        </plugin>
        ...

and

  <profiles>
    <profile>
        <id>desarrollo</id>
         <activation>
          <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <filters>
              <filter>src/main/properties/dev.properties</filter>
            </filters>
      </build>
    </profile>
    <profile>
        <id>pruebas</id>
        <build>
            <filters>
              <filter>src/main/properties/test.properties</filter>
            </filters>
      </build>
    </profile>
            ...

It works great when invoking Maven directly.

Unfortunately, when hot-deploying the webapp in Tomcat 6 with Eclipse WTP and m2e it always picks the unfiltered version of applicationContext.xml. (The file applicationContext.xml in the folder target/m2e-wtp/web-resources/WEB-INF/context is never filtered)

I can't find any useful documentation on the subject. I'm not even sure if it is implemented in m2e.

Is something wrong with my configuration or this is an unimplemented feature?

解决方案

Well, finally I got it.

First of all, I did what khmarbaise pointed out. I moved applicationContext.xml to the resources folder. War plugin webResources are meant to work with external resources, and filtering a file in the destination folder itself was not the best practice. I updated the POM to reflect the new configuration

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>

and

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <webResources>
                    <resource>
                        <filtering>true</filtering>
                        <targetPath>WEB-INF/context</targetPath>
                        <directory>src/main/resources/WEB-INF/context</directory>
                        <includes>
                            <include>applicationContext.xml</include>
                        </includes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>

So, half of the credit for him. But that's was not enough, it still didn't worked. I realized that Maven/m2e was indeed filtering my file, but it didn't get my defined properties files. After some testing I found out that m2e is ignoring the activeByDefault option in the profiles activation section.

So, I added my default profile to the project Maven configuration and then it worked

这篇关于使用Maven war插件的Web资源过滤在使用m2e的Eclipse中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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