【问题标题】:Maven: profile-based properties used in plugins sectionMaven:插件部分中使用的基于配置文件的属性
【发布时间】: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>

通过这种配置,我几乎可以在任何地方为我当前的配置文件使用相应的属性。无处不在,但 &lt;plugins&gt; 部分。我非常想从这些属性文件中加载例如我的数据库 url 或凭据,但是如果我将它们包含在 app.properties 中,它们不会在插件部分中进行评估(例如,我将 ${endpoint} 的值作为数据库端点) .

如何从&lt;plugins&gt; 部分可访问的配置文件的文件中加载属性?

PS:是的,如果我直接在pom.xml 中将这些属性添加为&lt;profiles&gt; 标记下的属性,它们是可以访问的,但我宁愿将我的密码从pom 中取出。

【问题讨论】:

  • 如果将密码放入pom.xml 是唯一的障碍,请将它们与密码文件的路径(或仅与某个已知位置相关的文件名以减少泄漏)交换。
  • @VictorSorokin 似是而非。我仍然在寻找最整洁的解决方案。如果我将它们放在文件中,那么我需要在某处读取它们的逻辑,对吗?
  • 如果我的任务正确,maven-resources-plugin 将允许您将每个配置文件的属性值放入稍后处理的模板文件中:portofino.manydesigns.com/en/docs/portofino3/tutorials/…
  • @VictorSorokin 我不相信你已经正确理解了我,因为通过你链接到的方法,我必须将密码存储在pom.xml

标签: java maven maven-3 maven-profiles


【解决方案1】:

我能够做我想做的事。我使用了properties-maven-plugin 链接,例如this answer

我做了以下事情:

  • 我添加了properties-maven-plugin 来读取我需要加载的文件

    <plugin>
       <groupId>org.codehaus.mojo</groupId>
       <artifactId>properties-maven-plugin</artifactId>
       <version>1.0-alpha-2</version>
       <executions>
         <execution>
           <phase>initialize</phase>
           <goals>
             <goal>read-project-properties</goal>
           </goals>
           <configuration>
             <files>
               <file>profiles/${build.profile.id}/app.properties</file>
             </files>
           </configuration>
         </execution>
       </executions>
     </plugin>
    

    很遗憾,在这里我无法让插件读取目录中的所有属性文件,但我觉得这已经足够了。

  • 我还需要删除上面插件定义在 Eclipse 中为我提供的错误 (Plugin execution not covered by lifecycle configuration)。为此,我按照following post 的说明进行操作。

通过这些步骤,我需要的属性可用于使用它们的插件。

注意:实际上属性是在 compile maven 命令之后加载的,但这对我来说已经足够了,因为我所有依赖于属性的目标都将在 compile 目标之后按照我所有的目标调用顺序执行案例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    • 2011-07-22
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    相关资源
    最近更新 更多