【发布时间】:2015-06-01 02:10:30
【问题描述】:
我想提一下我在 Maven 配置方面相对较新。
我的情况:
- 我使用 Maven 3.0.5 构建 J2E 应用程序
- 应用程序部署在四种不同的环境中:本地、开发、测试和生产
- 我使用 maven 配置文件来配置特定环境的配置
- 我已经在文件系统的
properties文件中定义了这些配置。
这是这些人的文件系统:
<my-project-root>
---profiles
------local
---------app.properties
------dev
---------app.properties
------test
---------app.properties
我在pom.xml 中加载了具有以下逻辑的相应属性文件:
<profiles>
<profile>
<id>local</id>
<!-- The development profile is active by default -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<build.profile.id>local</build.profile.id>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<build.profile.id>dev</build.profile.id>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<build.profile.id>prod</build.profile.id>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<build.profile.id>test</build.profile.id>
</properties>
</profile>
</profiles>
<build>
<finalName>MyProject</finalName>
<plugins>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>profiles/${build.profile.id}</directory>
</resource>
</resources>
</build>
通过这种配置,我几乎可以在任何地方为我当前的配置文件使用相应的属性。无处不在,但 <plugins> 部分。我非常想从这些属性文件中加载例如我的数据库 url 或凭据,但是如果我将它们包含在 app.properties 中,它们不会在插件部分中进行评估(例如,我将 ${endpoint} 的值作为数据库端点) .
如何从<plugins> 部分可访问的配置文件的文件中加载属性?
PS:是的,如果我直接在pom.xml 中将这些属性添加为<profiles> 标记下的属性,它们是可以访问的,但我宁愿将我的密码从pom 中取出。
【问题讨论】:
-
如果将密码放入
pom.xml是唯一的障碍,请将它们与密码文件的路径(或仅与某个已知位置相关的文件名以减少泄漏)交换。 -
@VictorSorokin 似是而非。我仍然在寻找最整洁的解决方案。如果我将它们放在文件中,那么我需要在某处读取它们的逻辑,对吗?
-
如果我的任务正确,
maven-resources-plugin将允许您将每个配置文件的属性值放入稍后处理的模板文件中:portofino.manydesigns.com/en/docs/portofino3/tutorials/… -
@VictorSorokin 我不相信你已经正确理解了我,因为通过你链接到的方法,我必须将密码存储在
pom.xml
标签: java maven maven-3 maven-profiles