2013-05-14 82 views
3

在我們的應用程序中,我們有一個白標籤系統。maven pass命令參數在構建時覆蓋屬性

application.properties有一個設置theme=default

此設置被注入一個Spring管理的bean,然後該應用是否會操縱通過框架,比如增加了正確的CSS等

我會喜歡能夠做到,在建造時(創建戰爭),指定主題,例如mvn clean install -theme:some-theme。那麼這將更新application.properties,並修改theme ,如果你只運行mvn clean install然後theme=defaultunmodified

這可能嗎?

回答

7

適當的方式來設置通過命令線的屬性使用-D

mvn -Dproperty=value clean package 

它將覆蓋先前在pom.xml定義的任何屬性。


所以,如果你在你的pom.xml

<properties> 
    <theme>myDefaultTheme</theme> 
</properties> 

mvn -Dtheme=halloween clean package將這個執行過程中覆蓋theme價值,有效果,如果你有:

<properties> 
    <theme>halloween</theme> 
</properties> 
+0

這與資源過濾結合正是我正在尋找 – kabal 2013-05-14 12:42:19

+0

謝謝,這正是我一直在尋找 – Anirban 2014-04-29 16:08:32

2

我猜你正在尋找的是maven build profile和resource filtering。您可以爲每個主題分配一個配置文件,並根據配置文件,可以更改application.properties中的參數值。

<build> 

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

    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>2.3.1</version> 
      <configuration> 
       <source>1.7</source> 
       <target>1.7</target> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

<profiles> 
    <profile> 
     <id>white</id> 
     <properties> 
      <theme>white</theme> 
      <prop1>xyz</prop1> 
      <!--and some other properties--> 
     </properties> 
    </profile> 

    <profile> 
     <id>default</id> 
     <properties> 
      <theme>default</theme> 
      <prop1>abc</prop1> 
      <!--and some other properties--> 
     </properties> 
    </profile> 
</profiles> 

而且你可以放在src /主/資源屬性文件:

application.properties:

my.theme=${theme} 
my.custom.property=${prop1} 

這種方法使做定製的基礎上,配置文件中的靈活性,所以可以說批量定製。