简体   繁体   English

Maven插件Mojo配置默认参数值

[英]Maven Plugin Mojo Configure Default Parameter Values

I have a maven plugin and a simple Mojo that looks somewhat close to 我有一个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;
}     

Other Mojos are extending this one. 其他Mojos正在扩展这一功能。 So, the idea is to set the source* parameters to the values of the corresponding target* parameters. 因此,我们的想法是将source*参数设置为相应target*参数的值。 Also some default values are set. 还设置了一些默认值。

In the test I have something like 在测试中,我有类似

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

}

The part of interest in the POM file in the test looks like 测试中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>

The test fails because all the Mojo parameters are null if I keep <configuration> commented out, if I uncomment it then only scriptsPath is set. 测试失败,因为如果我将<configuration>注释掉,则所有Mojo参数都为null;如果取消注释,则仅设置scriptsPath Other parameters are null . 其他参数为null

Do I have to do something else in my tests in order to have a fully configured Mojo ? 为了拥有完整配置的Mojo,我必须在测试中做其他事情吗?

I tried longer approach with 我尝试了更长的方法

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;
}

instead of lookupMojo but that didn't change a bit. 而不是lookupMojo但是并没有改变。

You will need to have the <configuration/> part and define the values you're interested in. You would expect it to be a little smarter, but in fact what the testing harness does is, it reads the values from the <configuration/> and ignores the ones from your annotations. 您将需要具有<configuration/>部分并定义您感兴趣的值。您希望它会更聪明一些,但是实际上,测试工具所做的是,它从<configuration/>并忽略注释中的内容。 The testing harness leaves a lot to be desired, it doesn't actually load the values for you like a proper Maven execution/interpolation... Hence, I would advise using the maven-invoker-plugin , if it better suits your needs. 测试工具还有很多不足之处,它实际上并不会像正确的Maven执行/内插那样为您加载值...因此,我建议您使用maven-invoker-plugin ,如果它更适合您的需求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在项目上执行目标 org.codehaus.mojo:exec-maven-plugin:1.6.0:exec(默认):参数“可执行文件”丢失或无效 - Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:exec (default) on project ors: The parameter 'executable' is missing or invalid 如何为具有Maven插件的多个值的参数配置默认值 - How to configure defaults for a parameter with multiple values for a Maven plugin Jetty,Maven插件-如何配置默认文档? - Jetty, Maven plugin - how to configure default document? Maven MOJO-在另一个参数中使用一个参数 - Maven MOJO - use a parameter in another parameter 无法执行目标 org.codehaus.mojo:exec-maven-plugin:3.0.0:exec (default-cli) - Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:exec (default-cli) 无法执行目标org.codehaus.mojo:tomcat-maven-plugin:1.1:deploy(default-cli) - Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:deploy (default-cli) 在Maven Mojo插件问题上需要帮助 - Need help on maven mojo plugin issue 如何从Maven中的另一个插件继承Mojo - How to inherit a Mojo from another plugin in Maven 使用Maven Plugin参数值作为另一个参数的默认值 - Use Maven Plugin parameter value as default value of another parameter 无法在项目上执行目标org.codehaus.mojo:exec-maven-plugin:1.2.1:exec(default-cli):命令执行失败 - Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project: Command execution failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM