从pom.xml设置活动弹簧配置文件并与IDE集成

时间:2013-04-08 02:17:47

标签: eclipse spring maven testing configuration-files

我在mavenized Java Spring Web应用程序上工作。我们正在广泛使用自动重启&在Eclipse IDE中进行热部署,但是我注意到当你开始混合maven和spring配置时,Eclipse中的集成并不好。我们希望在web.xml中有maven变量,这些变量将在maven-war-plugin的项目构建期间被替换。

web.xml中的Maven变量:

 <env-entry>
  <env-entry-name>spring.profiles.active</env-entry-name>
  <env-entry-type>java.lang.String</env-entry-type>
  <env-entry-value>${profileName}</env-entry-value>
 </env-entry>
在构建过程中,

和maven变量被maven-war-plugin替换:

        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>    
            <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
            </configuration>
        </plugin>

此解决方案仅在我从命令行通过maven构建项目时才有效。 Eclipse热部署机制显然会跳过所有maven插件......

你认为这是一个很好的做法,有没有办法如何在eclipse中使用这种配置无缝地工作?

我将适当地使用例如maven jetty插件可以在IDE中运行它,但是我不喜欢在shell窗口中使用控制台 - eclipse控制台它有点舒服。

1 个答案:

答案 0 :(得分:0)

通过maven build进行令牌过滤不适用于eclipse热部署。

一种选择是将-Dspring.profiles.active =“dev”传递给JVM / App进程。

我更喜欢使用env变量来控制配置文件加载。例如,MY_PROJECT_ENV = dev。

以下代码读取变量并初始化配置文件。

public class ProfileInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    public void initialize(ConfigurableApplicationContext applicationContext) {
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        environment.setActiveProfiles(System.getProperty("MY_PROJECT_ENV"));
    }
}