【问题标题】:Default property value in plugin configuration when parent POM not define that property当父 POM 未定义该属性时插件配置中的默认属性值
【发布时间】:2018-12-13 06:56:03
【问题描述】:

这是我的场景

我有一个 POM 文件,它使用我们开发的自定义插件让我们说 mycutom-plugin 并且该插件使用另一个名为 asciidoctor-maven-plugin 的插件

所以我会在这里记下我的担忧
1. 我在 asciidoctor-maven-plugin 中有一个参数(asciidoctor.sectnumlevels)作为配置属性,请参阅下面的 xml,此参数值由正在使用此插件的父 POM 传递。

现在,当我没有从正在使用的插件的父 POM 传递值时,我想要从 mycutom-plugin 的 POM 为 ${asciidoctor.sectnumlevels} 设置一个默认值

<plugin>
                <groupId>org.asciidoctor</groupId>
                <artifactId>asciidoctor-maven-plugin</artifactId>
                <version>${asciidoctor-maven-plugin.version} </version>
                .......
                    <attributes>
                        <toc2>left</toc2>
                ......
            <sectnumlevels>${asciidoctor.sectnumlevels}</sectnumlevels>

                    </attributes>
                </configuration>
                <executions>
                    <execution>
                        <phase>none</phase>
                        <goals>
                            <goal>process-asciidoc</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

请看我如何在父 POM 中定义属性

<properties>        
    <asciidoctor.sectnumlevels>4</asciidoctor.sectnumlevels>        
</properties>

此外,我还测试了下面的解决方案,但是这种方式采用默认值,但是当我将 POM 中的值作为属性插件端传递时,它不会覆盖

    <profiles>
    <profile>
        <id>asciidoctor.param.default</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <property>
                <name>!asciidoctor.sectnumlevels</name> <!--if not defined from parent POM-->
            </property>
        </activation>
        <properties>
            <asciidoctor.sectnumlevels>3</asciidoctor.sectnumlevels> <!--set default value-->
        </properties>
    </profile>
</profiles>

【问题讨论】:

    标签: maven maven-plugin


    【解决方案1】:

    不太确定是否了解您的设置。在我看来,您提到了 3 个 pom 文件:父、子和 mycutom-plugin。

    您不能将 mycustom-plugin pom 文件中的默认值传递给您的项目。不过,您可以在插件代码中定义默认值。

    要将默认值从父级传递给子级,您无需定义配置文件,只需在父级 pom 中定义默认值,然后在需要时覆盖子级 pom 中的相同值。 父母和孩子的声明都是以同样的方式完成的:

    <properties>        
         <asciidoctor.sectnumlevels>4</asciidoctor.sectnumlevels>        
    </properties>
    

    【讨论】:

    • 感谢您的及时回复这里有问题!!是的,正如您所说,我可以从父 POM 覆盖子 POM 中的属性值。请仔细查看我的要求,即当父 POM 没有定义时,我需要反映子属性默认值,而如果父 POM 定义,我需要通过父值覆盖子属性值!我想你明白我的意思
    • 好吧。这有点奇怪。这似乎是一个相当复杂的用例。在不知道父 pom 包含什么的情况下,您如何在子 pom 中定义属性?
    • 父 POM 使用插件并且插件有一些配置,如果父 POM 没有传递特定的覆盖值,所以插件本身应该为他的配置分配一个值
    猜你喜欢
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多