Maven过滤不适用于pom.xml中的属性

时间:2015-07-28 11:42:48

标签: spring maven

我正在尝试配置我的pom.xml文件,以便在Spring的配置文件中替换placeolders。

这是我的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过滤无法正常工作。你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

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

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

set.seed(24)
df1 <- cbind(df, y= rnorm(9))

只需调整 <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> 元素。