问题
Let's imagine I have following mojo:
@Mojo(name = "some-goal")
public class MyMojo {
@Parameter(required = true)
protected ComplexObject param;
/*...*/
}
Also I have plugin's descriptor in pom:
<plugin>
<!-- here artifact description -->
<executions>
<execution>
<phase>...</phase>
<goals><goal>some-goal</goal></goals>
<configuration>
<param>...</param>
</configuration>
</execution>
</executions>
</plugin>
For test this plugin I use maven-plugin-testing-harness
And my test code is:
@Test
public void test() throws Exception {
File pom = getFile("mix/pom.xml");
MyMojo plugin = (MyMojo) rule.lookupMojo("some-goal", pom);
/*....*/
}
Where rule is:
@Rule
public MojoRule rule = new MojoRule() {
@Override
protected void before() throws Throwable {
}
@Override
protected void after() {
}
};
But when I run test it fails with Exception:
org.apache.maven.plugin.testing.ConfigurationException: Cannot find a configuration element for a plugin with an artifactId of {plugin-name}.
at org.apache.maven.plugin.testing.AbstractMojoTestCase.extractPluginConfiguration(AbstractMojoTestCase.java:619)
at org.apache.maven.plugin.testing.AbstractMojoTestCase.extractPluginConfiguration(AbstractMojoTestCase.java:582)
at org.apache.maven.plugin.testing.AbstractMojoTestCase.lookupMojo(AbstractMojoTestCase.java:353)
at org.apache.maven.plugin.testing.MojoRule.lookupMojo(MojoRule.java:164)
When I debug source of maven-plugin-testing-harness I noticed that it read configuration from root plugin element only.
How can I force it to read configuration from execution element?
回答1:
Adding empty <configuration></configuration>
block to test plugin configuration helped me.
Try to use these deps:
<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-annotations</artifactId>
<version>1.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>3.3.9</version>
</dependency>
Maven plugin testing is not well described and looks buggy...
回答2:
There are two ways to fix this issue.
Change the call lookupMojo("some-goal", pom)
to lookupEmptyMojo("some-goal", pom)
Or inside build -> plugins -> plugin
add an empty <configuration></configuration>
section.
<plugin>
<!-- here artifact description -->
<configuration></configuration>
<executions>
<execution>
<phase>...</phase>
<goals><goal>some-goal</goal></goals>
<configuration>
<param>...</param>
</configuration>
</execution>
</executions>
</plugin>
回答3:
How did you specify the part
<!-- here artifact description -->
Did you specify the groupId and the artifactId of your plugin? If that's not the case the configuration part is not used. This might be related to what's generated by the archetype which is not completely correct (https://issues.apache.org/jira/projects/MARCHETYPES/issues/MARCHETYPES-67?filter=allopenissues)
来源:https://stackoverflow.com/questions/38243144/maven-get-goal-configuration-from-execution-element