当父POM未定义该属性时,插件配置中的默认属性值

时间:2018-12-13 06:56:04

标签: maven maven-plugin

这是我的情况

我有一个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>

1 个答案:

答案 0 :(得分:0)

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

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

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

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