【问题标题】:Set active spring profile from pom.xml and integration with IDE从 pom.xml 设置活动弹簧配置文件并与 IDE 集成
【发布时间】:2013-04-08 02:17:46
【问题描述】:

我从事 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 控制台它更舒服一些。

【问题讨论】:

    标签: eclipse spring maven testing configuration-files


    【解决方案1】:

    通过 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"));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-11-18
      • 2014-10-14
      • 2016-12-25
      • 1970-01-01
      • 2018-11-27
      • 2020-01-24
      • 2016-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多