【问题标题】:Maven filtering doesn't work with properties in pom.xmlMaven 过滤不适用于 pom.xml 中的属性
【发布时间】:2015-10-18 22:47:28
【问题描述】:

我正在尝试配置我的 pom.xml 文件以替换 Spring 配置文件中的占位符。

这是我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>tbetous.spring</groupId>
    <artifactId>blog</artifactId>
    <name>learning-tp-blog</name>
    <packaging>war</packaging>
    <version>1.0.0-BUILD-SNAPSHOT</version>
    <properties>
        <java-version>1.6</java-version>
        <env>dev</env>
        <org.springframework-version>3.1.1.RELEASE</org.springframework-version>
        <org.aspectj-version>1.6.10</org.aspectj-version>
        <org.slf4j-version>1.6.6</org.slf4j-version>
    </properties>
    <dependencies>
        [ALL DEPENDENCIES]
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/webapp</directory>
                <filtering>true</filtering>
                <includes>
                    <include>WEB-INF/spring/application-context-infrastructure-env.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/webapp</directory>
                <filtering>false</filtering>
                <excludes>
                    <exclude>WEB-INF/spring/application-context-infrastructure-env.xml</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

这是我的 Spring 配置文件(*path : /src/main/webapp/WEB-INF/spring/application-context-infrastructure-env.xml):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- ${env} is a variable that is replaced at build time by Maven (see pom.xml).-->
<import resource="infrastructure/${env}/application-context-${env}-infrastructure.xml"/>

</beans>

但是当我尝试在 mvn clean install 之后启动我的应用程序时,我得到了这个:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [application-context-infrastructure-env.xml]
Offending resource: ServletContext resource [/WEB-INF/spring/application-context.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/spring/application-context-infrastructure-env.xml]; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'env'

当我查看目标目录中的 application-context-infrastructure-env.xml 时,没有任何变化。我有 ${env} 占位符。

我认为我的 Maven 过滤无法正常工作。你能帮我解决这个问题吗?

【问题讨论】:

  • 您的资源定义指定排除您所引用的文件。是错字吗?
  • 另外,在修复资源声明后尝试添加资源插件。 org.apache.maven.pluginsmaven-resources-plugin2.7
  • 感谢您的帮助。您可以在下面找到解决方案。为了您的信息,我将我的文件包含在一组将被过滤的文件中,并将其从所有不会被过滤的文件中排除。仔细阅读我的配置,我一定会明白的。如果您想了解更多信息,请随时与我联系。

标签: spring maven


【解决方案1】:

我认为你必须使用maven-war-plugin

我的一个项目的示例配置:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.5</version>
        <configuration>
            <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
            <webResources>
                <resource>
                    <directory>src/main/webapp</directory>
                    <includes>
                        <include>**/*.html</include>
                    </includes>
                    <filtering>true</filtering>
                </resource>
                <resource>
                    <directory>src/main/webapp/META-INF</directory>
                    <targetPath>/META-INF</targetPath>
                    <filtering>true</filtering>
                </resource>

            </webResources>

        </configuration>
    </plugin>

只需调整&lt;directory&gt; 元素。

【讨论】:

  • 谢谢。有用 !但是我不明白我的错误。为什么需要这个插件?
  • @Elthum - 我不完全确定,但我认为需要 war 插件,因为 Web 配置文件与 Java 资源不同。后者位于src/main/resources,web文件位于src/main/webapp
  • 我已经测试了相同的配置,但我所有的 spring 文件都在 src/main/resources 中。似乎没有插件 maven-war-plugin 也可以工作。我想你是对的。非常感谢您的所有回答。
猜你喜欢
  • 2020-08-08
  • 2018-04-13
  • 2018-08-04
  • 2016-01-12
  • 2015-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-05
相关资源
最近更新 更多