[英]Maven profile filter works when building WAR but not in Tomcat7 run
我正在嘗試使用Maven在開發和生產中的web.xml文件中引入不同的配置點。 看起來似乎要使用配置文件和過濾器,實際上,當我使用“ mvn install”或“ mvn軟件包”構建WAR文件時,這確實可行。 但是,通過Tomcat 7插件運行它總是會導致“無法解析占位符”錯誤。 以下是相關行:
在web.xml中-這是我要設置的占位符
/WEB-INF/${sec-file-path}
在pom.xml中
<profiles>
<profile>
<id>development</id>
<properties>
<sec-file-path>one-thing-for-dev.xml</sec-file-path>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<sec-file-path>one-thing-for-prod.xml</sec-file-path>
</properties>
</profile>
</profiles>
....
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
</plugin>
<plugin>
<artifactId> maven-war-plugin </artifactId>
<version>2.4</version>
<configuration>
<webResources>
<resource>
<filtering>true</filtering>
<directory>src/main/webapp</directory>
<includes>
<include>**/web.xml</include>
</includes>
</resource>
</webResources>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
運行命令“ mvn package -P development”可以正常工作,並適當地填充占位符。 但是,運行“ mvn tomcat7:run -P development”結果導致“無法解析占位符”錯誤。 歡迎大家提出意見。
我發現在針對mvn tomcat:run(Tomcat 6)的運行中(似乎)正確放置了占位符值的類似行為,但是突然mvn tomcat7:run(Tomcat 7)會失敗。
我認為實際上,Tomcat6插件超出了其范圍,並且Tomcat7插件正常工作。 (我不是Spring或Maven方面的專家,因此請大聲說一下。)
Spring文件(applicationContext.xml)應該從屬性占位符文件而不是Maven篩選過程中獲取其占位符值。
解決此問題的方法是使用Spring控制的屬性文件中的占位符值,而不是maven過濾器文件。 您仍然可以利用過濾器文件中的數據,只需將它們傳遞到屬性文件中即可完成工作。
照常設置過濾器:
pom.xml
<build>
<filters>
<filter>src/main/filters/local.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
創建過濾器屬性文件:
src / main / filters / local.properties
# Session
session.timeout=20
創建您的Spring占位符屬性文件:
src / main / resources / META-INF / spring / timeout.properties
# Session
spring.session.timeout = ${session.timeout}
最后,在應用程序上下文中配置占位符:
src / main / resources / META-INF / spring / applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" 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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring- context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!-- Properties file for Session Configuration -->
<context:property-placeholder location="/opt/Projects/workspace/application_server/src/main/webapp/WEB-INF/spring/*.properties"/>
...
<bean id="timeout.example" class="java.lang.Integer">
<constructor-arg value="${spring.session.timeout}"/>
</bean>
</beans>
當Maven運行其資源階段時,timeout.properties文件中的占位符值將更新為在local.properties文件中找到的值。
當Spring進程“稍后”運行(在運行階段)時,它將詢問applicationContext.xml文件以查找占位符,並將其填充在PlaceholderConfigured文件(timeout.properties)中找到的值。
(注意:變量名不必相同(例如spring.session.timeout與session.timeout)。我在這里只是為了區分兩者。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.