Example usage for org.apache.maven.execution MavenSession getProjects

List of usage examples for org.apache.maven.execution MavenSession getProjects

Introduction

In this page you can find the example usage for org.apache.maven.execution MavenSession getProjects.

Prototype

public List<MavenProject> getProjects() 

Source Link

Usage

From source file:co.leantechniques.maven.BuildInformation.java

License:Apache License

private void initializeProjects(MavenSession session) {
    for (MavenProject mavenProject : session.getProjects()) {
        projects.add(new Project(mavenProject.getGroupId(), mavenProject.getArtifactId(),
                mavenProject.getVersion()));
    }/*from w  w  w  .  j a  v a  2s  . com*/
}

From source file:com.exentes.maven.versions.VersionMojoUtils.java

License:Apache License

public static SetMultimap<Artifact, MavenProject> allSnapshotProjectArtifacts(MavenSession session) {
    SetMultimap<Artifact, MavenProject> allSnapshotProjectArtifacts = HashMultimap.create();

    for (MavenProject project : session.getProjects()) {
        for (Artifact artifact : difference(newHashSet(filterSnapshots(project.getDependencyArtifacts())),
                newHashSet(transform(session.getProjects(), toProjectArtifactFunction)))) {
            allSnapshotProjectArtifacts.put(artifact, project);
        }/* w  w  w.  j  a v a2  s  .com*/
    }
    return allSnapshotProjectArtifacts;
}

From source file:com.exentes.maven.versions.VersionMojoUtils.java

License:Apache License

private static Map<String, Dependency> allDependencyManagementMap(MavenSession session) {
    Map<String, Dependency> allDependencyManagementMap = Maps.newHashMap();
    for (MavenProject project : session.getProjects()) {
        for (Dependency dependency : concat(dependencyManagement(project),
                allActiveProfilesDependencyManagement(project))) {
            allDependencyManagementMap.put(projectDependencyKey(project, dependency), dependency);
        }//from  w  w  w . j a  v  a 2  s . c om
    }
    return allDependencyManagementMap;
}

From source file:com.exentes.maven.versions.VersionMojoUtils.java

License:Apache License

public static Map<String, Dependency> allDependenciesMap(MavenSession session) {
    Map<String, Dependency> allDependencyManagementMap = allDependencyManagementMap(session);
    Map<String, Dependency> allDependenciesMap = Maps.newHashMap();
    for (MavenProject project : session.getProjects()) {
        for (Dependency dependency : concat(emptyIfNull(project.getOriginalModel().getDependencies()),
                allActiveProfilesDependencies(project))) {
            if (dependency.getVersion() != null) {
                allDependenciesMap.put(projectDependencyKey(project, dependency), dependency);
            } else {
                // dependency version is not specified. Perhaps it is specified in a dependencyManagement section
                // of the project of one of its parents
                Dependency parentDependency = null;
                MavenProject projectToCheck = project;
                while (parentDependency == null && projectToCheck != null) {
                    parentDependency = allDependencyManagementMap
                            .get(projectDependencyKey(projectToCheck, dependency));
                    if (parentDependency != null) {
                        allDependenciesMap.put(projectDependencyKey(project, dependency), parentDependency);
                    } else {
                        if (!projectToCheck.isExecutionRoot()) {
                            projectToCheck = projectToCheck.getParent();
                        } else {
                            projectToCheck = null;
                        }// w  w w . ja  va2 s  .co m
                    }
                }
            }
        }
    }
    return allDependenciesMap;
}

From source file:com.exentes.maven.versions.VersionMojoUtils.java

License:Apache License

public static Map<Object, MavenProject> origins(MavenSession session) {
    Map<Object, MavenProject> origins = new IdentityHashMap<Object, MavenProject>();

    for (MavenProject project : session.getProjects()) {
        if (project.getOriginalModel().getProperties() != null) {
            origins.put(project.getOriginalModel().getProperties(), project);
        }//from   w  w  w.j  a  v a 2  s . c o m
        for (Profile profile : project.getActiveProfiles()) {
            if (profile.getProperties() != null) {
                origins.put(profile.getProperties(), project);
            }
        }
        for (Dependency dependency : concat(emptyIfNull(project.getOriginalModel().getDependencies()),
                dependencyManagement(project), allActiveProfilesDependencies(project),
                allActiveProfilesDependencyManagement(project))) {
            origins.put(dependency, project);
        }
    }
    return origins;
}

