25

我正在尝试使用 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 中实现。

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

4

6 回答 6

29

嗯,最后我明白了。

首先,我做了khmarbaise指出的事情。我移动applicationContext.xml到资源文件夹。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>

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

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

在此处输入图像描述

于 2012-03-22T08:32:35.043 回答
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_FAQ的“什么是这个 Web 资源文件夹?”部分找到更多信息。

于 2014-01-27T15:34:22.593 回答
1

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

于 2012-03-21T09:38:57.400 回答
0

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

这就是问题所在 - swagger 在此上下文路径上调用 rest docs,它取决于 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>
...

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

资源已被过滤,我在目标的 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>

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

于 2014-07-17T12:01:04.623 回答
0

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

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

重要提示:每次需要在项目上运行 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>
于 2015-07-24T17:12:49.150 回答
0

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

于 2015-10-29T12:02:01.153 回答