Search code examples
javamavenmaven-plugin

Extracting licenses of dependencies from maven plugin


I'm writing a maven plugin that prints the licenses of all the project's dependencies. For that, I wrote a method that uses maven to fetch the model of each dependency.

public Model readModel(final String artifactId, final String groupId, final String version)
        throws ProjectBuildingException {
    final var artifact = this.repositorySystem.createProjectArtifact(groupId, artifactId, version);
    final ProjectBuildingResult build = this.mavenProjectBuilder.build(artifact,
            this.session.getProjectBuildingRequest());
    return build.getProject().getModel();
}

Later in my code, I pick the license from the model.

The repositorySystem is injected into the mojo via:

@Component
RepositorySystem repositorySystem;

The issue with that code is, that it only works with dependencies that are available on maven central. For other dependencies, it fails with:

Error resolving project artifact: Failure to find com.exasol:exasol-jdbc:pom:7.0.4 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced for project com.exasol:exasol-jdbc:pom:7.0.4

Is there something that I missed? I would expect that this repositorySystem uses the same repositories like maven itself that are configured in the pom.xml.

Is this the right way to solve the problem? (I'm also not happy with having the dependency injection, but could not find a way to solve this without it)


Solution

  • I just found a solution:

    @Override
        public Model readModel(final String artifactId, final String groupId, final String version)
                throws ProjectBuildingException {
        final Artifact artifactDescription = this.repositorySystem.createProjectArtifact(groupId, artifactId, version);
        final ProjectBuildingRequest projectBuildingRequest = this.session.getProjectBuildingRequest();
        projectBuildingRequest.setRemoteRepositories(this.mvnProject.getRemoteArtifactRepositories());
        final ProjectBuildingResult build = this.mavenProjectBuilder.build(artifactDescription, projectBuildingRequest);
        return build.getProject().getModel();
    }
    

    The trick was to remove the this.repositorySystem.createProjectArtifact which turned out to be overhead anyway and add the repositories of the project to the ProjectBuildingResult.