From source file:com.github.htfv.maven.plugins.buildconfigurator.extension.BuildConfiguratorExtension.java

License:Apache License

/**
 * Configures loaded projects.//from w w w . j  av a2s  .  co m
 *
 * @param session
 *            session containing loaded projects.
 *
 * @throws MavenExecutionException
 *             if projects could not be counfigured.
 */
@Override
public void afterProjectsRead(final MavenSession session) throws MavenExecutionException {
    try {
        for (final MavenProject project : session.getProjects()) {
            //
            // Check to see if Build Configurator is configured for this project.
            //

            final Plugin plugin = getPlugin(project);

            if (plugin == null) {
                continue;
            }

            //
            // Configure the project.
            //

            final ConfigureRequest request = new ConfigureRequest.Builder()
                    .extensionConfiguration(getModel(plugin)).project(project).build();

            buildConfigurator.configure(request);
        }
    } catch (final Exception e) {
        throw new MavenExecutionException(e.getMessage(), e);
    }
}

From source file:com.github.shyiko.sme.ServersExtension.java

License:Apache License

@Override
public void afterProjectsRead(MavenSession session) throws MavenExecutionException {
    MojoExecution mojoExecution = new MojoExecution(new MojoDescriptor());
    ExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator(session, mojoExecution);
    Properties userProperties = session.getUserProperties();

    boolean exportAsSysProp = isExtensionProperty(session, "servers.exportAsSysProp");

    Map<String, String> properties = new HashMap<String, String>();
    try {/*from   w  w w  .jav a 2s.c om*/
        for (Server server : session.getSettings().getServers()) {
            String serverId = server.getId();
            for (String field : FIELDS) {
                String[] aliases = getAliases(serverId, field);
                String fieldNameWithFirstLetterCapitalized = upperCaseFirstLetter(field);
                String fieldValue = (String) Server.class.getMethod("get" + fieldNameWithFirstLetterCapitalized)
                        .invoke(server);
                if (fieldValue != null) {
                    fieldValue = decryptInlinePasswords(fieldValue);
                }
                for (String alias : aliases) {
                    String userPropertyValue = userProperties.getProperty(alias);
                    if (userPropertyValue != null) {
                        fieldValue = userPropertyValue;
                        break;
                    }
                }
                String resolvedValue = (String) expressionEvaluator.evaluate(fieldValue);
                Server.class
                        .getMethod("set" + fieldNameWithFirstLetterCapitalized, new Class[] { String.class })
                        .invoke(server, resolvedValue);
                if (resolvedValue != null) {
                    for (String alias : aliases) {
                        properties.put(alias, resolvedValue);
                    }
                }
            }
        }

        if (exportAsSysProp) {
            System.getProperties().putAll(properties);
        } else {
            for (MavenProject project : session.getProjects()) {
                Properties projectProperties = project.getProperties();
                projectProperties.putAll(properties);
            }
        }
    } catch (Exception e) {
        throw new MavenExecutionException("Failed to expose settings.servers.*", e);
    }
}

From source file:com.jayway.maven.plugins.android.phase_prebuild.AarMavenLifecycleParticipant.java

License:Open Source License

@Override
public void afterProjectsRead(MavenSession session) throws MavenExecutionException {
    log.debug("");
    log.debug("AMLP afterProjectsRead");
    log.debug("");

    log.debug("CurrentProject=" + session.getCurrentProject());
    final List<MavenProject> projects = session.getProjects();

    for (MavenProject project : projects) {
        log.debug("");
        log.debug("project=" + project.getArtifact());

        final BuildHelper helper = new BuildHelper(repoSystem, session.getRepositorySession(), project, log);

        final Collection<Artifact> artifacts = getProjectsArtifacts(session, project);
        log.debug("projects deps: : " + artifacts);
        for (Artifact artifact : artifacts) {
            final String type = artifact.getType();
            if (type.equals(AndroidExtension.AAR)) {
                // An AAR lib contains a classes jar that needs to be added to the classpath.
                // Create a placeholder classes.jar and add it to the compile classpath.
                // It will replaced with the real classes.jar by GenerateSourcesMojo.
                addClassesToClasspath(helper, project, artifact);
            } else if (type.equals(AndroidExtension.APK)) {
                // The only time that an APK will likely be a dependency is when this an an APK test project.
                // So add a placeholder (we cannot resolve the actual dep pre build) to the compile classpath.
                // The placeholder will be replaced with the real APK jar later.
                addClassesToClasspath(helper, project, artifact);
            }/*w  w  w .  j  a va  2  s  . c  o m*/
        }
    }
}

From source file:com.jayway.maven.plugins.android.phase_prebuild.ClasspathModifierLifecycleParticipant.java

License:Open Source License

@Override
public void afterProjectsRead(MavenSession session) throws MavenExecutionException {
    log.debug("");
    log.debug("ClasspathModifierLifecycleParticipant#afterProjectsRead - start");
    log.debug("");

    log.debug("CurrentProject=" + session.getCurrentProject());
    final List<MavenProject> projects = session.getProjects();
    final DependencyResolver dependencyResolver = new DependencyResolver(log, dependencyGraphBuilder);
    final ArtifactResolverHelper artifactResolverHelper = new ArtifactResolverHelper(artifactResolver, log);

    for (MavenProject project : projects) {
        log.debug("");
        log.debug("project=" + project.getArtifact());

        if (!AndroidExtension.isAndroidPackaging(project.getPackaging())) {
            continue; // do not modify classpath if not an android project.
        }//from   ww w.  jav  a2s  .c o m

        final UnpackedLibHelper helper = new UnpackedLibHelper(artifactResolverHelper, project, log);

        final Set<Artifact> artifacts;

        // If there is an extension ClassRealm loaded for this project then use that
        // as the ContextClassLoader so that Wagon extensions can be used to resolves dependencies.
        final ClassLoader projectClassLoader = (project.getClassRealm() != null) ? project.getClassRealm()
                : Thread.currentThread().getContextClassLoader();

        final ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(projectClassLoader);
            artifacts = dependencyResolver.getProjectDependenciesFor(project, session);
        } catch (DependencyGraphBuilderException e) {
            // Nothing to do. The resolution failure will be displayed by the standard resolution mechanism.
            continue;
        } finally {
            Thread.currentThread().setContextClassLoader(originalClassLoader);
        }

        log.debug("projects deps: : " + artifacts);
        for (Artifact artifact : artifacts) {
            final String type = artifact.getType();
            if (type.equals(AndroidExtension.AAR)) {
                // An AAR lib contains a classes jar that needs to be added to the classpath.
                // Create a placeholder classes.jar and add it to the compile classpath.
                // It will replaced with the real classes.jar by GenerateSourcesMojo.
                addClassesToClasspath(helper, project, artifact);
                // Add jar files in 'libs' into classpath.
                addLibsJarsToClassPath(helper, project, artifact);
            } else if (type.equals(AndroidExtension.APK)) {
                // The only time that an APK will likely be a dependency is when this an an APK test project.
                // So add a placeholder (we cannot resolve the actual dep pre build) to the compile classpath.
                // The placeholder will be replaced with the real APK jar later.
                addClassesToClasspath(helper, project, artifact);
            } else if (type.equals(AndroidExtension.APKLIB)) {
                // Add jar files in 'libs' into classpath.
                addLibsJarsToClassPath(helper, project, artifact);
            }
        }
    }
    log.debug("");
    log.debug("ClasspathModifierLifecycleParticipant#afterProjectsRead - finish");
}

From source file:com.simpligility.maven.plugins.android.phase_prebuild.ClasspathModifierLifecycleParticipant.java

License:Open Source License

