3

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.

Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
  • If putting passwords into `pom.xml` is the only obstacle, swap them with paths to password files (or just file names relative to some known location to lessen leak). – Victor Sorokin Mar 27 '15 at 16:57
  • @VictorSorokin plausible. Still I am looking for neatest possible solution. If I place them in files, then i need the logic to read them somewhere, right? – Boris Strandjev Mar 27 '15 at 17:08
  • If I've got task right, `maven-resources-plugin` will allow you to put per-profile property values into template file which is processed later on: http://portofino.manydesigns.com/en/docs/portofino3/tutorials/using-maven-profiles-and-resource-filtering – Victor Sorokin Mar 27 '15 at 17:13
  • @VictorSorokin I don't believe you have understood me correctly, because with the approach you link to, I have to store the password in the `pom.xml` – Boris Strandjev Mar 29 '15 at 10:53

1 Answers1

2

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.

Community
  • 1
  • 1
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135