首页 文章

使用maven war插件的Web资源过滤在使用m2e的Eclipse中不起作用

提问于
浏览
22

我正在尝试使用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>
        ...

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

它直接调用Maven时效果很好 .

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

我找不到有关该主题的任何有用文档 . 我甚至不确定它是否在m2e中实现 .

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

6 回答

  • 26

    好吧,最后我明白了 .

    首先,我做了khmarbaise所指出的 . 我将applicationContext.xml移动到resources文件夹 . War插件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>
    

    所以,他的一半功劳 . 但是's was not enough, it still didn' t工作了 . 我意识到Maven / m2e确实正在过滤我的文件,但它没有得到我定义的属性文件 . 经过一些测试后,我在配置文件激活部分找到了 m2e is ignoring the activeByDefault option .

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

    enter image description here

  • 9

    我在过滤web.xml时遇到了类似的问题 . 我通过在eclipse中重新导入整个项目来解决问题 .

    原因是损坏的/.settings/org.eclipse.wst.common.component文件 . 在此文件中,定义了复制到本地Web服务器部署目录的文件的顺序 . 例如:

    <?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
      <wb-module deploy-name="liquidvote-rest">
        <wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
        <wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
        <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
        <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
        <property name="context-root" value="myapp"/>
        <property name="java-output-path" value="/myapp/target/classes"/>
      </wb-module>
    </project-modules>
    

    如果web.xml或application.xml存在于多个目录中,则将从找到的第一个目录中获取 . 因此重要的是

    <wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
    

    是第一个条目 .

    您可以在http://wiki.eclipse.org/M2E-WTP_FAQhttp://wiki.eclipse.org/M2E-WTP_FAQ找到更多信息 .

  • 0

    您是否尝试将资源放在src / main / resources / WEB-INF / ...下,而不是将资源区域配置为filter the resources,而不是将配置放入非默认的maven位置 .

  • 1

    我刚遇到类似的问题 . 我的解决方案并不优雅,我并不为此感到自豪,但让我们说这是可以接受的 . 在我的情况下,我有一个带有swagger应用程序的spring boot mvc应用程序(供测试人员测试我们的API) . 问题是我们在两个环境中使用这个应用程序,所以我创建了两个配置文件 - 开发和测试 . 在开发环境中,我们想通过“运行方式”从eclipse启动应用程序,因此上下文路径是空白的(我知道它可以在spring java config中设置,但它不是我们想要切换的唯一占位符)并且测试env应用程序在我们的自定义版本的tomcat中运行...因此上下文路径与war文件名相同 .

    这就是问题 - 招摇是在这个上下文路径上调用rest docs,它取决于spring profile . 所以我们有一个需要过滤的网络资源 . 起初我尝试使用m2e-wtp过滤:

    ...
    <build>
    <plugins>
    <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>**/scripts.js</include>
                                </includes>
                            </resource>
                        </webResources>
                    </configuration>
                </plugin>
    ...
    

    它工作但只能在嵌入式eclipse tomcat中运行,或者从命令行java -jar运行,它不能与eclipse中的“run as”一起使用 .

    资源被过滤了,我在目标网络资源中看到了它们,但是eclipse似乎直接在源代码上运行,或者正在制作副本我不知道......它看不到过滤后的资源......所以我认为我会做这样的事情:

    <resources>
                <resource>
                    <directory>src/main/resources</directory>
                </resource>
                <resource>
                    <filtering>true</filtering>
                    <directory>src/main/resources/webapp/swagger</directory>
                    <targetPath>${basedir}/src/main/webapp/swagger</targetPath>
                    <includes>
                        <include>scripts.js</include>
                    </includes>
                </resource>
            </resources>
    

    这不是最幸运的解决方案,因为它是修改代码而不是目标,但至少它是有效的 . 如果有人会有任何建议如何让它在没有代码修改的情况下工作,我会很有帮助 .

  • 0

    今天,当几个 jar 被纳入战争时,我遇到了类似的问题 . 我设置过滤,并将原始 jar 与过滤器进行比较 . 他们似乎是一样的,但事实并非如此 . 我试图将jar重命名为zip并且由于jar(zip)内部的结构损坏而无法解压缩内容,而原始的一个没问题 . 这里也提到adding filtering web resources其中说过滤必须设置为false以防止破坏二进制文件 .

  • 0

    我已经尝试了上述所有内容但没有成功 . 这些文件在“m2e-wtp”文件夹中被过滤并正确生成,但出于某种原因,Eclipse正在从“target / classes”复制文件 .

    因此,我改变了我的pom.xml,将目标文件夹从“m2e-wtp”更改为“target / classes”,如代码提前 .

    重要提示:每次需要在项目上运行maven构建时,必须更改pom才能构建项目 .

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>  
        <configuration>
            <failOnMissingWebXml>false</failOnMissingWebXml>
            <filters>
                <filter>${project.basedir}/src/main/filters/${build.profile.id}/config.properties</filter>
            </filters>
            <webResources>
                <resource>
                    <filtering>true</filtering>
                    <directory>${project.basedir}/src/main/resources</directory>
                    <targetPath>${project.basedir}/target/classes</targetPath>
                    <includes>
                        <include>*.properties</include>
                        <include>*.xml</include>
                        <include>META-INF/spring/*.xml</include>
                    </includes>
                </resource>
            </webResources>
        </configuration>
    </plugin>
    

相关问题