Search in sources :

Example 1 with MavenProject

use of org.apache.maven.project.MavenProject in project jersey by jersey.

the class GenerateJerseyModuleListMojo method categorizeModules.

private Map<String, List<MavenProject>> categorizeModules(List<MavenProject> projects) {
    Map<String, List<MavenProject>> categorizedProjects = new HashMap<>();
    for (MavenProject project : projects) {
        String groupId = project.getGroupId();
        if (categorizedProjects.containsKey(groupId)) {
            categorizedProjects.get(groupId).add(project);
        } else {
            List<MavenProject> actualList = new LinkedList<>();
            actualList.add(project);
            categorizedProjects.put(groupId, actualList);
        }
    }
    return categorizedProjects;
}
Also used : MavenProject(org.apache.maven.project.MavenProject) HashMap(java.util.HashMap) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList)

Example 2 with MavenProject

use of org.apache.maven.project.MavenProject in project intellij-community by JetBrains.

the class MavenEmbedder method readProject.

@NotNull
private MavenExecutionResult readProject(@NotNull final MavenExecutionRequest request) {
    ProfileManager globalProfileManager = request.getGlobalProfileManager();
    globalProfileManager.loadSettingsProfiles(request.getSettings());
    MavenProject rootProject = null;
    final List<Exception> exceptions = new ArrayList<Exception>();
    Object result = null;
    try {
        final File pomFile = new File(request.getPomFile());
        if (!pomFile.exists()) {
            throw new FileNotFoundException("File doesn't exist: " + pomFile.getPath());
        }
        final Method getProjectsMethod = DefaultMaven.class.getDeclaredMethod("getProjects", MavenExecutionRequest.class);
        getProjectsMethod.setAccessible(true);
        Maven maven = getComponent(Maven.class);
        result = getProjectsMethod.invoke(maven, request);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        return handleException(e.getTargetException());
    } catch (Exception e) {
        return handleException(e);
    }
    if (result != null) {
        MavenProjectBuilder builder = getComponent(MavenProjectBuilder.class);
        for (Object p : (List) result) {
            MavenProject project = (MavenProject) p;
            try {
                builder.calculateConcreteState(project, request.getProjectBuilderConfiguration(), false);
            } catch (ModelInterpolationException e) {
                exceptions.add(e);
            }
            if (project.isExecutionRoot()) {
                rootProject = project;
            }
        }
        if (rootProject == null && exceptions.isEmpty()) {
            throw new RuntimeException("Could't build project for unknown reason");
        }
    }
    return new MavenExecutionResult(rootProject, exceptions);
}
Also used : DefaultProfileManager(org.apache.maven.profiles.DefaultProfileManager) ProfileManager(org.apache.maven.profiles.ProfileManager) DefaultMaven(org.apache.maven.DefaultMaven) Maven(org.apache.maven.Maven) FileNotFoundException(java.io.FileNotFoundException) Method(java.lang.reflect.Method) PlexusContainerException(org.codehaus.plexus.PlexusContainerException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) FileNotFoundException(java.io.FileNotFoundException) ModelInterpolationException(org.apache.maven.project.interpolation.ModelInterpolationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) SecDispatcherException(org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MavenProjectBuilder(org.apache.maven.project.MavenProjectBuilder) ModelInterpolationException(org.apache.maven.project.interpolation.ModelInterpolationException) MavenProject(org.apache.maven.project.MavenProject) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with MavenProject

use of org.apache.maven.project.MavenProject in project asterixdb by apache.

the class LicenseMojo method addDependencyToLicenseMap.

private void addDependencyToLicenseMap(MavenProject depProject, List<Pair<String, String>> depLicenses, String depLocation) {
    final String depGav = toGav(depProject);
    getLog().debug("adding " + depGav + ", location: " + depLocation);
    final MutableBoolean usedMetric = new MutableBoolean(false);
    if (depLicenses.size() > 1) {
        Collections.sort(depLicenses, (o1, o2) -> {
            final int metric1 = getLicenseMetric(o1.getLeft());
            final int metric2 = getLicenseMetric(o2.getLeft());
            usedMetric.setValue(usedMetric.booleanValue() || metric1 != LicenseSpec.UNDEFINED_LICENSE_METRIC || metric2 != LicenseSpec.UNDEFINED_LICENSE_METRIC);
            return Integer.compare(metric1, metric2);
        });
        if (usedMetric.booleanValue()) {
            getLog().info("Multiple licenses for " + depGav + ": " + depLicenses + "; taking lowest metric: " + depLicenses.get(0));
        } else {
            getLog().warn("Multiple licenses for " + depGav + ": " + depLicenses + "; taking first listed: " + depLicenses.get(0));
        }
    } else if (depLicenses.isEmpty()) {
        getLog().info("no license defined in model for " + depGav);
        depLicenses.add(new ImmutablePair<>("MISSING_LICENSE", null));
    }
    Pair<String, String> key = depLicenses.get(0);
    String licenseUrl = key.getLeft();
    final String displayName = key.getRight();
    if (!urlToLicenseMap.containsKey(licenseUrl)) {
        // assuming we've not already mapped it, annotate the URL with artifact info, if not an actual URL
        try {
            getLog().debug("- URL: " + new URL(licenseUrl));
        // life is good
        } catch (MalformedURLException e) {
            // we encounter this a lot.  Log a warning, and use an annotated key
            final String fakeLicenseUrl = depGav.replaceAll(":", "--") + "_" + licenseUrl;
            getLog().info("- URL for " + depGav + " is malformed: " + licenseUrl + "; using: " + fakeLicenseUrl);
            licenseUrl = fakeLicenseUrl;
        }
    }
    addProject(new Project(depProject, depLocation, depProject.getArtifact().getFile()), new LicenseSpec(licenseUrl, displayName), true);
}
Also used : Project(org.apache.hyracks.maven.license.project.Project) MavenProject(org.apache.maven.project.MavenProject) MalformedURLException(java.net.MalformedURLException) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) URL(java.net.URL)

