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.buildforce.build.maven.plugins.RemoveDependenciesFromLocalRepositoryMojo.java

License:Apache License

private void removeAncestors() throws MojoExecutionException {
    MavenProject parent = project.getParent();
    MavenProject last = project;//from   ww  w .ja v  a  2 s .  c  o m

    while (parent != null) {
        Artifact parentArtifact = last.getParentArtifact();

        File localArtifactFile = new File(localRepository.getBasedir(), localRepository.pathOf(parentArtifact));
        File localMetadataFile = new File(localArtifactFile.getParentFile(), MAVEN_METADATA_LOCAL);

        if (localMetadataFile.exists()) {
            boolean deletedSuccessfully = localMetadataFile.delete();

            if (!deletedSuccessfully)
                throw new MojoExecutionException(
                        "Failed to remove " + localMetadataFile.getAbsolutePath() + "!");
            else
                getLog().info("Removed maven-metadata-local.xml for parent " + parentArtifact.getGroupId() + ":"
                        + parentArtifact.getArtifactId() + ":" + parentArtifact.getVersion() + ": "
                        + localMetadataFile.getAbsolutePath());
        }

        parent = parent.getParent();
    }
}

From source file:org.codehaus.mojo.cobertura.CoberturaReportMojo.java

License:Apache License

/**
 * Generates various information needed for building aggregate reports.
 *///from w  ww.j a v  a  2  s.co m
private void buildAggregateInfo() {
    if (projectChildren != null) {
        // already did this work
        return;
    }

    // build parent-child map
    projectChildren = new HashMap<MavenProject, List<MavenProject>>();
    for (MavenProject proj : reactorProjects) {
        List<MavenProject> depList = projectChildren.get(proj.getParent());
        if (depList == null) {
            depList = new ArrayList<MavenProject>();
            projectChildren.put(proj.getParent(), depList);
        }
        depList.add(proj);
    }

    // attempt to determine where data files and output dir are
    relDataFileName = relativize(project.getBasedir(), getDataFile());
    if (relDataFileName == null) {
        getLog().warn("Could not determine relative data file name, defaulting to 'cobertura/cobertura.ser'");
        relDataFileName = "cobertura/cobertura.ser";
    }
    relAggregateOutputDir = relativize(project.getBasedir(), outputDirectory);
    if (relAggregateOutputDir == null) {
        getLog().warn("Could not determine relative output dir name, defaulting to 'cobertura'");
        relAggregateOutputDir = "cobertura";
    }
}

From source file:org.codehaus.mojo.jsimport.LocalRepositoryCollector.java

License:Apache License

/**
 * Constructor./* w  ww  .ja v  a2  s  .  co  m*/
 * 
 * @param project The Maven project to be processed.
 * @param localRepository The local repository being used by Maven.
 * @param additionalFolderBases an array containing the locations of additional base folders to be considered.
 */
public LocalRepositoryCollector(MavenProject project, //
        ArtifactRepository localRepository, File[] additionalFolderBases) {
    // The local repo will always be in this list and it is optimal to assume that it will be hit tested the most
    // outside of here.
    localRepositoryPaths.add(localRepository.getBasedir());

    // Determine the root project

    String projectArtifactId = project.getArtifactId();

    MavenProject parentProject = project.getParent();
    while (parentProject != null) {
        List<?> modules = parentProject.getModules();
        if (modules.contains(projectArtifactId)) {
            projectArtifactId = parentProject.getArtifactId();
            String parentProjectPath = parentProject.getBasedir().getAbsolutePath();
            localRepositoryPaths.add(parentProjectPath);
        } else {
            break;
        }

        parentProject = parentProject.getParent();
    }

    // Consider additional folders and their immediate sub folders.

    for (File additionalFolderBase : additionalFolderBases) {
        File[] additionalFolders = additionalFolderBase.listFiles();
        if (additionalFolders != null) {
            for (File additionalFolder : additionalFolders) {
                if (additionalFolder.isDirectory()) {
                    localRepositoryPaths.add(additionalFolder.getAbsolutePath());
                }
            }
        }
    }
}

From source file:org.codehaus.mojo.pom.AbstractPomModifyMojo.java

License:Apache License

/**
 * {@inheritDoc}/*from  ww  w  . j ava  2s. co m*/
 */
