Ula Krukar
Ula Krukar

Reputation: 12999

Maven - Can I reference profile id in profile definition?

I've set profiles in a pom.xml, like shown as follows:

<profile>
<id><em>profileId1</em></id>
    <build>
        <filters>
            <filter>src/main/filters/<em>profileId1</em>.properties</filter>
        </filters>
// rest of the profile 
</profile>
<profile>
<id><em>profileId2</em></id>
    <build>
        <filters>
            <filter>src/main/filters/<em>profileId2</em>.properties</filter>
        </filters>
// rest of the profile
</profile>

Question:

Is there any way to extract this piece from all the profiles, so that there is no need to repeat it for every profile (and possibly misspell it)?

Upvotes: 42

Views: 38273

Answers (3)

alfonx
alfonx

Reputation: 7216

With maven 2.2.1 and later, I was able to get the ID of the first active profile using:

${project.activeProfiles[0].id}

Of course this fails if there is not a least one active profile.

Using the

${project.profiles[0].id}

as suggested by Pascal did not work for me.

Hint: While investigating this, I really started to love mvn help:evaluate.

Upvotes: 42

Mike R
Mike R

Reputation: 4538

As an alternative to ${project.activeProfiles[0].id} (which doesn't seem to work on older versions of maven), just define a property:

    <profile>
        <id>dev</id>
        <properties>
            <profile-id>dev</profile-id>
        </properties>
    </profile>

Then use ${profile-id}.

Note: just make sure one is always active by default

Upvotes: 19

Pascal Thivent
Pascal Thivent

Reputation: 570365

According to PLXUTILS-37, it should be possible to access properties in a List or Map using "Reflection Properties" (see the MavenPropertiesGuide for more about this).

So just try ${project.profiles[0].id}, ${project.profiles[1].id}, etc.

If this doesn't work (I didn't check if it does), I'd use profile activation based on a system property as described in Introduction to build profiles and use that property in the filter. Something like that:

<profile>  
  <id>profile-profileId1</id>  
  <activation>
    <property>
      <name>profile</name>
      <value>profileId1</value>
    </property>
  </activation>
  <build>  
    <filters>  
      <filter>src/main/filters/${profile}.properties</filter>  
    </filters>  
    // rest of the profile  
</profile>

To activate this profile, you would type this on the command line:

mvn groupId:artifactId:goal -Dprofile=profileId1 

Upvotes: 4

Related Questions