使用Maven war插件来过滤Web资源并不能在Eclipse中使用m2e。

时间:2023-01-25 14:24:11

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)

不幸的是,当在Tomcat 6中使用Eclipse WTP和m2e热部署webapp时,它总是选择未过滤的applicationContext.xml。(applicationContext的文件。文件夹目标/m2e-wtp/web资源/WEB-INF/上下文中的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?

我的配置有问题吗?或者这是一个未实现的特性?

6 个解决方案

#1


24  

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

首先,我做了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 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.

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

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

因此,我将默认的概要文件添加到项目Maven配置中,然后它就工作了。

使用Maven war插件来过滤Web资源并不能在Eclipse中使用m2e。

#2


7  

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或应用程序。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_FAQ in the section "What is this web resources folder?"

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

#3


1  

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

你有没有试过把资源放在src/main/resources/WEB-INF/…而不是配置资源区域来过滤资源,而不是将配置放入一个非默认的maven位置。

#4


0  

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.

我有一个类似的问题。我的解决方案不优雅,我也不感到自豪,但我们可以接受。在我的例子中,我有一个带有轻便应用程序的spring boot mvc应用程序(供测试人员测试我们的API)。问题是我们在两个环境中使用这个应用程序,所以我创建了两个profiles - dev和test。在dev env我们愿火从eclipse应用程序只有“运行”上下文路径是空白(我知道它在春天可以设置java配置,但它不是唯一的占位符我们想开关)和测试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:

这里有一个问题——大摇大摆的在这个上下文中调用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或commandline 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资源中看到了它们,但是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.

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

#5


0  

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 resources where is said that filtering has to be set false to prevent corrupting your binary files.

今天我遇到了类似的问题,几个罐子被卷入了战争。我设置了过滤,并将原始的jar与过滤的进行了比较。它们看起来是一样的,但这不是真的。我试着将jar重命名为zip,但是由于jar(zip)中的结构损坏,我无法将其解压,而原来的则可以。这里还提到了添加过滤web资源,其中说过滤必须设置为false,以防止损坏二进制文件。

#6


0  

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”改为“目标/类”,比如前面的代码。

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>

#1


24  

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

首先,我做了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 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.

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

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

因此,我将默认的概要文件添加到项目Maven配置中,然后它就工作了。

使用Maven war插件来过滤Web资源并不能在Eclipse中使用m2e。

#2


7  

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或应用程序。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_FAQ in the section "What is this web resources folder?"

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

#3


1  

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

你有没有试过把资源放在src/main/resources/WEB-INF/…而不是配置资源区域来过滤资源,而不是将配置放入一个非默认的maven位置。

#4


0  

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.

我有一个类似的问题。我的解决方案不优雅,我也不感到自豪,但我们可以接受。在我的例子中,我有一个带有轻便应用程序的spring boot mvc应用程序(供测试人员测试我们的API)。问题是我们在两个环境中使用这个应用程序,所以我创建了两个profiles - dev和test。在dev env我们愿火从eclipse应用程序只有“运行”上下文路径是空白(我知道它在春天可以设置java配置,但它不是唯一的占位符我们想开关)和测试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:

这里有一个问题——大摇大摆的在这个上下文中调用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或commandline 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资源中看到了它们,但是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.

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

#5


0  

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 resources where is said that filtering has to be set false to prevent corrupting your binary files.

今天我遇到了类似的问题,几个罐子被卷入了战争。我设置了过滤,并将原始的jar与过滤的进行了比较。它们看起来是一样的,但这不是真的。我试着将jar重命名为zip,但是由于jar(zip)中的结构损坏,我无法将其解压,而原来的则可以。这里还提到了添加过滤web资源,其中说过滤必须设置为false,以防止损坏二进制文件。

#6


0  

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”改为“目标/类”,比如前面的代码。

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>