All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.teavm.maven.MavenSourceFileProviderLookup Maven / Gradle / Ivy

There is a newer version: 0.10.2
Show newest version
/*
 *  Copyright 2013 Alexey Andreev.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.teavm.maven;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.teavm.tooling.DirectorySourceFileProvider;
import org.teavm.tooling.JarSourceFileProvider;
import org.teavm.tooling.SourceFileProvider;

/**
 *
 * @author Alexey Andreev
 */
public class MavenSourceFileProviderLookup {
    private MavenProject mavenProject;
    private RepositorySystem repositorySystem;
    private ArtifactRepository localRepository;
    private List remoteRepositories;
    private List pluginDependencies;

    public void setMavenProject(MavenProject mavenProject) {
        this.mavenProject = mavenProject;
    }

    public void setRepositorySystem(RepositorySystem repositorySystem) {
        this.repositorySystem = repositorySystem;
    }

    public void setLocalRepository(ArtifactRepository localRepository) {
        this.localRepository = localRepository;
    }

    public void setRemoteRepositories(List remoteRepositories) {
        this.remoteRepositories = remoteRepositories;
    }

    public void setPluginDependencies(List pluginDependencies) {
        this.pluginDependencies = pluginDependencies;
    }

    public List resolve() {
        List initialArtifacts = new ArrayList<>();
        initialArtifacts.addAll(mavenProject.getArtifacts());
        if (pluginDependencies != null) {
            initialArtifacts.addAll(pluginDependencies);
        }
        Set artifacts = new HashSet<>();
        for (Artifact artifact : initialArtifacts) {
            if (artifact.getClassifier() != null && artifact.getClassifier().equals("sources")) {
                artifacts.add(artifact);
            } else {
                artifacts.add(repositorySystem.createArtifactWithClassifier(artifact.getGroupId(),
                        artifact.getArtifactId(), artifact.getVersion(), artifact.getType(), "sources"));
            }
        }
        List providers = new ArrayList<>();
        for (Artifact artifact : artifacts) {
            ArtifactResolutionRequest request = new ArtifactResolutionRequest()
                    .setLocalRepository(localRepository)
                    .setRemoteRepositories(new ArrayList<>(remoteRepositories))
                    .setArtifact(artifact);
            ArtifactResolutionResult result = repositorySystem.resolve(request);
            for (Artifact resolvedArtifact : result.getArtifacts()) {
                if (resolvedArtifact.getFile() != null) {
                    providers.add(new JarSourceFileProvider(resolvedArtifact.getFile()));
                }
            }
        }
        for (String sourceRoot : mavenProject.getCompileSourceRoots()) {
            providers.add(new DirectorySourceFileProvider(new File(sourceRoot)));
        }
        return providers;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy