2013-04-08 51 views
1

我使用mavenized Java Spring web应用程序。我们在Eclipse IDE中使用了广泛的自动重启&热部署,但是我注意到,在开始混合使用maven和spring配置时,Eclipse中的集成并不好。我们希望在web.xml中有maven变量,maven-war-plugin将在项目构建过程中替换它们。从pom.xml设置活动弹簧配置文件并与IDE集成

Maven的变量在web.xml:

<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的战争插件在构建过程中替换:

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

此解决方案仅当我通过建设项目来自命令行的maven。 Eclipse的热部署机制显然会跳过所有这些Maven插件...

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

我会合适地使用例如maven码头插件运行它的IDE,但我不喜欢在shell窗口中使用控制台 - Eclipse的控制台它有点更舒适。

回答

0

通过maven构建令牌过滤不适用于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")); 
    } 
}