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:hudson.gridmaven.reporters.MavenFingerprinter.java

License:Open Source License

private void recordParents(MavenBuildProxy build, MavenProject pom) throws IOException, InterruptedException {
    MavenProject parent = pom.getParent();
    while (parent != null) {
        File parentFile = parent.getFile();

        if (parentFile == null) {
            // Parent artifact contains no actual file, so we resolve against
            // the local repository
            ArtifactRepository localRepository = getLocalRepository(build.getMavenBuildInformation(), parent,
                    pom);//  w w  w  .ja  v  a  2  s.com
            if (localRepository != null) {
                Artifact parentArtifact = getArtifact(parent);
                // Don't use ArtifactRepository.find(), for compatibility with Maven 2.x
                if (parentArtifact != null) {
                    parentFile = new File(localRepository.getBasedir(), localRepository.pathOf(parentArtifact));
                }
            }
        }

        if (parentFile != null) {
            // we need to include the artifact Id for poms as well, otherwise a
            // project with the same groupId would override its parent's
            // fingerprint
            record(parent.getGroupId() + ":" + parent.getArtifactId(), parentFile, used);
        }
        parent = parent.getParent();
    }
}

From source file:info.mikaelsvensson.devtools.sitesearch.SiteSearchPlugin.java

License:Apache License

private Map<String, String> getProperties() {
    if (null == properties) {
        properties = new HashMap<String, String>();
        try {//from   w ww . ja va 2s.  c  om
            File dir = baseDirectory;
            MavenProject proj = project;
            while (proj != null) {
                File file = siteTool.getSiteDescriptorFromBasedir(siteDirectory, dir, null);
                if (file.exists() && file.isFile()) {
                    DecorationModel decorationModel = getDecorationModel(file);
                    Xpp3Dom custom = (Xpp3Dom) decorationModel.getCustom();
                    if (custom != null) {
                        Xpp3Dom siteSearchPluginElement = custom.getChild("siteSearchPlugin");
                        if (null != siteSearchPluginElement) {
                            Xpp3Dom[] propertyElements = siteSearchPluginElement.getChildren();
                            for (Xpp3Dom propertyElement : propertyElements) {
                                if (!properties.containsKey(propertyElement.getName())) {
                                    properties.put(propertyElement.getName(), propertyElement.getValue());
                                }
                            }
                            getLog().info(
                                    "Loaded configuration parameters from " + file.getAbsolutePath() + ".");
                        } else {
                            getLog().info("No site indexer configuration parameters defined in "
                                    + file.getAbsolutePath() + ".");
                        }
                    }
                } else {
                    getLog().info("Looked for " + file.getAbsolutePath() + " but couldn't find it.");
                }
                proj = proj.getParent();
                dir = proj != null ? proj.getBasedir() : null;
            }
        } catch (XmlPullParserException e) {
            getLog().warn(e);
        } catch (IOException e) {
            getLog().warn(e);
        }
    }
    return properties;
}

From source file:info.mikaelsvensson.devtools.sitesearch.SiteSearchPlugin.java

License:Apache License

