代码之家  ›  专栏  ›  技术社区  ›  Jorge Ferreira

基于命令行开关的Maven自定义属性

  •  1
  • Jorge Ferreira  · 技术社区  · 14 年前

    [简化]

  • 模块具有在生成期间过滤其内容的属性文件
  • 有多组属性可以分组
  • 给定的生成中只存在一组属性
  • parent
    +- pom.xml
    +- foo
       +- src/main/resources/test.properties
    

    我在父级中设置了配置文件 pom.xml . 每个概要文件都由命令行参数激活,并设置该组的属性值。

    mvn clean compile -Dgroup1=true
    

    <properties> 对于每个组,使用单个概要文件来控制构建顺序、插件和其他内容。

    2 回复  |  直到 14 年前
        1
  •  2
  •   Sean Patrick Floyd    14 年前

    <profiles>
        <profile>
            <id>group1</id>
            <build>
                <filters>
                    <filter>src/main/resources/propertyfile1.txt</filter>
                    <filter>src/main/resources/propertyfile2.txt</filter>
                </filters>
            </build>
        </profile>
        <profile>
            <id>group2</id>
            <build>
                <filters>
                    <filter>src/main/resources/propertyfile3.txt</filter>
                    <filter>src/main/resources/propertyfile4.txt</filter>
                </filters>
            </build>
        </profile>
    </profiles>
    

    现在,在资源筛选中,每个概要文件提供不同的值。


    如果还需要pom中的值,则必须进行更多的处理,可能使用 maven properties plugin 或者类似的东西。将插件定义的内容放在主构建元素中:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0-alpha-2</version>
        <executions>
            <execution>
                <phase>initialize</phase>
                <goals>
                    <goal>read-project-properties</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    <profile>
        <id>group1</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>properties-maven-plugin</artifactId>
                    <configuration>
                        <files>
                            <file>src/main/resources/propertyfile1.txt</file>
                            <file>src/main/resources/propertyfile2.txt</file>
                        </files>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    
        2
  •  0
  •   Raghuram    14 年前

    我认为这可以通过使用 <pluginManagement> <插件管理> 然后在每个子项中,指定 <plugin> properties .