3

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?

Artsiom Kotau
  • 221
  • 1
  • 4
  • 11

3 Answers3

6

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...

tjuchniewicz
  • 103
  • 1
  • 7
  • Just adding dependencies without working out if you need them can throw you into dependency hell. Caveat emptor. Plus the OP has already included a configuration tag. – Adam Jul 06 '17 at 11:26
  • The OP does have the configuration tag, but it is embedded in an execution. Adding a top level configuration is enough to fix the issue. The dependencies are extras to fix other issues. Before getting to this error I had already added the maven-compat and maven-plugin-testing-harness. – david.tanner Dec 27 '17 at 21:11
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>
david.tanner
  • 529
  • 3
  • 8
  • 13
0

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)

lorenzo-bettini
  • 2,106
  • 1
  • 16
  • 11