为什么 Maven Embedded 在我的 Maven 项目上抛出错误?

问题描述 投票:0回答:1

我有一个简单的 Maven 项目,用于 IT 测试,我正在尝试将其编译为我的测试的一部分。

我正在尝试使用此代码:

    System.setProperty("maven.multiModuleProjectDirectory", projectRoot.getAbsolutePath());
    MavenCli cli = new MavenCli();
    cli.doMain(
        new String[] {"clean","install"}, projectRoot.getAbsolutePath(), System.out, System.err);

但我收到此错误:

[2023-05-31 16:52:02.450] [ERROR] [main] o.a.m.c.MavenCli [CLIReportingUtils.java:142] Error executing Maven.
[2023-05-31 16:52:02.452] [ERROR] [main] o.a.m.c.MavenCli [CLIReportingUtils.java:145] com.google.inject.ProvisionException: Unable to provision, see the following errors:

1) No implementation for RepositorySystem was bound.
  while locating DefaultProjectBuildingHelper
  at ClassRealm[maven.ext, parent: ClassRealm[plexus.core, parent: null]]
      \_ installed by: WireModule -> PlexusBindingModule
  while locating ProjectBuildingHelper
  while locating DefaultProjectBuilder
  at ClassRealm[maven.ext, parent: ClassRealm[plexus.core, parent: null]]
      \_ installed by: WireModule -> PlexusBindingModule
  while locating ProjectBuilder
  while locating DefaultMaven
  at ClassRealm[maven.ext, parent: ClassRealm[plexus.core, parent: null]]
      \_ installed by: WireModule -> PlexusBindingModule
  while locating Maven

1 error

======================
Full classname legend:
======================
DefaultMaven:                 "org.apache.maven.DefaultMaven"
DefaultProjectBuilder:        "org.apache.maven.project.DefaultProjectBuilder"
DefaultProjectBuildingHelper: "org.apache.maven.project.DefaultProjectBuildingHelper"
Maven:                        "org.apache.maven.Maven"
PlexusBindingModule:          "org.eclipse.sisu.plexus.PlexusBindingModule"
ProjectBuilder:               "org.apache.maven.project.ProjectBuilder"
ProjectBuildingHelper:        "org.apache.maven.project.ProjectBuildingHelper"
RepositorySystem:             "org.apache.maven.repository.RepositorySystem"
WireModule:                   "org.eclipse.sisu.wire.WireModule"
========================
End of classname legend:
========================

      role: org.apache.maven.Maven
  roleHint: 

以下是项目POM的相关部分:

 <dependencies>
    <dependency>
      <groupId>org.eclipse.jgit</groupId>
      <artifactId>org.eclipse.jgit</artifactId>
      <version>6.5.0.202303070854-r</version>
      <exclusions>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.gitlab4j</groupId>
      <artifactId>gitlab4j-api</artifactId>
      <version>5.2.0</version>
      <exclusions>
        <exclusion>
          <groupId>jakarta.activation</groupId>
          <artifactId>jakarta.activation-api</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.javassist</groupId>
          <artifactId>javassist</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.apache.httpcomponents</groupId>
          <artifactId>httpclient</artifactId>
        </exclusion>
        <exclusion>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-core</artifactId>
        </exclusion>
        <exclusion>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-annotations</artifactId>
        </exclusion>
        <exclusion>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
        </exclusion>
        <exclusion>
          <groupId>jakarta.xml.bind</groupId>
          <artifactId>jakarta.xml.bind-api</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.28</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.8.0</version>
      <exclusions>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>5.3.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-embedder</artifactId>
      <version>3.9.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

它在 JDK 11、Maven 3.9.2 上运行。有什么想法吗?

我尝试了多个版本的依赖项,并在网上寻找答案,但没有成功。

java-11 maven-embedder
1个回答
0
投票

我调试了 Maven 本身以确定

mvn
使用的实现。您需要在类路径中包含
maven-compat
才能获得
RepositorySystem
实现。这就是
mvn
使用的。

您还需要:

org.apache.maven.resolver:maven-resolver-connector-basic (to support below two deps)
org.apache.maven.resolver:maven-resolver-transport-http (for http://)
org.apache.maven.resolver:maven-resolver-transport-file (for file://)

因为他们需要实际下载工件。

© www.soinside.com 2019 - 2024. All rights reserved.