针对不同目标的不同Maven配置

时间:2015-10-15 11:04:12

标签: java maven liquibase

我有一个Maven项目,其中包含一个Maven插件(Liquibase Maven plugin),它暴露了不同的目标。 其中两个目标(更新和差异)需要不同的参数,它们之间存在冲突(因为两者的语义不同),所以我需要在两个目标执行中给Maven不同的属性。

这就是我所做的

<plugin>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-maven-plugin</artifactId>
    <version>3.4.1</version>

    <!-- This configuration is used for every goal except "diff" -->
    <configuration>
        <propertyFile>src/main/resources/liquibase.properties</propertyFile>
        <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
    </configuration>
    <executions>
        <execution>
            <id>default-cli</id>
            <goals>
                <goal>diff</goal>
            </goals>
            <!-- This configuration is used for the "diff" goal -->
            <configuration>
                <propertyFile>src/main/resources/liquibaseDiff.properties</propertyFile>
                <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
            </configuration>
        </execution>
    </executions>
</plugin>

但是,这种配置是错误的,因为对于每个目标(差异,更新其他目标),只使用liquibaseDiff.properties文件。

有没有办法在Maven中为不同的目标传递不同的配置?

1 个答案:

答案 0 :(得分:7)

插件的配置可以在两个不同的位置完成:

  • Globally for all executions。全局配置使用<configuration>下的<plugin>元素完成。此配置由所有执行继承。
  • Per execution。这是使用<configuration>下的<execution>元素完成的。

在您的示例中,请考虑此POM:

<plugin>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-maven-plugin</artifactId>
    <version>3.4.1</version>
    <configuration>
        <!-- put the configuration here that is common to all executions -->
    </configuration>
    <executions>
        <execution>
            <id>diff</id>
            <goals>
                <goal>diff</goal>
            </goals>
            <configuration>
                <!-- put the specific configuration of the diff goal here, this will inherit from the global configuration -->
            </configuration>
        </execution>
        <execution>
            <id>update</id>
            <goals>
                <goal>update</goal>
            </goals>
            <configuration>
                <!-- put the specific configuration of the update goal here, this will inherit from the global configuration -->
            </configuration>
        </execution>
    </executions>
</plugin>

默认继承行为是根据元素名称合并配置元素的内容。如果子POM具有特定元素,则该值将成为有效值。如果子POM没有元素,但是父元素,则父值变为有效值。

如果发生冲突,您可以使用combine.children and combine.self控制Maven执行的默认继承。引用Maven文档:

  

combine.children="append"按顺序导致父元素和子元素的串联。另一方面,combine.self="override"完全抑制父配置。

除此之外,您还需要注意,在命令行上执行Maven命令时,会直接调用目标,例如mvn liquibase:diff,它会创建 new 使用default-cli的ID执行。因此,由于目标diff的上述特定配置是在ID为diff的执行中完成的,因此不会使用它。这实际上是正常的,因为相同插件的相同目标可能存在于具有不同配置的多个执行块中:如果在命令行上执行,则应该使用哪一个,而没有其他信息?

通常,这种情况以两种方式解决:

  • 在命令行上执行特定的执行,即您配置的执行。 This is possible since Maven 3.3.1您将执行

    mvn liquibase:diff@diff
    

    上面命令中的@diff指的是在POM中配置的执行的唯一<id>

  • 将您的执行绑定到Maven生命周期的特定阶段,并让它以生命周期的正常流程执行。这通常是首选的解决方案。在上面的示例中,我们可以在<phase>test</phase>执行的执行块中添加diff;然后Maven将在构建过程中运行测试阶段时执行它。

相关问题