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

时间:2010-10-11 22:45:47

标签: maven-2 maven

[简化为简化]

  • 我有一个包含多个Maven模块的项目
  • 模块具有属性文件,其内容在构建期间被过滤
  • 可以分组的属性集
  • 给定版本中只存在一组属性
  • 所有组都具有相同的属性,只有它们的值在组之间发生变化
  • parent
    +- pom.xml
    +- foo
       +- src/main/resources/test.properties
    

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

    mvn clean compile -Dgroup1=true
    

    问题是我必须在组之间复制整个配置文件。我只想为每个组定义<properties>,并使用单个配置文件来控制构建顺序,插件和其他内容。

    任何人都知道是否可以这样做?

    2 个答案:

    答案 0 :(得分:2)

    听起来我觉得你只想为每个配置文件创建一组不同的属性文件,如下所示:

    <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>
    

    答案 1 :(得分:0)

    我认为这可以通过<pluginManagement>来实现。您可以在<pluginManagement>部分的父pom中声明构建顺序,插件和其他内容,然后在每个子项中,您可以将<plugin>与相应的properties进行简化。

    相关问题