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

io.sealights.onpremise.agents.plugin.ModuleDependencyHelper Maven / Gradle / Ivy

The newest version!
package io.sealights.onpremise.agents.plugin;

import io.sealights.onpremise.agents.infra.types.ModuleDependencies;
import io.sealights.plugins.engine.api.DependencyInfo;
import org.apache.maven.model.Dependency;
import org.apache.maven.project.MavenProject;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ModuleDependencyHelper {

    public ModuleDependencies getModuleDependencies(MavenProject rootProject) {
        final Set modules = collectModulesIdentifiers(rootProject);
        final ModuleDependencies moduleDependencies = new ModuleDependencies();
        for (MavenProject moduleSubproject : rootProject.getCollectedProjects()) {
            final ModuleDependencies.ModuleDependencyData moduleDependencyData = createModuleDependencyData(
                    moduleSubproject, modules);
            moduleDependencies.getModuleDependencies().add(moduleDependencyData);
        }
        final ModuleDependencies.ModuleDependencyData moduleDependencyData = createModuleDependencyData(
                rootProject, modules);
        moduleDependencies.getModuleDependencies().add(moduleDependencyData);
        return moduleDependencies;
    }

    private static Set collectModulesIdentifiers(MavenProject rootProject) {
        final Set modules = new HashSet<>();
        modules.add(buildProjectIdentifier(rootProject.getGroupId(), rootProject.getName()));
        for (MavenProject project : rootProject.getCollectedProjects()) {
            modules.add(buildProjectIdentifier(project.getGroupId(), project.getName()));
        }
        return modules;
    }

    private static String buildProjectIdentifier(String groupId, String name) {
        return groupId + ":" + name;
    }

    private ModuleDependencies.ModuleDependencyData createModuleDependencyData(
            MavenProject moduleSubproject, Set modules) {
        final List dependencyInfos = collectDependencies(moduleSubproject);
        final ModuleDependencies.ModuleDependencyData moduleDependencyData = new ModuleDependencies.ModuleDependencyData();
        moduleDependencyData.setModuleName(moduleSubproject.getName());
        for (DependencyInfo dependencyInfo : dependencyInfos) {
            final String dependencyIdentifier = buildProjectIdentifier(dependencyInfo.getGroupID(), dependencyInfo.getArtifactID());
            if (modules.contains(dependencyIdentifier)) {
                moduleDependencyData.getDependencies().add(create(dependencyInfo));
            }
        }
        return moduleDependencyData;
    }

    private List collectDependencies(MavenProject project) {
        final List dependenciesData = new ArrayList<>();
        for (Dependency d : project.getDependencies()) {
            dependenciesData.add(new DependencyInfo(d.getGroupId(), d.getArtifactId(), d.getVersion()));
        }
        return dependenciesData;
    }

    private ModuleDependencies.ModuleDependencyItem create(DependencyInfo dependencyInfo){
        final ModuleDependencies.ModuleDependencyItem item = new ModuleDependencies.ModuleDependencyItem();
        item.setArtifactId(dependencyInfo.getArtifactID());
        item.setGroupId(dependencyInfo.getGroupID());
        item.setVersion(dependencyInfo.getVersion());
        return item;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy