使用 Maven 战争插件过滤 Web 资源在带有 m2e 的 Eclipse 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9801826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 20:58:37  来源:igfitidea点击:

Web resources filtering with Maven war plugin does not work in Eclipse with m2e

eclipsemaveneclipse-wtpm2e

提问by Juan Calero

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

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

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.

直接调用 Maven 时效果很好。

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)

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

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

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

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

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

回答by Juan Calero

Well, finally I got it.

嗯,我终于明白了。

First of all, I did what khmarbaise pointed out. I moved applicationContext.xmlto 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

首先,我做了khmarbaise所指出的。我移动applicationContext.xml到资源文件夹。War 插件 webResources 旨在处理外部资源,过滤目标文件夹中的文件本身并不是最佳做法。我更新了 POM 以反映新配置

    <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 to him. But that's was not enough, it still didn't work. 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 activeByDefaultoptionin the profiles activation section.

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

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

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

enter image description here

在此处输入图片说明

回答by insideout

I had a similar problem with filtering the web.xml. I solved the problem by reimporting the whole project in eclipse.

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

The reason was a corrupt /.settings/org.eclipse.wst.common.component file. In this file the order of the files copied to the local web servers deploy directory is defined. For example:

原因是 /.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>

If the web.xml or application.xml exists in several directories it will be taken from the first directory found. Therefore its important that

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

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

is the first entry.

是第一个条目。

You will find more informations at http://wiki.eclipse.org/M2E-WTP_FAQin the section "What is this web resources folder?"

您可以在http://wiki.eclipse.org/M2E-WTP_FAQ的“这个 Web 资源文件夹是什么?”部分中找到更多信息。

回答by khmarbaise

Have you tried to put the resources under src/main/resources/WEB-INF/... instead of and configured the resources area to filter the resourcesinstead of putting configuration into a non default maven location.

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

回答by Piotr Tempes

I just had a similar problem. My solution is not elegant and I am not proud of it, but let's say it is acceptable. In my case I have a spring boot mvc application with swagger application (for testers to test our API). The thing is we are using this app in two environments, so I created two profiles - dev and test. In dev env we would like to fire application from eclipse with just "run as" so the context path is blank (I know it can be set in spring java config, but it is not the only placeholder we would like to switch) and in test env the application is run in our customized version of tomcat... so the context path is the same as war file name.

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

and here is the problem - swagger is calling rest docs on this context path and it depends on spring profile. So we have a web resource that needs to be filtered. At first I tried to use m2e-wtp filtering:

这就是问题所在 - swagger 在此上下文路径上调用 rest 文档,这取决于 spring 配置文件。所以我们有一个需要过滤的网络资源。起初我尝试使用 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>
...

it was working but only run in embedded in eclipse tomcat or from commandline java -jar it was not working with "run as" from eclipse.

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

The resource was filtered and I saw them in web-resource in target, but eclipse seems to be running on a source code directly or is making a copy I do not know... it cannot see filtered resources... so I thought that I will make something like this:

资源被过滤了,我在目标的 web-resource 中看到了它们,但是 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>

It is not most fortunate solution, because it is modifing code and not the target, but at least it is working. If anyone would have any suggestions how to make it work without code modifications I would be greateful.

这不是最幸运的解决方案,因为它正在修改代码而不是目标,但至少它正在工作。如果有人对如何在不修改代码的情况下使其工作有任何建议,我会很高兴。

回答by Cristiano De Magalh?es Almeida

I've tried everything described above without success. The files were filtered and generated correctly at the "m2e-wtp" folder, but for some reason Eclipse was copying files from "target/classes".

我已经尝试了上面描述的所有方法都没有成功。这些文件在“m2e-wtp”文件夹中被过滤并正确生成,但由于某种原因,Eclipse 正在从“目标/类”复制文件。

Thus, I've changed my pom.xml, changing the destination folder from "m2e-wtp" to "target/classes", like code ahead.

因此,我更改了 pom.xml,将目标文件夹从“m2e-wtp”更改为“target/classes”,就像前面的代码一样。

Important: everytime you need to run a maven build on project, you must change the pom in order to build de project.

重要提示:每次您需要在项目上运行 Maven 构建时,您都必须更改 pom 以构建 de 项目。

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

回答by Michal

Today I had similar problem when several jars were included into war. I set the filtering on,and compared the original jars with filtered. They seem to be same,but this is not true. I tried to rename jar to zip and I was not able to unpack the content due to corrupted structure inside jar(zip) whereas the original one were ok. It is also mentioned here adding filtering web resourceswhere is said that filtering has to be set false to prevent corrupting your binary files.

今天,当几个罐子被纳入War时,我遇到了类似的问题。我设置了过滤,并将原始罐子与过滤器进行了比较。他们似乎是一样的,但事实并非如此。我试图将 jar 重命名为 zip,但由于 jar(zip) 内部的结构损坏,我无法解压缩内容,而原始的没问题。这里还提到添加过滤网络资源,据说过滤必须设置为 false 以防止损坏您的二进制文件。