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?

4
  • Did you resolve the situation?
    – Adam
    Jul 6, 2017 at 11:27
  • Unfortunately no. I made workaround. Jul 9, 2017 at 11:05
  • Yes, I sympathise. I looked at the source code. It's very simple, hard-coded, unconfigurable. But easy enough to modify to your own purposes if you decided to fork the source.
    – Adam
    Jul 9, 2017 at 11:26
  • Add an empty configuration block to the top level of your plugin config. Dec 27, 2017 at 21:13

3 Answers 3

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

2
  • 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 6, 2017 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. Dec 27, 2017 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>
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.