2

我想提一下我在 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.xmlas 属性中添加这些属性<profiles>,它们是可以访问的,但我宁愿将我的密码保留在 pom.xml 中。

4

1 回答 1

1

我能够做我想做的事。我用properties-maven-plugin了链接,说这个答案

我所做的是以下内容:

  • 我添加了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>
    

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

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

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

注意:实际上属性是在compilemaven 命令之后加载的,但这对我来说已经足够了,因为在我compile所有的情况下,我所有与属性相关的目标都将在目标之后按照目标调用的顺序执行。

于 2015-03-29T12:16:16.133 回答