【发布时间】: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.plugins maven-resources-plugin 2.7 -
感谢您的帮助。您可以在下面找到解决方案。为了您的信息,我将我的文件包含在一组将被过滤的文件中,并将其从所有不会被过滤的文件中排除。仔细阅读我的配置,我一定会明白的。如果您想了解更多信息,请随时与我联系。