2010-02-25 120 views
22

我的應用程序中有一個名爲ApplicationResources.properties的屬性文件,其屬性根據環境而變化。讓我們說的屬性是:Maven根據配置文件更改文件中的值

 resources.location=/home/username/resources 

和發展時,當應用程序投入生產過程中執行的應用程序這個值是不同的。

我知道我可以在Maven中使用不同的配置文件在不同的環境中執行不同的構建任務。我想要做的是以某種方式根據所使用的Maven配置文件替換屬性文件中的resources.location的值。這甚至有可能嗎?

回答

46

我想要做的是以某種方式根據所使用的Maven配置文件替換屬性文件中resources.location的值。這甚至有可能嗎?

是的。激活資源過濾並定義要在每個配置文件中替換的值。

在你ApplicationResources.properties,聲明令牌替換這樣的:

resources.location=${your.location} 

在你的POM,加<filtering>標籤爲適當<resource>並將其設置爲true這樣的:

<project> 
    ... 
    <build> 
    ... 
    <resources> 
     <resource> 
     <directory>src/main/resources</directory> 
     <filtering>true</filtering> 
     </resource> 
     ... 
    </resources> 
    ... 
    </build> 
    ... 
</project> 

然後,在每個配置文件內的<properties>元素內添加一個<your.location>元素:

<project> 
    ... 
    <profiles> 
    <profile> 
     <id>my-profile</id> 
     ... 
     <properties> 
     <your.location>/home/username/resources</your.location> 
     </properties> 
     ... 
    </profile> 
    ... 
    </profiles> 
</project> 

關於過濾資源herehere的更多信息。

+1

@Pascal Thivent謝謝。這是一個很好的解釋 – 2010-02-25 17:06:53

+0

+1我第二個:D – ant 2010-02-25 23:10:05