[英]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.