Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.codehaus.plexus.component.repository.exception.ComponentLookupException: java.util.NoSuchElementException

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
like image 724
nKognito Avatar asked Dec 30 '15 12:12

nKognito


3 Answers

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

like image 113
Vijay Nandwana Avatar answered Nov 12 '22 23:11

Vijay Nandwana


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.

like image 43
carlspring Avatar answered Nov 13 '22 01:11

carlspring


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

like image 1
Shruti Sarda Avatar answered Nov 13 '22 00:11

Shruti Sarda