public void execute() throws MojoExecutionException, MojoFailureException {

    super.execute();
    this.matcher = new DependencyPatternMatcher(this.groupId, this.artifactId, this.version, this.type,
            this.scope, this.classifier);
    // pass 1: search matching modules in reactor...
    for (MavenProject module : this.reactorProjects) {
        ProjectContainer parent = null;
        if (module.getParent() != null) {
            MavenProject parentProject = module.getParent();
            ProjectId parentId = new ProjectId(parentProject);
            parent = this.projectContainerMap.get(parentId);
            if (parent == null) {
                getLog().debug("Parent project '" + parentId + "' NOT in reactor.");
            }
        }
        ProjectContainer container = new ProjectContainer(module, parent, getLog());
        this.projectContainerMap.put(container.getId(), container);
        // getLog().info("Matching " + module);
    }
    // pass 2: modify modules...
    for (MavenProject module : this.reactorProjects) {
        ProjectId pid = new ProjectId(module);
        ProjectContainer container = this.projectContainerMap.get(pid);
        execute(container);
    }
    // pass 3: save modified POMs...
    for (MavenProject module : this.reactorProjects) {
        ProjectId pid = new ProjectId(module);
        ProjectContainer container = this.projectContainerMap.get(pid);
        container.save(this.xmlEncoding, this.overwrite);
    }
}

From source file:org.codehaus.mojo.pom.PomRefactorMojo.java

License:Apache License

/**
 * {@inheritDoc}/*from  w  ww.  j  a va  2s . c  o m*/
 */
@Override
@SuppressWarnings("unchecked")
public void execute(ProjectContainer projectContainer) throws MojoExecutionException, MojoFailureException {

    if (getNewAttributes().isEmpty()) {
        throw new MojoExecutionException(
                "At least one of the new*-parameters (newVersion, newArtifactId, newGroupId, newScope or newClassifier) have to be configured!");
    }
    super.execute(projectContainer);
    MavenProject project = projectContainer.getProject();
    updateProject(projectContainer, project, false);
    MavenProject parent = project.getParent();
    if (parent != null) {
        updateProject(projectContainer, parent, true);
    }
    updateDependencies(project.getDependencies(), projectContainer, ProjectContainer.XML_TAG_DEPENDENCIES);
    DependencyManagement dependencyManagement = project.getDependencyManagement();
    if (dependencyManagement != null) {
        updateDependencies(dependencyManagement.getDependencies(), projectContainer,
                ProjectContainer.XML_TAG_DEPENDENCY_MANAGEMENT);
    }
}

From source file:org.codehaus.mojo.versions.DisplayPluginUpdatesMojo.java

License:Apache License

private String getRequiredMavenVersion(MavenProject mavenProject, String defaultValue) {
    ArtifactVersion requiredMavenVersion = null;
    while (mavenProject != null) {
        final Prerequisites prerequisites = mavenProject.getPrerequisites();
        final String mavenVersion = prerequisites == null ? null : prerequisites.getMaven();
        if (mavenVersion != null) {
            final ArtifactVersion v = new DefaultArtifactVersion(mavenVersion);
            if (requiredMavenVersion == null || requiredMavenVersion.compareTo(v) < 0) {
                requiredMavenVersion = v;
            }/*from  w  w w . j  a v  a  2s.  c o  m*/
        }
        mavenProject = mavenProject.getParent();
    }
    return requiredMavenVersion == null ? defaultValue : requiredMavenVersion.toString();
}

From source file:org.codehaus.mojo.versions.DisplayPluginUpdatesMojo.java

License:Apache License

/**
 * Returns all the parent projects of the specified project, with the root project first.
 *
 * @param project The maven project to get the parents of
 * @return the parent projects of the specified project, with the root project first.
 * @throws org.apache.maven.plugin.MojoExecutionException
 *          if the super-pom could not be created.
 * @since 1.0-alpha-1/*from  ww  w . jav a2s .c  om*/
 */
private List<MavenProject> getParentProjects(MavenProject project) throws MojoExecutionException {
    List<MavenProject> parents = new ArrayList<MavenProject>();
    while (project.getParent() != null) {
        project = project.getParent();
        parents.add(0, project);
    }
    return parents;
}

From source file:org.commonjava.emb.project.ProjectLoader.java

License:Apache License

private void addProjects(final ProjectToolsSession session, final List<MavenProject> projects) {
    final DependencyGraph depGraph = session.getDependencyGraph();
    for (final MavenProject project : projects) {
        final LinkedList<Artifact> parentage = new LinkedList<Artifact>();
        MavenProject parent = project;
        while (parent != null) {
            final org.apache.maven.artifact.Artifact pomArtifact = mavenRepositorySystem
                    .createArtifact(project.getGroupId(), project.getArtifactId(), project.getVersion(), "pom");

            final Artifact aetherPomArtifact = RepositoryUtils.toArtifact(pomArtifact);

            parentage.addFirst(aetherPomArtifact);

            parent = parent.getParent();
        }/*from   w  ww .j a  v  a2  s . c  o m*/

        Artifact current = parentage.removeFirst();
        while (!parentage.isEmpty()) {
            final Artifact next = parentage.getFirst();

            // This is WEIRD, but the parent POM is actually a dependency of the current one,
            // since it's required in order to build the current project...
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Marking parent POM: " + current + " as dependency of POM: " + next);
            }
            depGraph.addDependency(next, current, true, true);

            if (!parentage.isEmpty()) {
                current = parentage.removeFirst();
            }
        }
    }
}

