简体   繁体   中英

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

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 .

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

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM