【问题标题】:How can I set spring profile on tomcat7-maven-plugin?如何在 tomcat7-maven-plugin 上设置弹簧配置文件?
【发布时间】:2015-03-11 11:40:31
【问题描述】:

如何设置活动弹簧配置文件或 tomcat maven 插件?我想在运行时从环境中获取活动配置文件,但我总是得到空数组。

我尝试在 pom.xml 的插件配置中添加这个 systemProperties:

<JAVA_OPTS>-Dspring.profiles.active=local</JAVA_OPTS>

<name>spring.profiles.active</name>
<value>locale</value>

我尝试在 context.xml 中添加这个:

<environment name="spring.profiles.active" value="local" type="java.lang.String" override="false"/>

但仍然没有活跃的个人资料...

我的环境:Java 6、tomcat7-maven-plugin 2.2 版、Netbeans 8.0.2、Spring 3.1.1.RELEASE。

我的 pom.xml tomcat 插件看起来是这样的:

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>

    <configuration>
        <!-- http port -->
        <port>8084</port>
        <contextFile>${basedir}/src/main/resources-local/context.xml</contextFile>
        <systemProperties>
            <JAVA_OPTS>-Dspring.profiles.active=local</JAVA_OPTS>
        </systemProperties>
    </configuration>
    <executions>
        <execution>
            <id>tomcat-run</id>
            <goals>
                <goal>run-war</goal>
            </goals>
            <phase>package</phase>
        </execution>
    </executions>
<dependencies>

【问题讨论】:

    标签: spring maven-tomcat-plugin


    【解决方案1】:

    Tomcat 7 Documentation 显示了一种不同的系统属性语法,如下所示。所以尝试这样做:

    <plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.1</version>
    <configuration>
      <systemProperties>
        <example.value.1>alpha</example.value.1>
        <example.value.2>beta</example.value.2>
      </systemProperties>
    </configuration>
    </plugin>
    

    【讨论】:

    • 这样我可以使用 System.getProperty("spring.profile.active") 读取“本地”值,但是 environment.getActiveProfiles() 仍然返回空数组......这可能是部分解决方案这对我的目的就足够了......谢谢
    • @CodeRunner,您的编辑不正确。你必须恢复它。您使用 tomcat6 信息更新了它,而问题是关于 tomcat7。我会拒绝编辑,但没有机会。我现在不选择拒绝编辑。
    • @minion 我已经更正了。感谢您指出版本错误 - 我的其余编辑是正确的,并且符合 tomcat7 文档。
    猜你喜欢
    • 1970-01-01
    • 2014-10-14
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 2017-05-27
    • 2018-11-18
    相关资源
    最近更新 更多