Maven过滤不适用于pom.xml中的属性 [英] Maven filtering doesn't work with properties in pom.xml

查看:99
本文介绍了Maven过滤不适用于pom.xml中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to configure my pom.xml file in order to replace placeolders in a configuration file for Spring.

这是我的pom.xml:

Here is my 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):

Here is my Spring configuration file (*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全新安装后启动我的应用程序时,我得到了:

But when I try to launch my application after a mvn clean install, I get this :

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}占位符.

And when I look at my application-context-infrastructure-env.xml in the target directory, nothing have changed. I have the ${env} placeholder.

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

I think my maven filtering doesn't work correctly. Could you help me to fix this ?

推荐答案

我认为您必须使用 maven-war-plugin .

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

Example configuration from one of my projects:

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

只需调整<directory>元素.

这篇关于Maven过滤不适用于pom.xml中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