Why does Maven Embedded throws error on my Maven project?

182 views Asked by At

I have a simple Maven project I use for IT tests and I'm trying to make it compile as part of my tests.

I'm trying to use this code:

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

But I'm getting this error:

[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: 

Here are the relevant parts of the project 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>

Ths is ran on JDK 11, Maven 3.9.2. Any thoughts?

I've tried multiple versions of the dependencies and looking online for answers without any success.

1

There are 1 answers

1
Jtvd78 On BEST ANSWER

I debugged Maven itself to determine the implementation that mvn uses. You need to include maven-compat on your classpath to get a RepositorySystem implementation. That is what mvn uses.

You will also need:

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://)

As they are required to actually download artifacts.

Note that maven-compat seems to be going away in Maven 4.x so I am not sure how long this pattern will last. However, for Maven 3.9.x this seems like the way to go.