private void addSearchFormToPage(final File file) {
    try {/*from ww  w  .  j a v a2 s  .c  o m*/
        Set<String> relatedProjectPaths = getRelatedIndexedProjectPaths();

        StringBuilder content = new StringBuilder(FileUtils.readFileToString(file));

        Map<String, String> props = getProperties();

        String findExpr = props.get(PROPERTY_SEARCHBOX_CONTAINER_INSERTAT);
        Pattern insertionPointPattern = Pattern.compile(findExpr);
        ModifyAction modifyAction = ModifyAction
                .valueOf(props.get(PROPERTY_SEARCHBOX_CONTAINER_INSERTACTION).toUpperCase());
        String html = getSearchForm();
        boolean isFormInserted = modifyStringBuilder(content, insertionPointPattern, modifyAction, html);
        if (isFormInserted) {
            if (shouldJqueryLibraryFileBeIncluded()) {
                modifyStringBuilder(content, END_OF_HEAD_PATTERN, ModifyAction.PREPEND,
                        getScriptFileElement(getRelativePath(file, getJqueryLibraryFile())));
            }

            modifyStringBuilder(content, END_OF_HEAD_PATTERN, ModifyAction.PREPEND,
                    getScriptFileElement(getRelativePath(file, getJavascriptLibraryFile())));

            String pathToProjectRoot = getRelativePath(file, getSiteOutputFolder());

            for (String relatedProjectPath : relatedProjectPaths) {
                if (StringUtils.isNotBlank(relatedProjectPath)) {
                    modifyStringBuilder(content, END_OF_HEAD_PATTERN, ModifyAction.PREPEND,
                            getScriptFileElement(PathUtils.trailingSlash(pathToProjectRoot)
                                    + PathUtils.trailingSlash(relatedProjectPath)
                                    + JAVASCRIPT_DATABASE_FILE_NAME));
                }
            }

            String pathToAncestorProjectRoot = pathToProjectRoot;
            MavenProject ancestorProject = project;
            while (true) {
                if (ancestorProject.getParent() != null) {
                    pathToAncestorProjectRoot = "../" + pathToAncestorProjectRoot;
                    ancestorProject = ancestorProject.getParent();
                } else {
                    break;
                }
            }

            modifyStringBuilder(content, END_OF_HEAD_PATTERN, ModifyAction.PREPEND,
                    getScriptFileElement(getRelativePath(file, getJavascriptDatabaseFile())));

            modifyStringBuilder(content, END_OF_HEAD_PATTERN, ModifyAction.PREPEND,
                    getStylesheetFileElement(getRelativePath(file, getStylesheetFile())));

            modifyStringBuilder(content, END_OF_HEAD_PATTERN, ModifyAction.PREPEND,
                    getScriptElement(MessageFormat.format(JAVASCRIPT_INIT_TEMPLATE,
                            getProperties().get(PROPERTY_SEARCHBOX_CONTAINER_ID),
                            getRelativePath(file, getSiteOutputFolder()),
                            JAVASCRIPT_VARIABLE_NAME_INDEX_DATA)));
        }

        FileUtils.writeStringToFile(file, content.toString());
    } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
}

From source file:info.mikaelsvensson.devtools.sitesearch.SiteSearchPlugin.java

License:Apache License

private MavenProject getRootProject() {
    MavenProject rootProject = project;
    while (rootProject.getParent() != null) {
        rootProject = rootProject.getParent();
    }/*from w  w  w  . ja va2s.c  o m*/
    return rootProject;
}

From source file:io.fabric8.maven.AbstractNamespacedMojo.java

License:Apache License

/**
 * Returns the root project folder//w  ww  . j a  v a2s  .  c  om
 */
protected File getRootProjectFolder() {
    File answer = null;
    MavenProject project = getProject();
    while (project != null) {
        File basedir = project.getBasedir();
        if (basedir != null) {
            answer = basedir;
        }
        project = project.getParent();
    }
    return answer;
}

From source file:io.fabric8.maven.AbstractNamespacedMojo.java

License:Apache License

/**
 * Returns the root project folder//  w w w  . j  ava 2 s  . c  o m
 */
protected MavenProject getRootProject() {
    MavenProject project = getProject();
    while (project != null) {
        MavenProject parent = project.getParent();
        if (parent == null) {
            break;
        }
        project = parent;
    }
    return project;
}

From source file:io.fabric8.maven.core.util.MavenUtil.java

License:Apache License

/**
 * Returns the root maven project or null if there is no maven project
 *//*  ww  w  .  j av  a 2 s. c o m*/
public static MavenProject getRootProject(MavenProject project) {
    while (project != null) {
        MavenProject parent = project.getParent();
        if (parent == null) {
            break;
        }
        project = parent;
    }
    return project;
}

From source file:io.fabric8.maven.CreateProfileZipMojo.java