@Override
public void afterProjectsRead(MavenSession session) throws MavenExecutionException {
    log.debug("");
    log.debug("ClasspathModifierLifecycleParticipant#afterProjectsRead - start");
    log.debug("");

    log.debug("CurrentProject=" + session.getCurrentProject());
    final List<MavenProject> projects = session.getProjects();
    final DependencyResolver dependencyResolver = new DependencyResolver(log, dependencyGraphBuilder);
    final ArtifactResolverHelper artifactResolverHelper = new ArtifactResolverHelper(artifactResolver, log);

    for (MavenProject project : projects) {
        log.debug("");
        log.debug("project=" + project.getArtifact());

        if (!AndroidExtension.isAndroidPackaging(project.getPackaging())) {
            continue; // do not modify classpath if not an android project.
        }//from ww  w. j a  v  a  2 s . co m

        final String unpackedLibsFolder = getMojoConfigurationParameter(project, UNPACKED_LIBS_FOLDER_PARAM,
                null);
        final UnpackedLibHelper helper = new UnpackedLibHelper(artifactResolverHelper, project, log,
                unpackedLibsFolder == null ? null : new File(unpackedLibsFolder));

        final Set<Artifact> artifacts;

        // If there is an extension ClassRealm loaded for this project then use that
        // as the ContextClassLoader so that Wagon extensions can be used to resolves dependencies.
        final ClassLoader projectClassLoader = (project.getClassRealm() != null) ? project.getClassRealm()
                : Thread.currentThread().getContextClassLoader();

        final ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(projectClassLoader);
            artifacts = dependencyResolver.getProjectDependenciesFor(project, session);
        } catch (DependencyGraphBuilderException e) {
            // Nothing to do. The resolution failure will be displayed by the standard resolution mechanism.
            continue;
        } finally {
            Thread.currentThread().setContextClassLoader(originalClassLoader);
        }

        boolean includeFromAar = getMojoConfigurationParameter(project, INCLUDE_FROM_AAR_PARAM,
                INCLUDE_FROM_AAR_DEFAULT);
        boolean includeFromApklib = getMojoConfigurationParameter(project, INCLUDE_FROM_APKLIB_PARAM,
                INCLUDE_FROM_APKLIB_DEFAULT);
        boolean disableConflictingDependenciesWarning = getMojoConfigurationParameter(project,
                DISABLE_CONFLICTING_DEPENDENCIES_WARNING_PARAM,
                DISABLE_CONFLICTING_DEPENDENCIES_WARNING_DEFAULT);

        log.debug("projects deps: : " + artifacts);

        if (!disableConflictingDependenciesWarning) {
            ProvidedDependencyChecker checker = new ProvidedDependencyChecker();
            checker.checkProvidedDependencies(artifacts, log);
        }

        for (Artifact artifact : artifacts) {
            final String type = artifact.getType();
            if (type.equals(AndroidExtension.AAR)) {
                // An AAR lib contains a classes jar that needs to be added to the classpath.
                // Create a placeholder classes.jar and add it to the compile classpath.
                // It will replaced with the real classes.jar by GenerateSourcesMojo.
                addClassesToClasspath(helper, project, artifact);

                // An AAR may also contain zero or more internal libs in the libs folder.
                // If 'includeLibsJarsFromAar' config param is true then include them too.
                if (includeFromAar) {
                    // Add jar files in 'libs' into classpath.
                    addLibsJarsToClassPath(helper, project, artifact);
                }
            } else if (type.equals(AndroidExtension.APK)) {
                // The only time that an APK will likely be a dependency is when this an an APK test project.
                // So add a placeholder (we cannot resolve the actual dep pre build) to the compile classpath.
                // The placeholder will be replaced with the real APK jar later.
                addClassesToClasspath(helper, project, artifact);
            } else if (type.equals(AndroidExtension.APKLIB)) {
                if (includeFromApklib) {
                    // Add jar files in 'libs' into classpath.
                    addLibsJarsToClassPath(helper, project, artifact);
                }
            }
        }
    }

    if (addedJarFromLibs) {
        log.warn("Transitive dependencies should really be provided by Maven dependency management.\n"
                + "          We suggest you to ask the above providers to package their component properly.\n"
                + "          Things may break at compile and/or runtime due to multiple copies of incompatible libraries.");
    }
    log.debug("");
    log.debug("ClasspathModifierLifecycleParticipant#afterProjectsRead - finish");
}