2013-05-14 35 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} 

这种方法使做定制的基础上,配置文件中的灵活性,所以可以说批量定制。