8

I try to test my maven plugin and receive weird exception. Found similar question here, but the answer didn't help.

pom.xml

<dependencies>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>3.3.9</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-project</artifactId>
        <version>3.0-alpha-2</version>
    </dependency>
    <dependency>
        <groupId>com.jcabi</groupId>
        <artifactId>jcabi-aether</artifactId>
        <version>0.10.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugin-tools</groupId>
        <artifactId>maven-plugin-annotations</artifactId>
        <version>3.4</version>
        <scope>provided</scope>
    </dependency>
    <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.apache.maven</groupId>
        <artifactId>maven-aether-provider</artifactId>
        <version>3.3.9</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-core</artifactId>
        <version>3.3.9</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-compat</artifactId>
        <version>3.3.9</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-model</artifactId>
        <version>3.3.9</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Test class:

public class ConverterMojoTest {

    @Rule
    public MojoRule rule = new MojoRule() {

        @Override
        protected void before() throws Throwable {
            super.before();
        }

        @Override
        protected void after() {
            super.after();
        }
    };

    @Rule
    public TestResources resources = new TestResources();


    @Test
    public void testExecute() throws Exception {
        File project = resources.getBasedir("valid");
        File pom = new File(project, "pom.xml");
        Assert.assertNotNull(pom);
        Assert.assertTrue(pom.exists());

        ConverterMojo mojo = (ConverterMojo) rule.lookupMojo("convert", pom);
        Assert.assertNotNull(mojo);
        mojo.execute();
    }
}

Test project pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.my.utils.test</groupId>
    <artifactId>project-to-test</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>Test</name>

    <build>
        <plugins>
            <plugin>
                <groupId>com.my.utils</groupId>
                <artifactId>converter-maven-plugin</artifactId>
                <configuration>

                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Exception:

org.codehaus.plexus.component.repository.exception.ComponentLookupException: java.util.NoSuchElementException
      role: org.apache.maven.plugin.Mojo
  roleHint: com.my.utils:converter-maven-plugin:1.0.0-SNAPSHOT:convert
Community
  • 1
  • 1
nKognito
  • 6,297
  • 17
  • 77
  • 138

6 Answers6

3

I was facing this issue in one of the modules that had proper execution configuration with the appropriate goal. I resolved this by simply running mvn clean install in that specific module (without deleting anything from the local repo).

Vijay Nandwana
  • 2,476
  • 4
  • 25
  • 42
1

Your test project's pom.xml needs to have the an <execution/> section with the respective <goal/>:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.my.utils.test</groupId>
    <artifactId>project-to-test</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Test</name>

    <build>
        <plugins>
            <plugin>
                <groupId>com.my.utils</groupId>
                <artifactId>converter-maven-plugin</artifactId>

                <configuration>
                    ...
                </configuration>

                <executions>
                    <execution>
                        <goals>
                            <goal>convert</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Otherwise, the testing harness will fail to understand what it has to map and load.

carlspring
  • 31,231
  • 29
  • 115
  • 197
  • 1
    This does not work. The same exception is thrown still. Double checked that my goal value matches as well. – MMP Sep 21 '17 at 01:48
1

Just update the maven project and mvn clean install. It will work

1

open your location

File | Settings | Build, Execution, Deployment | Build Tools | Maven

Change "maven home path:" to Use Maven wrapper here. This is how my problem was solved.enter image description here

0

I had the same issue, followed all of the advice but still had the issue. I solved it by clearing out my local repo then doing a mvn clean install -DskipTests from terminal. Then I ran my jUnit test from Eclipse and it worked. Must have been a conflicting dependency somewhere.

Bobby King
  • 141
  • 1
  • 8
0

I had a similar issue working with jmeter maven plugin. In my case, because I was using about 15 third part plugins for integration jmeter<->maven, I had the need to build and install those plugins separately in a local repository, different by default repository of maven: ${user.home}/.m2/repository, and then I had to refer each one of them in pom.xml as dependencies, specifying the repository too.

During the build and the resolving of artifact for each one of them I was facing the issue:
org.codehaus.plexus.component.repository.exception.ComponentLookupException: java.util.NoSuchElementException . I solved the issue by deciding to install those third part plugin in default repository too: ${user.home}/.m2/repository, and then specifying the new repository in pom.xml:

`

<repository>

    <id>project-repo</id>

    <url>file://C:/Users/myuser/.m2/repository</url>

</repository>

`

This worked fine to me.

Tocotti
  • 11
  • 3