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

时间:2015-12-30 12:27:25

标签: java maven maven-3 maven-plugin

我尝试测试我的maven插件并收到奇怪的异常。找到了类似的问题here,但答案没有帮助。

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

测试类:

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();
    }
}

测试项目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>

例外:

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

4 个答案:

答案 0 :(得分:0)

您的测试项目pom.xml需要<execution/>部分与相应的<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>

否则,测试工具将无法理解映射和加载的内容。

答案 1 :(得分:0)

我在具有适当执行配置和适当目标的模块之一中遇到了此问题。我通过在该特定模块中运行mvn clean install(而不从本地存储库删除任何内容)来解决了这个问题。

答案 2 :(得分:0)

只需更新Maven项目并执行mvn全新安装。会起作用

答案 3 :(得分:-1)

我遇到了同样的问题,遵循了所有的建议,但仍有问题。我通过清除我的本地仓库然后从终端执行mvn clean install -DskipTests来解决它。然后我从Eclipse运行了我的jUnit测试,它运行起来了。在某处肯定是一个冲突的依赖。