针对不同目标的不同Maven配置 [英] Different Maven configurations for different goals

查看:109
本文介绍了针对不同目标的不同Maven配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含Maven插件( Liquibase Maven插件)的Maven项目.揭示了不同的目标. 这些目标中的两个(更新和差异)需要不同的参数,这两个参数之间存在冲突(因为两者的语义不同),因此我需要在两个目标执行中赋予Maven不同的属性.

I have a Maven project which includes a Maven plugin (the Liquibase Maven plugin) which exposes different goals. Two of these goals (update and diff) need different parameters which are in conflict between them (because the semantics of the two is different), so I need to give Maven different properties in the two goal executions.

这就是我所做的

<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文件.

However, this configuration is wrong because for each goal (diff, update of the others) only the liquibaseDiff.properties file is used.

在Maven中有什么方法可以传递不同的配置以实现不同的目标吗?

Is there any way to pass different configurations for different goals in Maven?

推荐答案

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

Configuration of plugins can be done in two different locations:

  • Globally for all executions. The global configuration is done with the <configuration> element under <plugin>. This configuration in inherited by all executions.
  • Per execution. This is done using the <configuration> element under <execution>.

在您的示例中,考虑以下POM:

In your example, consider this 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没有元素,则父值成为有效值.

The default inheritance behavior is to merge the content of the configuration element according to element name. If the child POM has a particular element, that value becomes the effective value. If the child POM does not have an element, but the parent does, the parent value becomes the effective value.

如果发生冲突,则可以使用 combine.childrencombine.self .引用Maven文档:

In case of conflicts, you can control the default inheritance performed by Maven using combine.children and combine.self. Quoting the Maven docs:

combine.children="append"导致父元素和子元素按此顺序串联.另一方面,combine.self="override"完全禁止了父配置.

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.


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


In addition to this, you need to be aware that when executing a Maven command, on the command line, that directly invokes a goal, such as mvn liquibase:diff, it creates a new execution with an id that is default-cli. As such, since the specific configuration above of the goal diff is done in an execution with id diff, it will not be used. This is actually normal, since the same goal of the same plugin could be present in multiple execution blocks with different configuration: which one should be used if it is executed on the command line, without additional information?

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

Typically, this situation is solved in 2 manners:

mvn liquibase:diff@diff

上面命令中的@diff表示在POM中配置的执行的唯一<id>.

The @diff in the command above refers to the unique <id> of the execution that is configured in the POM.

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

Bind your execution to a specific phase of the Maven lifecycle, and let it be executed with the normal flow of the lifecycle. This is generally the prefered solution. In the example above, we could, for example, add a <phase>test</phase> in the execution block of the diff execution; and then Maven will execute it when the test phase is ran during the course of the build.

这篇关于针对不同目标的不同Maven配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