2010-01-06 113 views
34

在pom.xml中我已經集合O配置文件是這樣的:Maven - 我可以在配置文件定義中引用配置文件ID嗎?

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

有什麼辦法,我可以提取這一塊的所有配置文件,所以沒有必要重複這對於每一個配置文件(也可能拼錯它)?

回答

2

根據PLXUTILS-37,應該可以使用「反射屬性」(請參閱​​MavenPropertiesGuide瞭解更多信息)訪問列表或地圖中的屬性。

所以只是嘗試${project.profiles[0].id}${project.profiles[1].id}

如果這不起作用(我沒有檢查,如果它),我會使用配置文件激活基於在Introduction to build profiles描述的系統性能和在過濾器中使用該屬性。類似的東西:

​​

要激活此配置文件,您可以在命令行中輸入:

mvn groupId:artifactId:goal -Dprofile=profileId1 
29

與Maven 2.2.1或更高版本,我能拿到第一的ID活動配置文件使用:

${project.activeProfiles[0].id} 

當然,如果沒有至少一個活動配置文件,則會失敗。

使用

${project.profiles[0].id} 

由帕斯卡爾建議做爲我工作。

提示:在調查這個,我才真正開始喜歡mvn help:evaluate

+2

使用'$ {project.activeProfiles [0] .ID}'完全爲我工作。您可以確保至少有一個配置文件在一個配置文件的激活塊中使用' true'激活。 – SimonB 2012-02-14 11:03:14

+1

同樣在這裏,$ {project.activeProfiles [0] .id}起作用,maven 3.2 – chrismarx 2016-06-13 14:56:58

+0

出於某種原因,它不會提取父pom屬性;因此,例如,如果激活2個配置文件,其中一個配置文件來自父項,則只會檢索當前項目屬性。 – ftkg 2016-12-08 18:08:52

11

至於${project.activeProfiles[0].id}替代(這似乎並沒有工作在舊版本的Maven的),只是定義屬性:

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

然後使用${profile-id}

注:只要確保一個始終處於活動狀態默認

+3

爲什麼我看到蛇咬尾巴? – 2016-03-10 12:19:26

相關問題