From source file:org.commonjava.maven.plugins.betterdep.AbstractDepgraphGoal.java

License:Open Source License

protected void readFromReactorProjects() throws MojoExecutionException {
    getLog().info("Initializing direct graph relationships from reactor projects: " + projects);
    for (final MavenProject project : projects) {
        final List<Profile> activeProfiles = project.getActiveProfiles();
        if (activeProfiles != null) {
            for (final Profile profile : activeProfiles) {
                profiles.add(RelationshipUtils.profileLocation(profile.getId()));
            }/*from   w  w w .  j a  v a2s.co  m*/
        }

        final ProjectVersionRef projectRef = new SimpleProjectVersionRef(project.getGroupId(),
                project.getArtifactId(), project.getVersion());
        roots.add(projectRef);

        final List<Dependency> deps = project.getDependencies();
        //            final List<ProjectVersionRef> roots = new ArrayList<ProjectVersionRef>( deps.size() );

        URI localUri;
        try {
            localUri = new URI(MavenLocationExpander.LOCAL_URI);
        } catch (final URISyntaxException e) {
            throw new MojoExecutionException(
                    "Failed to construct dummy local URI to use as the source of the current project in the depgraph: "
                            + e.getMessage(),
                    e);
        }

        final int index = 0;
        for (final Dependency dep : deps) {
            final ProjectVersionRef depRef = new SimpleProjectVersionRef(dep.getGroupId(), dep.getArtifactId(),
                    dep.getVersion());

            //                roots.add( depRef );

            final List<Exclusion> exclusions = dep.getExclusions();
            final List<ProjectRef> excludes = new ArrayList<ProjectRef>();
            if (exclusions != null && !exclusions.isEmpty()) {
                for (final Exclusion exclusion : exclusions) {
                    excludes.add(new SimpleProjectRef(exclusion.getGroupId(), exclusion.getArtifactId()));
                }
            }

            rootRels.add(new SimpleDependencyRelationship(localUri, projectRef,
                    new SimpleArtifactRef(depRef, dep.getType(), dep.getClassifier(), dep.isOptional()),
                    DependencyScope.getScope(dep.getScope()), index, false,
                    excludes.toArray(new ProjectRef[excludes.size()])));
        }

        final DependencyManagement dm = project.getDependencyManagement();
        if (dm != null) {
            final List<Dependency> managed = dm.getDependencies();
            int i = 0;
            for (final Dependency dep : managed) {
                if ("pom".equals(dep.getType()) && "import".equals(dep.getScope())) {
                    final ProjectVersionRef depRef = new SimpleProjectVersionRef(dep.getGroupId(),
                            dep.getArtifactId(), dep.getVersion());
                    rootRels.add(new SimpleBomRelationship(localUri, projectRef, depRef, i++));
                }
            }
        }

        ProjectVersionRef lastParent = projectRef;
        MavenProject parent = project.getParent();
        while (parent != null) {
            final ProjectVersionRef parentRef = new SimpleProjectVersionRef(parent.getGroupId(),
                    parent.getArtifactId(), parent.getVersion());

            rootRels.add(new SimpleParentRelationship(localUri, projectRef, parentRef));

            lastParent = parentRef;
            parent = parent.getParent();
        }

        rootRels.add(new SimpleParentRelationship(lastParent));
    }

}

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

License:Apache License

/**
 * {@inheritDoc}//from   ww  w.java 2  s  . co  m
 * @throws MojoExecutionException 
 * @see org.commonjava.maven.plugins.execroot.AbstractDirectoryGoal#findDirectory()
 */
@Override
protected File findDirectory() throws MojoExecutionException {
    File dir = null;

    final Stack<MavenProject> toCheck = new Stack<MavenProject>();
    toCheck.addAll(projects);

    while (!toCheck.isEmpty()) {
        final MavenProject p = toCheck.pop();
        if (project.matches(p)) {
            dir = p.getBasedir();
            break;
        }

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

    if (dir == null) {
        throw new MojoExecutionException("Cannot find directory for project: " + project);
    }

    return dir;
}