001/*
002 * JDrupes Builder
003 * Copyright (C) 2025 Michael N. Lipp
004 * 
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.builder.mvnrepo;
020
021import java.io.File;
022import java.util.List;
023import org.apache.maven.model.Dependency;
024import org.apache.maven.model.Parent;
025import org.apache.maven.model.Repository;
026import org.apache.maven.model.building.FileModelSource;
027import org.apache.maven.model.building.ModelSource;
028import org.apache.maven.model.resolution.ModelResolver;
029import org.apache.maven.model.resolution.UnresolvableModelException;
030import org.eclipse.aether.RepositorySystem;
031import org.eclipse.aether.RepositorySystemSession;
032import org.eclipse.aether.artifact.DefaultArtifact;
033import org.eclipse.aether.repository.RemoteRepository;
034import org.eclipse.aether.resolution.ArtifactRequest;
035import org.eclipse.aether.resolution.ArtifactResolutionException;
036import org.eclipse.aether.resolution.ArtifactResult;
037
038/// A maven model resolver using aether.
039///
040public class MvnModelResolver implements ModelResolver {
041
042    private final RepositorySystem repoSystem;
043    private final RepositorySystemSession session;
044    private final List<RemoteRepository> repositories;
045
046    /// Initializes a new maven model resolver.
047    ///
048    /// @param repoSystem the repo system
049    /// @param session the session
050    /// @param repositories the repositories
051    ///
052    public MvnModelResolver(RepositorySystem repoSystem,
053            RepositorySystemSession session,
054            List<RemoteRepository> repositories) {
055        this.repoSystem = repoSystem;
056        this.session = session;
057        this.repositories = repositories;
058    }
059
060    @SuppressWarnings({ "deprecation", "PMD.AvoidDuplicateLiterals" })
061    @Override
062    public ModelSource resolveModel(String groupId, String artifactId,
063            String version) throws UnresolvableModelException {
064        return resolve(groupId, artifactId, version);
065    }
066
067    @SuppressWarnings("deprecation")
068    @Override
069    public ModelSource resolveModel(Parent parent)
070            throws UnresolvableModelException {
071        // parent has groupId, artifactId, version
072        return resolve(parent.getGroupId(), parent.getArtifactId(),
073            parent.getVersion());
074    }
075
076    @SuppressWarnings("deprecation")
077    @Override
078    public ModelSource resolveModel(Dependency dependency)
079            throws UnresolvableModelException {
080        return resolve(dependency.getGroupId(), dependency.getArtifactId(),
081            dependency.getVersion());
082    }
083
084    @SuppressWarnings("deprecation")
085    private ModelSource resolve(String groupId, String artifactId,
086            String version) throws UnresolvableModelException {
087        try {
088            ArtifactRequest request = new ArtifactRequest();
089            request.setArtifact(
090                new DefaultArtifact(groupId, artifactId, "pom", version));
091            request.setRepositories(repositories);
092
093            ArtifactResult result
094                = repoSystem.resolveArtifact(session, request);
095            File pomFile = result.getArtifact().getFile();
096            return new FileModelSource(pomFile);
097        } catch (ArtifactResolutionException e) {
098            throw new UnresolvableModelException(
099                "Could not resolve POM for " + groupId + ":" + artifactId + ":"
100                    + version,
101                groupId, artifactId, version, e);
102        }
103    }
104
105    @Override
106    public void addRepository(Repository repository) {
107        // Do nothing because we configure the repositories differently
108    }
109
110    @Override
111    public void addRepository(Repository repository, boolean replace) {
112        // Do nothing because we configure the repositories differently
113    }
114
115    @Override
116    public ModelResolver newCopy() {
117        return new MvnModelResolver(repoSystem, session, repositories);
118    }
119}