License:Apache License

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    try {/*from  ww w.ja va 2s.  c  o  m*/
        if (isIgnoreProject())
            return;

        generateZip();

        if (reactorProjects != null) {
            List<MavenProject> pomZipProjects = new ArrayList<>();
            List<MavenProject> fabricZipGoalProjects = new ArrayList<>();
            List<MavenProject> fabricHasParentZipGoalProject = new ArrayList<MavenProject>();
            for (MavenProject reactorProject : reactorProjects) {

                if ("pom".equals(reactorProject.getPackaging())) {
                    pomZipProjects.add(reactorProject);
                }

                List<Plugin> buildPlugins = reactorProject.getBuildPlugins();
                for (Plugin buildPlugin : buildPlugins) {
                    String artifactId = buildPlugin.getArtifactId();
                    // TODO I guess we could try find if the "zip" goal is being invoked?
                    if ("fabric8-maven-plugin".equals(artifactId)) {
                        // TODO should we only consider reactorProjects which have a fabric8:zip goal?
                        Object goals = buildPlugin.getGoals();
                        boolean hasZipGoal = goals != null && goals.toString().contains("zip");
                        List<PluginExecution> executions = buildPlugin.getExecutions();
                        for (PluginExecution execution : executions) {
                            List<String> execGoals = execution.getGoals();
                            if (execGoals.contains("zip")) {
                                hasZipGoal = true;
                            }
                        }
                        getLog().debug(
                                "project " + reactorProject.getArtifactId() + " has zip goal: " + hasZipGoal);

                        fabricZipGoalProjects.add(reactorProject);
                    }
                }
            }

            // we want a list of projects which has a parent that has a zip goal too
            // as that helps us detect the 'last' project when we do a full build from the entire project
            for (MavenProject project : fabricZipGoalProjects) {
                if (fabricZipGoalProjects.contains(project.getParent())) {
                    fabricHasParentZipGoalProject.add(project);
                }
            }

            // are we the last project?
            boolean last = reactorProjects.size() > 1
                    && project == reactorProjects.get(reactorProjects.size() - 1);
            if (!last) {
                // are we the last project with the zip goal, part of a group as they have a parent?
                // TODO: there can be multiple groups, so when we switch to a new group we should aggregate
                last = fabricHasParentZipGoalProject.size() > 1 && project == fabricHasParentZipGoalProject
                        .get(fabricHasParentZipGoalProject.size() - 1);
            }
            if (!last) {
                // are we the last project with the zip goal?
                last = fabricZipGoalProjects.size() > 1
                        && project == fabricZipGoalProjects.get(fabricZipGoalProjects.size() - 1);
            }

            // we need to generate the aggregated zip last, so we have all the generated profiles in the other modules
            // which we can aggregate
            if (last) {
                getLog().info("");
                getLog().info("Creating aggregated profile zip");
                getLog().info("built the last fabric8:zip project so generating a combined zip for all "
                        + fabricZipGoalProjects.size() + " projects with a fabric8:zip goal");

                // favor root project as the 1st project with fabric8:zip goal
                MavenProject rootProject = fabricZipGoalProjects.size() > 0 ? fabricZipGoalProjects.get(0)
                        : reactorProjects.get(0);

                // we got the root project, now filter out pom projects which has the rootProject as one of their parents
                List<MavenProject> ourPomZipProjects = new ArrayList<MavenProject>();
                // include the root project if its a zip as well
                if (pomZipProjects.contains(rootProject)) {
                    ourPomZipProjects.add(rootProject);
                }
                ourPomZipProjects.add(rootProject);
                for (MavenProject zip : pomZipProjects) {
                    if (hasParent(zip, rootProject, true)) {
                        ourPomZipProjects.add(zip);
                    }
                }

                getLog().info("Choosing root project " + rootProject.getArtifactId()
                        + " for generation of aggregated zip");
                generateAggregatedZip(rootProject, fabricZipGoalProjects, ourPomZipProjects);
            }
        }

    } catch (MojoFailureException e) {
        throw e;
    } catch (MojoExecutionException e) {
        throw e;
    } catch (Exception e) {
        throw new MojoExecutionException("Error executing", e);
    }
}

From source file:io.fabric8.maven.CreateProfileZipMojo.java

License:Apache License

protected boolean hasParent(MavenProject me, MavenProject parent, boolean recusive) {
    if (me == null) {
        return false;
    } else if (me.getParent() == parent) {
        return true;
    } else if (recusive) {
        return hasParent(me.getParent(), parent, recusive);
    } else {//  www. java2s  .  co  m
        return false;
    }
}

From source file:io.fabric8.maven.enricher.fabric8.DocLinkEnricher.java

License:Apache License

protected DistributionManagement findProjectDistributionManagement() {
    MavenProject project = getProject();
    while (project != null) {
        DistributionManagement distributionManagement = project.getDistributionManagement();
        if (distributionManagement != null) {
            return distributionManagement;
        }//from   w w  w.  ja  va2 s.c  om
        project = project.getParent();
    }
    return null;
}