java Maven 插件测试中 org.apache.maven.repository.RepositorySystem 的组件查找异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15779351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:49:58  来源:igfitidea点击:

component lookup exception with org.apache.maven.repository.RepositorySystem in Maven plugin testing

javamaven-3maven-plugin

提问by yegor256

I'm trying to use maven-plugin-testing-harnessversion 2.1 with the following test case:

我正在尝试将maven-plugin-testing-harness2.1 版与以下测试用例一起使用:

public class FooTest extends AbstractMojoTestCase {
  @Override
  protected void setUp() throws Exception {
    super.setUp();
  }
  public void testSomething() throws Exception {
    // todo
  }
}

The test fails at the setUp()call:

setUp()调用时测试失败:

org.codehaus.plexus.component.repository.exception.ComponentLookupException: java.util.NoSuchElementException
  role: org.apache.maven.repository.RepositorySystem
roleHint: 
    at org.codehaus.plexus.DefaultPlexusContainer.lookup(DefaultPlexusContainer.java:257)
    at org.codehaus.plexus.DefaultPlexusContainer.lookup(DefaultPlexusContainer.java:245)
    at org.codehaus.plexus.DefaultPlexusContainer.lookup(DefaultPlexusContainer.java:239)
    at org.codehaus.plexus.PlexusTestCase.lookup(PlexusTestCase.java:206)
    at org.apache.maven.plugin.testing.AbstractMojoTestCase.setUp(AbstractMojoTestCase.java:118)
    at foo.FooTest.setUp(FooTest.java:54)

These dependencies I have in the pom.xml:

这些依赖项我在pom.xml

    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>3.0.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-model</artifactId>
        <version>3.0.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-core</artifactId>
        <version>3.0.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugin-testing</groupId>
        <artifactId>maven-plugin-testing-harness</artifactId>
        <version>2.1</version>
        <scope>test</scope>
    </dependency>

Any ideas?

有任何想法吗?

回答by smoke

Recently I faced the same exception. After a bit researching I found that maven-compat plugin solves the problem:

最近我遇到了同样的异常。经过一番研究,我发现 maven-compat 插件解决了这个问题:

<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-compat</artifactId>
    <version>3.0.5</version>
    <scope>test</scope>
</dependency>

回答by Rob

Leaving this here for anyone who runs into this problem in the future:

把这个留在这里给将来遇到这个问题的人:

smoke's answer does work, but make sure the versions of the dependencies included in yegor256's in the original question match. Adding org.apache.maven:maven-compat did not work for me until I had changed those 4 dependencies to also have version 3.0.5.

烟雾的答案确实有效,但请确保原始问题中 yegor256 中包含的依赖项版本匹配。添加 org.apache.maven:maven-compat 对我不起作用,直到我将这 4 个依赖项更改为版本 3.0.5。

回答by Ismael Sarmento

Currently, the class org.apache.maven.repository.RepositorySystemis found in the lib maven-resolver-api. If after adding maven-compat, anybody here still gets the error, make sure to add maven-resolver-api artifact as dependency as well.

目前,类org.apache.maven.repository.RepositorySystem位于 lib maven-resolver-api 中。如果添加 maven-compat 后,这里的任何人仍然收到错误,请确保也添加 maven-resolver-api 工件作为依赖项。

<dependency>
  <groupId>org.apache.maven.resolver</groupId>
  <artifactId>maven-resolver-api</artifactId>
  <version>1.3.1</version>
</dependency>