Maven - Can I reference profile id in profile definition?

后端 未结 3 1491
盖世英雄少女心
盖世英雄少女心 2020-12-09 14:28

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


profileId1
             


        
相关标签:
3条回答
  • 2020-12-09 15:15

    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

    0 讨论(0)
  • 2020-12-09 15:21

    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.

    0 讨论(0)
  • 2020-12-09 15:26

    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 
    
    0 讨论(0)
提交回复
热议问题