Maven插件Mojo配置默认参数值

Evg*_*pov 5 java maven-plugin maven-3 maven

我有一个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;
}     
Run Code Online (Sandbox Code Playgroud)

其他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");
    }

}
Run Code Online (Sandbox Code Playgroud)

测试中对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>
Run Code Online (Sandbox Code Playgroud)

测试失败,因为如果我一直<configuration>注释掉,所有Mojo参数都为null ,如果我取消注释,则只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;
}
Run Code Online (Sandbox Code Playgroud)

而不是lookupMojo但没有改变一点.

car*_*ing 5

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

  • 它看起来像`maven-plugin-testing-harness`还有很长的路要走. (2认同)