Example usage for org.apache.maven.project MavenProject getParent

List of usage examples for org.apache.maven.project MavenProject getParent

Introduction

In this page you can find the example usage for org.apache.maven.project MavenProject getParent.

Prototype

public MavenProject getParent() 

Source Link

Document

Returns the project corresponding to a declared parent.

Usage

From source file:org.commonjava.maven.plugins.execroot.HighestBasedirGoal.java

License:Apache License

/**
 * {@inheritDoc}//from  ww w. j  a va  2 s  . com
 * @throws MojoExecutionException 
 * @see org.commonjava.maven.plugins.execroot.AbstractDirectoryGoal#findDirectory()
 */
@Override
protected File findDirectory() throws MojoExecutionException {
    final Stack<MavenProject> toCheck = new Stack<MavenProject>();
    toCheck.addAll(projects);

    final List<File> files = new ArrayList<File>();
    while (!toCheck.isEmpty()) {
        final MavenProject p = toCheck.pop();
        if (p.getBasedir() == null) {
            // we've hit a parent that was resolved. Don't bother going higher up the hierarchy.
            continue;
        }

        if (!files.contains(p.getBasedir())) {
            // add to zero to maybe help pre-sort the paths...the shortest (parent) paths should end up near the
            // top.
            files.add(0, p.getBasedir());
        }

        if (p.getParent() != null) {
            toCheck.add(p.getParent());
        }
    }

    if (files.isEmpty()) {
        throw new MojoExecutionException("No project base directories found! Are you sure you're "
                + "executing this on a valid Maven project?");
    }

    Collections.sort(files, new PathComparator());
    final File dir = files.get(0);

    if (files.size() > 1) {
        final File next = files.get(1);
        if (!next.getAbsolutePath().startsWith(dir.getAbsolutePath())) {
            throw new MojoExecutionException("Cannot find a single highest directory for this project set. "
                    + "First two candidates directories don't share a common root.");
        }
    }

    return dir;
}

From source file:org.debian.dependency.DefaultDependencyCollection.java

License:Apache License

@Override
public List<DependencyNode> installDependencies(final List<DependencyNode> graphs,
        final DependencyNodeFilter selection, final ArtifactRepository repository, final MavenSession session)
        throws DependencyResolutionException, ArtifactInstallationException {
    Set<Artifact> installing = new HashSet<Artifact>();
    List<DependencyNode> notInstalled = collectNodes(graphs, selection, installing);

    for (Artifact artifact : installing) {
        getLogger().debug("Installing " + artifact);

        // artifact first as you could potentially use it without pom, albeit not easily
        artifact = resolveArtifact(artifact, session);
        artifactInstaller.install(artifact.getFile(), artifact, repository);

        // now the parent poms as the project one is useless without them
        MavenProject project = buildProject(artifact.getGroupId(), artifact.getArtifactId(),
                artifact.getVersion(), session);
        for (MavenProject parent = project.getParent(); parent != null; parent = parent.getParent()) {
            Artifact parentArtifact = resolveArtifact(parent.getArtifact(), session);
            artifactInstaller.install(parentArtifact.getFile(), parentArtifact, repository);
        }//from   www  .j ava  2  s.c o  m

        // finally the pom itself
        Artifact pomArtifact = repositorySystem.createProjectArtifact(artifact.getGroupId(),
                artifact.getArtifactId(), artifact.getVersion());
        pomArtifact = resolveArtifact(pomArtifact, session);
        artifactInstaller.install(pomArtifact.getFile(), pomArtifact, repository);
    }

    return notInstalled;
}

From source file:org.eclipse.che.maven.server.MavenServerImpl.java

License:Open Source License

private List<String> getActiveProfiles(MavenProject project) throws RemoteException {
    List<Profile> profiles = new ArrayList<>();

    try {/*from  www.java2  s.c o m*/
        while (project != null) {
            profiles.addAll(project.getActiveProfiles());
            project = project.getParent();
        }
    } catch (Exception e) {
        MavenServerContext.getLogger().info(e);
    }

    return profiles.stream().filter(p -> p.getId() != null).map(Profile::getId).collect(Collectors.toList());
}

