Search code examples
javamavenmaven-3maven-profiles

Maven: profile-based properties used in plugins section


I would like to mention I am relatively new in Maven configurations.

My situation:

  • I use Maven 3.0.5 to build J2E application
  • the application is deployed in four different environments: local, dev, test and prod
  • I use maven profiles to configure environment-specific configurations
  • I have defined these configurations in properties files in the file system.

This is the file system for those:

<my-project-root>
---profiles
------local
---------app.properties
------dev
---------app.properties
------test
---------app.properties

I load the corresponding property file with the following logic in my 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>

With this configuration I can use the respective properties for my current profile almost everywhere. Everywhere, but the <plugins> section. I would pretty much like to load e.g, my database url or credentials from such properties files, but if I include them in the app.properties they are not evaluated in the plugins section (e.g. I get value of ${endpoint} as database endpoint).

How do I get the properties loaded from files for the profile accessible in the <plugins> section?

PS: Yes, if I add those properties directly in the pom.xml as properties under <profiles> tag, they are accessible, but I would rather keep my passwords off the pom.


Solution

  • I was able to do what I wanted to do. I used properties-maven-plugin linked from, say this answer.

    What I did was the following:

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

    With those steps the properties I needed became available for the plugins, that used them.

    Note: actually the properties get loaded after the compile maven command, but this is good enough for me, as all my property-dependant goals are to be executed after compile goal in sequence of goal calls in all my cases.