Maven:插件部分使用基于配置文件的属性

3

我想提一下我在Maven配置方面还比较新手。

我的情况是:

  • 我使用Maven 3.0.5来构建J2E应用程序
  • 该应用程序部署在四个不同的环境中:本地、开发、测试和生产
  • 我使用Maven配置文件来配置特定于环境的配置
  • 我已经在文件系统中的属性文件中定义了这些配置。

下面是这些文件在文件系统中的位置:

<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}的值作为数据库端点)。
如何让来自文件的配置文件对于插件部分可用?
PS: 是的,如果我直接将这些属性添加到 pom.xml<profiles>标签下,它们是可访问的,但我更愿意将密码保存在 pom 之外。

如果将密码放入pom.xml是唯一的障碍,可以将它们与指向密码文件(或仅相对于某个已知位置的文件名)的路径进行交换,以减少泄漏的可能性。 - Victor Sorokin
@VictorSorokin 的建议很有道理。但我仍在寻找最简洁的解决方案。如果我将它们放在文件中,那么我需要在某个地方编写读取它们的逻辑,对吧? - Boris Strandjev
如果我理解得正确的话,maven-resources-plugin 将允许您将每个配置文件属性值放入模板文件中,稍后进行处理:http://portofino.manydesigns.com/en/docs/portofino3/tutorials/using-maven-profiles-and-resource-filtering - Victor Sorokin
@VictorSorokin 我不认为你理解我说的话,因为按照你提供的方法,我必须将密码存储在 pom.xml 文件中。 - Boris Strandjev
1个回答

2

我成功实现了自己的目标。我使用了从这个答案中链接的properties-maven-plugin

我的操作如下:

  • I added the properties-maven-plugin to read the files I needed loaded

    <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>
    

    Regretfully, here I was not able to make the plugin read all property files in a directory, but I find this good enough.

  • I also needed to remove the error the plugin definition above gave for me in Eclipse (Plugin execution not covered by lifecycle configuration). To do thatI followed the instructions from the following post.
通过这些步骤,插件所需的属性变得可用。
注意:实际上,这些属性在执行“compile”Maven命令后加载,但对我来说已经足够了,因为在所有情况下,我的依赖属性目标都是在“compile”目标之后按目标调用顺序执行的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接