From source file:org.eclipse.m2e.core.internal.M2EUtils.java

License:Open Source License

public static Collection<MavenProject> getDefiningProjects(MojoExecutionKey key,
        Collection<MavenProject> projects) {
    Set<MavenProject> sourceProjects = new HashSet<MavenProject>();
    for (MavenProject project : projects) {
        if (definesPlugin(project, key)) {
            sourceProjects.add(project);
            continue;
        }//from   ww w  . j  a v  a 2s . c om
        for (MavenProject parent = project.getParent(); parent != null; parent = parent.getParent()) {
            if (definesPlugin(parent, key)) {
                sourceProjects.add(parent);
                // Only the first instance is necessary
                break;
            }
        }
    }
    return sourceProjects;
}

From source file:org.eclipse.m2e.wtp.internal.filtering.ResourceFilteringBuildParticipant.java

License:Open Source License

/**
 * Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=356725. 
 * Loads the parent project hierarchy if needed.
 * @param facade//from ww  w  . j av  a 2 s  . co m
 * @param monitor
 * @return true if parent projects had to be loaded.
 * @throws CoreException
 */
private boolean loadParentHierarchy(IMavenProjectFacade facade, IProgressMonitor monitor) throws CoreException {
    boolean loadedParent = false;
    MavenProject mavenProject = facade.getMavenProject();
    try {
        if (mavenProject.getModel().getParent() == null || mavenProject.getParent() != null) {
            //If the method is called without error, we can assume the project has been fully loaded
            //No need to continue. 
            return false;
        }
    } catch (IllegalStateException e) {
        //The parent can not be loaded properly 
    }
    MavenExecutionRequest request = null;
    while (mavenProject != null && mavenProject.getModel().getParent() != null) {
        if (monitor.isCanceled()) {
            break;
        }
        if (request == null) {
            request = MavenPlugin.getMavenProjectRegistry().createExecutionRequest(facade, monitor);
        }
        MavenProject parentProject = MavenPlugin.getMaven().resolveParentProject(request, mavenProject,
                monitor);
        if (parentProject != null) {
            mavenProject.setParent(parentProject);
            loadedParent = true;
        }
        mavenProject = parentProject;
    }
    return loadedParent;
}

From source file:org.eclipse.scada.build.helper.DefaultPomHelper.java

License:Open Source License

