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

gvd*_*vdm 2 java liquibase maven

我有一个Maven项目,其中包含一个Maven插件(Liquibase Maven插件),它暴露了不同的目标.其中两个目标(update和diff)需要不同的参数,它们之间存在冲突(因为两者的语义不同),所以我需要在两个目标执行中给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>
Run Code Online (Sandbox Code Playgroud)

但是,此配置是错误的,因为对于每个目标(diff,更新其他目标),仅使用该liquibaseDiff.properties文件.

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

Tun*_*aki 8

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

  • 全球所有处决.全局配置使用下面的<configuration>元素完成<plugin>.此配置由所有执行继承.
  • 每次执行.这是使用下面的<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>
Run Code Online (Sandbox Code Playgroud)

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

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

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


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

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

  • 在命令行上执行特定的执行,即您配置的执行.这是可能的,因为Maven 3.3.1并且您将执行

    mvn liquibase:diff@diff
    
    Run Code Online (Sandbox Code Playgroud)

    @diff在上述命令是指独特<id>的是在POM中配置的执行.

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