2014-01-09 83 views
1

蔭使用maven的pom.xml(剛開始學習)無法檢索Maven的從屬性文件值

我有一些.properties文件(如爲:log4j.properties),我應該能夠從檢索值它們在pom.xml或web.xml文件中,我的意思是如果我在pom.xml或web.xml中使用類似$ {somename.version}的東西,則應該從.properties文件中檢索此值。

我的屬性文件正在如下:

src/main/resources/log4j.properties 
src/main/env/dev/config.properties 

IAM嘗試如下,但無法從屬性文件中檢索值.. IAM做錯了什麼。

<resources> 
      <resource> 
       <directory>src/main/resources</directory> 
       <filtering>true</filtering> 
      </resource> 
      <resource> 
       <directory>src/main/env/dev</directory> 
       <filtering>true</filtering> 
      </resource> 
     </resources> 

請建議我。

+0

任何人都可以嗎? – user3128668

+0

也許你需要屬性插件? :) –

+0

哪些屬性插件我需要使用?這是強制性的使用? – user3128668

回答

0

你的配置是錯誤的,從文件中讀取使用性質:

<filters> 
    <filter>src/main/resources/log4j.properties</filter> 
</filters> 

您的實際配置是關於應過濾哪些文件。

欲瞭解更多信息和示例,請訪問:Maven Resource Plugin - Filtering


您還可以使用Properties Maven Plugin

+0

請問你看看這個:http://stackoverflow.com/questions/21018252/not-getting-values-from-properties-files-in-context-xml-file – user3128668

+0

是的,我可以,但是如果這可以幫助你或激勵你,你可以開心嗎? :) – MariuszS

+0

我做到了,請你在這裏回答:http:// stackoverflow。com/questions/21037291 /在項目內部保留一個插件-a-plugin-a-profile- – user3128668

0

資源過濾是爲了在pom.xml中設置一個屬性,然後在屬性文件中使用它。

與$ {} somename.version在log4j.properties在你的pom.xml $ {} somename.version適當的代碼替換爲你的pom.xml

例如內部pom.xml中輸入的內容

<property> <name>somename.version</name> <value>123</value> </property>

在log4j.property $ {} somename.version將

被替換,你會發現你的文件,y之後的目標目錄內的替換值ou啓動mvn包

資源過濾用於與配置文件打包時使用。每個配置文件可以在pom.xml中更改屬性

你的屬性可以認爲,與像Windows或配置Linux配置環境在改變值

+0

可以請你看看這個,在這裏:http://stackoverflow.com/questions/21018252/not-getting-values-from- properties-files-in-context-xml-file – user3128668