【问题标题】:maven pass command argument at build time to overwrite propertymaven 在构建时传递命令参数以覆盖属性
【发布时间】:2013-05-14 12:14:09
【问题描述】:

在我们的应用程序中,我们有一个白标系统。

application.properties中有一个设置theme=default

此设置被注入 Spring 托管 bean,然后通过框架对应用程序进行操作,例如添加正确的 css 等

我希望能够在构建时(创建战争)指定主题,例如mvn clean install -theme:some-theme。这将更新application.properties,并修改theme 如果你只运行mvn clean install 那么theme=defaultunmodified

这可能吗?

【问题讨论】:

    标签: java maven maven-3


    【解决方案1】:

    通过命令行设置属性的正确方法是使用-D:

    mvn -Dproperty=value clean package
    

    它将覆盖之前在pom.xml 中定义的任何属性。


    所以如果你有你的pom.xml:

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

    mvn -Dtheme=halloween clean package 将在此执行期间覆盖 themes 的值,效果就像您有:

    <properties>
        <theme>halloween</theme>
    </properties>
    

    【讨论】:

    • 这与资源过滤相结合正是我想要的
    • 谢谢,这正是我想要的
    【解决方案2】:

    我猜你正在寻找的是 maven 构建配置文件和 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/main/resources 中拥有一个属性文件:

    application.properties:

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

    这种方法提供了基于配置文件进行自定义的灵活性,因此可以说是批量自定义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-27
      • 2012-12-02
      • 2011-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-14
      相关资源
      最近更新 更多