@Override
public Set<MavenProject> expandProjects(final Collection<MavenProject> projects, final Log log,
        final MavenSession session) {
    try {//from   w  w  w  . j a  v a  2  s .c o  m
        final Set<MavenProject> result = new HashSet<MavenProject>();

        final Queue<MavenProject> queue = new LinkedList<MavenProject>(projects);
        while (!queue.isEmpty()) {
            final MavenProject project = queue.poll();
            log.debug("Checking project: " + project);
            if (project.getFile() == null) {
                log.info("Skipping non-local project: " + project);
                continue;
            }
            if (!result.add(project)) {
                // if the project was already in our result, there is no need to process twice
                continue;
            }
            // add all children to the queue
            queue.addAll(loadChildren(project, log, session));
            if (hasLocalParent(project)) {
                // if we have a parent, add the parent as well
                queue.add(project.getParent());
            }
        }
        return result;
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.geotools.maven.JarCollector.java

License:Open Source License

/**
 * Copies the {@code .jar} files to the collect directory.
 *
 * @throws MojoExecutionException if the plugin execution failed.
 *//*from w  w  w  .ja va  2 s .c o  m*/
public void execute() throws MojoExecutionException {
    /*
     * Gets the parent "target" directory.
     */
    MavenProject parent = project;
    while (parent.hasParent()) {
        parent = parent.getParent();
    }
    collectDirectory = parent.getBuild().getDirectory();
    /*
     * Now collects the JARs.
     */
    try {
        collect();
    } catch (IOException e) {
        throw new MojoExecutionException("Error collecting the JAR file.", e);
    }
}

From source file:org.glassfish.jersey.tools.plugins.GenerateJerseyModuleListMojo.java

License:Open Source License

/**
 * Build the project-info link path by including all the artifactId up to (excluding) the root parent
 * @param project project for which the path should be determined.
 * @return path consisting of hierarchically nested maven artifact IDs. Used for referencing to the project-info on java.net
 *//* w ww . j av  a2s.co m*/
private String getLinkPath(MavenProject project) {
    String path = "";
    MavenProject parent = project.getParent();
    while (parent != null && !(parent.getArtifactId().equals("project")
            && parent.getGroupId().equals("org.glassfish.jersey"))) {
        path = parent.getArtifactId() + "/" + path;
        parent = parent.getParent();
    }
    return path;
}

From source file:org.heneveld.maven.license_audit.util.MavenUtil.java

/** If the URL is not declared on the project's original model, 
 * take the parent's declared URL; NOT the inference done by maven
 * (which appends the child artifactId, often wrongly.
 *//*from  w  w  w  .  j  a va  2s.co m*/
public static String getDeclaredUrl(MavenProject p) {
    String result = p.getUrl();
    if (result == null)
        return null;
    if (p.getOriginalModel() == null || p.getOriginalModel().getUrl() != null)
        return result;
    // if url is inferred, take the parent's url instead
    MavenProject pp = p.getParent();
    if (pp == null)
        return result;
    return getDeclaredUrl(pp);
}

From source file:org.hoydaa.maven.plugins.ParentCheckerMojo.java

License:Apache License

public void execute() throws MojoExecutionException {
    MavenProject tProject = project;
    while (tProject != null) {
        Artifact parentArtifact = tProject.getParentArtifact();
        if (null == parentArtifact || checkArtifacts == null || !hasValidParent(tProject)) {
            getLog().info("This parent '" + parentArtifact + "' is not in the list of artifacts '"
                    + checkArtifacts + "' to be checked, skipping...");
        } else {/*  ww  w.ja v  a  2 s.  c o m*/
            try {
                // get newer versions of the parent, if there is one.
                ArtifactVersion currentVersion = tProject.getParentArtifact().getSelectedVersion();
                List<ArtifactVersion> availableVersions = artifactMetadataSource.retrieveAvailableVersions(
                        artifactFactory.createParentArtifact(parentArtifact.getGroupId(),
                                parentArtifact.getArtifactId(), parentArtifact.getVersion()),
                        localRepository, remoteArtifactRepositories);
                List<ArtifactVersion> newVersions = getNewerVersions(currentVersion, availableVersions);

                // if there is newer versions available
                if (newVersions.size() > 0) {
                    boolean forcedUpdateExists = false;

                    getLog().warn("New versions available for your parent POM " + parentArtifact.toString()
                            + " of project '" + tProject.getArtifact().toString() + "'!");
                    for (ArtifactVersion version : newVersions) {
                        boolean forced = isForced(version, tProject);
                        forcedUpdateExists = forcedUpdateExists || forced;
                        getLog().warn(version.toString() + " (" + (forced ? "FORCED" : "not forced") + ")");
                    }

                    if (forceUpgrade) {
                        throw new MojoExecutionException(getWarningText(newVersions, tProject)
                                + " You have to upgrade your parent POM to the latest version!");
                    } else if (forcedUpdateExists) {
                        throw new MojoExecutionException(getWarningText(newVersions, tProject)
                                + " You have to upgrade your parent POM to the latest forced update at least!");
                    } else {
                        getLog().warn(getWarningText(newVersions, tProject));
                    }
                } else {
                    getLog().info("Your parent POM's are all up-to-date, good to do dude.");
                }
            } catch (ArtifactMetadataRetrievalException e) {
                e.printStackTrace();
            } catch (OverConstrainedVersionException e) {
                e.printStackTrace();
            } catch (ProjectBuildingException e) {
                e.printStackTrace();
            }
        }

        Artifact temp = tProject.getParentArtifact();
        tProject = tProject.getParent();
        if (null != tProject)
            tProject.setArtifact(temp);
    }
}