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

标签 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 中为不同的目标传递不同的配置吗?

最佳答案

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

  • 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 没有元素,但父 POM 有,则父值成为有效值。

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

combine.children="append" results in the concatenation of parent and child elements, in that order. combine.self="override", on the other hand, completely suppresses parent configuration.


除此之外,您需要注意,在命令行上执行 Maven 命令时,会直接调用一个目标,例如 mvn liquibase:diff ,它会创建一个 ID 为 default-cli 执行.因此,由于上面目标的具体配置 diff在 ID 为 diff 的执行中完成,它不会被使用。这实际上是正常的,因为同一个插件的相同目标可能出现在具有不同配置的多个执行 block 中:如果在命令行上执行,应该使用哪个,没有附加信息?

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

  • 在命令行上执行特定的执行,即您配置的那个。 This is possible since Maven 3.3.1然后你会执行

    mvn liquibase:diff@diff
    

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

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

关于java - 针对不同目标的不同 Maven 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33146819/

相关文章:

java - SQL语法错误异常 : ORA-00911: invalid character

java - 将用户输入存储在数组中

java - 如何在 LDAP 中使用 DN 和密码在 Java 中进行绑定(bind)?

java - 使用图像作为按钮,获得空白屏幕

sql - addUniqueConstraint 与 liquibase 中的 where 子句

mysql - Liquibase 无法连接到 SQL Server

maven - 无需重建即可部署 Maven Artifact

java - Maven/Nexus - 自动依赖更新

java - Apache Storm 应用程序运行失败

java - spring-boot 应用程序重启时 HSQL DB 丢失