【问题标题】:Transitive maven plugin dependency configuration传递maven插件依赖配置
【发布时间】:2018-03-21 14:45:20
【问题描述】:

如果我有两个 maven 插件,其中 A 依赖于 BB 有配置参数,我应该如何以及在哪里为 B 定义配置?

我可以想到三种解决方案,我知道哪一种不适用,我不确定哪一种可行,或者还有其他可能性。不幸的是,即使是这个简单的案例,我也找不到任何示例。

试试 1(不可能)

<configuration> 放在依赖项下。这是不可能的,因为 <dependency> 标记不能包含 <configuration> 标记 (see xsd)。

<plugin>
    <groupId>A</groupId>
    <artifactId>A</artifactId>
    <version>1.0.0</version>
    <dependencies>
        <dependency>
            <groupId>B</groupId>
            <artifactId>B</artifactId>
            <version>1.0.0</version>
            <configuration>
                <paramForB><param>bbb</param></paramForB>
            </configuration>
        </dependency>
    </dependencies>
</plugin>

试试 2

把配置参数放到A的config下。

<plugin>
    <groupId>A</groupId>
    <artifactId>A</artifactId>
    <version>1.0.0</version>
    <dependencies>
        <dependency>
            <groupId>B</groupId>
            <artifactId>B</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
    <configuration>
        <paramForB><param>bbb</param></paramForB>
    </configuration>
</plugin>

试试 3

B 作为一个单独的插件引入我需要的配置。

<plugin>
    <groupId>B</groupId>
    <artifactId>B</artifactId>
    <version>1.0.0</version>
    <configuration>
        <paramForB><param>bbb</param></paramForB>
    </configuration>
</plugin>
<plugin>
    <groupId>A</groupId>
    <artifactId>A</artifactId>
    <version>1.0.0</version>
    <dependencies>
        <dependency>
            <groupId>B</groupId>
            <artifactId>B</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
</plugin>

更多问题(只是好奇)

  1. 另外,如果B 依赖于C 会怎样。我应该如何配置C
  2. 如果我有一个 maven 插件 X,它也依赖于 B,但会与其他配置一起使用:我可以这样做吗?

【问题讨论】:

  • 如果你有两个插件,它们就不能相互依赖......此外,插件的依赖关系是不同的......你只能给插件配置,但不能给依赖关系。也许你可以举一个更具体的例子来完成你想要完成的事情......
  • 感谢您的评论,我已经阅读了有关该主题的更多信息并找到了答案。

标签: maven


【解决方案1】:

在 Maven 中,您有配置文件和属性。 先从属性说起:

项目 B

<properties>
   <customeParameter>parameterInB</customeParameter>
</properties>
<plugin>
    <groupId>A</groupId>
    <artifactId>A</artifactId>
    <version>1.0.0</version>
    <dependencies>
        <dependency>
            <groupId>B</groupId>
            <artifactId>B</artifactId>
            <version>1.0.0</version>
            <configuration>
                <paramForB>${customeParameter}</paramForB>
            </configuration>
        </dependency>
    </dependencies>
</plugin>

项目 A

<properties>
   <customeParameter>overrideInA</customeParameter>
</properties>

查看我对问题的回答:How to get a command-line property to overwrite a maven property

如果项目 B 是项目 A 的父级,我的示例将起作用。如果不是,请在父级 pom 中定义共享配置(它将成为 A 和 B 的父级)并对其进行参数化。然后在孩子中,您可以在“属性”部分中定义此配置的值。

【讨论】:

  • 感谢您的回答!我不知道只能在命令行中覆盖属性。 Maven 说,我不能在 标签下定义 标签,我已经尝试过了。与此同时,我对 maven 插件的工作原理有了更深入的了解,因此我发布了我一直在寻找的答案。
【解决方案2】:

答案基本上是尝试2,即在插件用法A中添加B的配置,但这取决于插件A是如何实现的,因为它有责任通过参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 2011-05-05
    • 2013-02-17
    • 2014-04-25
    • 2019-08-23
    • 2019-10-29
    相关资源
    最近更新 更多