【问题标题】:Maven profile filter works when building WAR but not in Tomcat7 runMaven 配置文件过滤器在构建 WAR 时有效,但在 Tomcat7 运行时无效
【发布时间】:2014-04-09 18:09:32
【问题描述】:

我正在尝试使用 Maven 在开发和生产中的 web.xml 文件中引入不同的配置点。看起来使用配置文件和过滤器是可行的方法,当我使用“mvn install”或“mvn package”构建 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”会导致“无法解析占位符”错误。欢迎大家提出意见。

【问题讨论】:

    标签: java maven tomcat


    【解决方案1】:

    我发现针对 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)。我在这里只是为了区分两者。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-19
      • 2011-01-27
      • 2015-10-09
      • 1970-01-01
      • 2011-02-15
      • 1970-01-01
      • 2011-10-13
      相关资源
      最近更新 更多