Maven插件Mojo配置默认参数值

时间:2016-01-04 23:17:08

标签: java maven maven-3 maven-plugin

我有一个maven插件和一个看起来有点接近

的简单Mojo
public abstract class AbstractSetupMojo extends AbstractMojo {
    @Parameter(property="targetHost", defaultValue="localhost") 
    private String targetHost;
    @Parameter(property="targetPort", defaultValue="27017")
    private Integer targetPort;
    @Parameter(property="targetDbName", required=true) 
    private String targetDbName;
    @Parameter(property="sourceHost", defaultValue="${mojo.configuration.targetHost}") 
    private String sourceHost;
    @Parameter(property="sourcePort", defaultValue="${mojo.configuration.targetPort}")
    private Integer sourcePort;
    @Parameter (property="sourceDbName", defaultValue="${mojo.configuration.targetDbName}")
    private String sourceDbName;
    @Parameter(property="scriptsPath")
    private File scriptsPath;
}     

其他Mojos正在延伸这一个。因此,我们的想法是将source*参数设置为相应target*参数的值。还设置了一些默认值。

在测试中,我有类似

的内容
public class GenerateMojoTest extends AbstractMojoTestCase {

    protected void setUp() throws Exception {
        super.setUp();
    }

    public void testConfiguration() throws Exception {
        File pom = getTestFile("src/test/resources/test-project-1/pom.xml");
        assertNotNull(pom);
        assertTrue(pom.exists());
        GenerateMojo generateMojo = (GenerateMojo)lookupMojo("generate", pom);
        assertThat(generateMojo.getSourceDbName()).isEqualTo(generateMojo.getTargetDbName());
        assertThat(generateMojo.getSourcePort()).isEqualTo(generateMojo.getTargetPort()).isEqualTo(27017);
        assertThat(generateMojo.getSourceHost()).isEqualTo(generateMojo.getTargetHost()).isEqualTo("localhost");
    }

}

测试中对POM文件感兴趣的部分看起来像

        <plugin>
            <groupId>com.ffy</groupId>
            <artifactId>setup-maven-plugin</artifactId>
    <!--                
            <configuration>
                <scriptsPath>src/test/resources/temp</scriptsPath>
            </configuration>
    -->                
            <executions>
                <execution>
                    <id>generate</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

测试失败,因为如果我将<configuration>注释掉,则所有Mojo参数都为空,如果我取消注释,则只设置scriptsPath。其他参数为null

我是否必须在测试中执行其他操作才能拥有完全配置的Mojo?

我尝试了更长的方法

protected GenerateMojo setupMojo(final File pom) throws ComponentConfigurationException, Exception {
    final MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest();
    final ProjectBuildingRequest buildingRequest = executionRequest.getProjectBuildingRequest();
    final ProjectBuilder projectBuilder = this.lookup(ProjectBuilder.class);
    final MavenProject project = projectBuilder.build(pom, buildingRequest).getProject();
    final MavenSession session = newMavenSession(project);
    final MojoExecution execution = newMojoExecution("generate");
    final GenerateMojo mojo = (GenerateMojo) this.lookupConfiguredMojo(session, execution);
    return mojo;
}

而不是lookupMojo,但这并没有改变。

1 个答案:

答案 0 :(得分:5)

你需要拥有<configuration/>部分并定义你感兴趣的值。你会期望它更聪明,但实际上测试工具的作用是,它从中读取值<configuration/>并忽略注释中的那些。测试工具有很多不足之处,它实际上并不像你正确的Maven执行/插值那样为你加载值...因此,我建议使用maven-invoker-plugin,如果它更适合你的需要。