我致力于 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 控制台它更舒服一些。
最佳答案
通过 maven 构建的 token 过滤不适用于 eclipse 热部署。
一种选择是将 -Dspring.profiles.active="dev"传递给 JVM/App 进程。
我更喜欢使用环境变量来控制配置文件加载。例如,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"));
}
}
关于eclipse - 从 pom.xml 设置事件 Spring 配置文件并与 IDE 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15870252/