拥有嵌套复杂对象和默认值的自己的maven插件

时间:2017-08-15 06:58:40

标签: maven maven-3 maven-plugin

在开发我的一些maven插件时,我想到了一种更有条理的方式来提供配置,但我不确定有哪种方法可行。

是否有可能拥有一个MOJO,其中有BaseSettingsMavenSettings的实例以这种方式使用,或者我被强迫进行" polute"这些字段直接进入我的MOJO?

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Parameter;

public abstract class SomeSpecialAbstractMojo extends AbstractMojo{

    @Parameter
    protected BaseSettings baseSettings = new BaseSettings();

    @Parameter
    protected MavenSettings mavenSettings = new MavenSettings();
}

-

import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;

public class MavenSettings {

    @Parameter(name = "project", property = "project", readonly = true, required = true)
    protected MavenProject project;

    @Component
    protected MavenProjectHelper projectHelper;
}

-

import org.apache.maven.plugins.annotations.Parameter;

public class BaseSettings {

    @Parameter(name = "skip", defaultValue = "false")
    protected boolean skip = false;

    @Parameter(name="verbose", defaultValue = "true")
    protected boolean verbose = true;
}

我们的想法是将一些通用属性捆绑到他们自己的" space"中,这使我的MOJO更小,而且我的代码更有条理。

修改 我想要这种执行配置:

<configuration>
    <baseSettings>
        <verbose>true</verbose>
    </baseSettings>
    <!-- mavenSettings is omitted, I want to "auto-use-default" here -->
    <otherComplexSettings>
        <complexValue1>value1</complexValue1>
    </otherComplexSettings>
</configuration>
<executions>
    <execution>
        <id>goal1-call</id>
        <goals>
            <goal>mojo1</goal>
        </goals>
        <configuration>
            <otherComplexSettings>
                <complexValue1>override-value1</complexValue1>
            </otherComplexSettings>
        </configuration>
    </execution>
    <execution>
        <id>goal2-call</id>
        <goals>
            <goal>mojo2</goal>
        </goals>
        <configuration>
            <!-- only part of mojo2 -->
            <anotherComplexSettings>
                <complexValue3>value3</complexValue3>
            </anotherComplexSettings>
        </configuration>
    </execution>
    <execution>
        <id>goal3-call</id>
        <goals>
            <goal>mojo3</goal>
        </goals>
        <configuration>
            <otherComplexSettings>
                <complexValue1>override-value012</complexValue1>
            </otherComplexSettings>
        </configuration>
    </execution>
    <execution>
        <id>goal4-call</id>
        <goals>
            <goal>mojo4</goal>
        </goals>
    </execution>
</executions>

1 个答案:

答案 0 :(得分:0)

不确定是否可以回答您的问题,但是您可以使用自己的POJO作为参数。这是example。您也可以使用属性或地图。