【问题标题】:How do I include maven environment variables in eclipse to war file如何在 Eclipse 中将 Maven 环境变量包含到 war 文件中
【发布时间】:2017-05-30 08:36:06
【问题描述】:
我的 application.properties 中有这样的环境变量 spring.mail.username=${username}
${username} 在 eclipse 环境变量中声明。当我构建 maven 包并安装时,然后将其部署到 tcServer。 TC 服务器不知道${username}。换句话说,环境变量在构建过程中不包含在war文件中。
如何在 eclipse 中获取环境变量以包含在 war 文件中以进行部署?
【问题讨论】:
标签:
java
maven
spring-boot
【解决方案1】:
使用 alexbt 的answer 中描述的 Maven 过滤是包含在别处定义的值的正确方法。他的示例涉及包含操作系统环境变量。您也可以将其扩展到 Maven 属性。例如,
<project ...>
<properties>
<spring.mailuser>bob@mycompany.com</spring.mailuser>
</properties>
...
<build>
...
</build>
</project>
定义一个 Maven 属性,其值由${spring.mailuser} 检索,可以用作一部分
其他 Maven 配置或通过 Maven 过滤作为内容注入。鉴于此,改变
applicable.properties如下
spring.mail.username=${spring.mailuser}
将在构建时注入属性的值。
【解决方案2】:
如果您希望替换构建时变量,我建议您使用 maven 过滤:
有一个环境变量(不是 Eclipse 的):
export username=user3184890
然后,在您的 pom.xml 中,激活 maven 过滤 资源(假设您的 application.properties 在 src/main/resources:
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
另外,将您的 application.properties 更改为:
spring.mail.username=${env.username}
或
spring.mail.username=@env.username@