Example 4 with MavenProject

use of org.apache.maven.project.MavenProject in project asterixdb by apache.

the class LicenseMojo method resolveDependency.

protected MavenProject resolveDependency(Artifact depObj) throws ProjectBuildingException {
    String key = depObj.getGroupId() + ":" + depObj.getArtifactId() + ":" + depObj.getVersion();
    MavenProject depProj = projectCache.get(key);
    if (depProj == null) {
        try {
            depProj = projectBuilder.buildFromRepository(depObj, remoteRepositories, localRepository, false);
        } catch (ProjectBuildingException e) {
            throw new ProjectBuildingException(key, "Error creating dependent artifacts", e);
        }
        Model supplement = supplementModels.get(SupplementalModelHelper.generateSupplementMapKey(depObj.getGroupId(), depObj.getArtifactId()));
        if (supplement != null) {
            Model merged = SupplementalModelHelper.mergeModels(assembler, depProj.getModel(), supplement);
            Set<String> origLicenses = depProj.getModel().getLicenses().stream().map(License::getUrl).collect(Collectors.toSet());
            Set<String> newLicenses = merged.getLicenses().stream().map(License::getUrl).collect(Collectors.toSet());
            if (!origLicenses.equals(newLicenses)) {
                getLog().warn("license list for " + toGav(depProj) + " changed with supplemental model; was: " + origLicenses + ", now: " + newLicenses);
            }
            depProj = new MavenProject(merged);
            depProj.setArtifact(depObj);
            depProj.setVersion(depObj.getVersion());
        }
        depProj.getArtifact().setScope(depObj.getScope());
        projectCache.put(key, depProj);
    }
    return depProj;
}
Also used : ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) MavenProject(org.apache.maven.project.MavenProject) Model(org.apache.maven.model.Model)

Example 5 with MavenProject

use of org.apache.maven.project.MavenProject in project asterixdb by apache.

the class LicenseMojo method gatherProjectDependencies.

private void gatherProjectDependencies(MavenProject project, Map<MavenProject, List<Pair<String, String>>> dependencyLicenseMap, Map<String, MavenProject> dependencyGavMap) throws ProjectBuildingException {
    final Set dependencyArtifacts = project.getArtifacts();
    if (dependencyArtifacts != null) {
        for (Object depArtifactObj : dependencyArtifacts) {
            final Artifact depArtifact = (Artifact) depArtifactObj;
            if (!excludedScopes.contains(depArtifact.getScope())) {
                MavenProject dep = resolveDependency(depArtifact);
                dep.setArtifact(depArtifact);
                dependencyGavMap.put(toGav(dep), dep);
                List<Pair<String, String>> licenseUrls = new ArrayList<>();
                for (Object license : dep.getLicenses()) {
                    final License license1 = (License) license;
                    String url = license1.getUrl() != null ? license1.getUrl() : (license1.getName() != null ? license1.getName() : "LICENSE_EMPTY_NAME_URL");
                    licenseUrls.add(new ImmutablePair<>(url, license1.getName()));
                }
                dependencyLicenseMap.put(dep, licenseUrls);
            }
        }
    }
}
Also used : TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) MavenProject(org.apache.maven.project.MavenProject) ArrayList(java.util.ArrayList) License(org.apache.maven.model.License) Artifact(org.apache.maven.artifact.Artifact) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair)

Aggregations

MavenProject (org.apache.maven.project.MavenProject)679 File (java.io.File)308 ArrayList (java.util.ArrayList)132 Artifact (org.apache.maven.artifact.Artifact)130 Test (org.junit.Test)84 Model (org.apache.maven.model.Model)77 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)63 IOException (java.io.IOException)52 MavenSession (org.apache.maven.execution.MavenSession)50 HashMap (java.util.HashMap)43 MojoFailureException (org.apache.maven.plugin.MojoFailureException)39 ConsoleLogger (org.codehaus.plexus.logging.console.ConsoleLogger)36 LinkedHashSet (java.util.LinkedHashSet)31 Properties (java.util.Properties)30 HashSet (java.util.HashSet)29 Assembly (org.apache.maven.plugins.assembly.model.Assembly)28 EasyMockSupport (org.easymock.classextension.EasyMockSupport)27 Plugin (org.apache.maven.model.Plugin)26 Dependency (org.apache.maven.model.Dependency)25 ProjectBuildingRequest (org.apache.maven.project.ProjectBuildingRequest)24