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

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

Introduction

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

Prototype

public List<String> getModules() 

Source Link

Usage

From source file:org.maven.ide.eclipse.embedder.MavenModelManager.java

License:Apache License

private void initMavenProject(IFile pomFile, IFile rootPomFile, Map mavenProjects, IProgressMonitor monitor,
        ResolverConfiguration resolverConfiguration) throws CoreException {
    String pomKey = getPomFileKey(pomFile);
    if (mavenProjects.containsKey(pomKey)) {
        return;//  ww w. j  ava 2s . co m
    }

    MavenExecutionResult result = readMavenProject(pomFile.getLocation().toFile(), monitor, true, false,
            resolverConfiguration);
    MavenProject mavenProject = result.getProject();
    if (mavenProject == null) {
        return;
    }

    mavenProjects.put(pomKey, mavenProject);

    Set artifacts = mavenProject.getArtifacts();
    for (Iterator it = artifacts.iterator(); it.hasNext();) {
        if (monitor.isCanceled()) {
            throw new OperationCanceledException();
        }
        Artifact artifact = (Artifact) it.next();
        addProjectArtifact(pomFile, artifact);
        addProjectArtifact(rootPomFile, artifact);
    }

    if (resolverConfiguration.shouldIncludeModules()) {
        IContainer parent = pomFile.getParent();
        for (Iterator it = mavenProject.getModules().iterator(); it.hasNext();) {
            if (monitor.isCanceled()) {
                throw new OperationCanceledException();
            }
            String module = (String) it.next();
            IResource memberPom = parent.findMember(module + "/" + Maven2Plugin.POM_FILE_NAME); //$NON-NLS-1$
            if (memberPom != null && memberPom.getType() == IResource.FILE && memberPom.isAccessible()) {
                initMavenProject((IFile) memberPom, rootPomFile, mavenProjects, monitor, resolverConfiguration);
            }
        }
    }
}

From source file:org.owasp.dependencycheck.maven.AggregateMojo.java

License:Apache License

/**
 * Returns a set containing all the descendant projects of the given
 * project.//from www. j a v a 2  s . c o m
 *
 * @param project the project for which all descendants will be returned
 * @return the set of descendant projects
 */
protected Set<MavenProject> getDescendants(MavenProject project) {
    if (project == null) {
        return Collections.emptySet();
    }
    final Set<MavenProject> descendants = new HashSet<>();
    int size;
    if (getLog().isDebugEnabled()) {
        getLog().debug(String.format("Collecting descendants of %s", project.getName()));
    }
    for (String m : project.getModules()) {
        for (MavenProject mod : getReactorProjects()) {
            try {
                File mpp = new File(project.getBasedir(), m);
                mpp = mpp.getCanonicalFile();
                if (mpp.compareTo(mod.getBasedir()) == 0 && descendants.add(mod) && getLog().isDebugEnabled()) {
                    getLog().debug(String.format("Descendant module %s added", mod.getName()));

                }
            } catch (IOException ex) {
                if (getLog().isDebugEnabled()) {
                    getLog().debug("Unable to determine module path", ex);
                }
            }
        }
    }
    do {
        size = descendants.size();
        for (MavenProject p : getReactorProjects()) {
            if (project.equals(p.getParent()) || descendants.contains(p.getParent())) {
                if (descendants.add(p) && getLog().isDebugEnabled()) {
                    getLog().debug(String.format("Descendant %s added", p.getName()));

                }
                for (MavenProject modTest : getReactorProjects()) {
                    if (p.getModules() != null && p.getModules().contains(modTest.getName())
                            && descendants.add(modTest) && getLog().isDebugEnabled()) {
                        getLog().debug(String.format("Descendant %s added", modTest.getName()));
                    }
                }
            }
            final Set<MavenProject> addedDescendants = new HashSet<>();
            for (MavenProject dec : descendants) {
                for (String mod : dec.getModules()) {
                    try {
                        File mpp = new File(dec.getBasedir(), mod);
                        mpp = mpp.getCanonicalFile();
                        if (mpp.compareTo(p.getBasedir()) == 0) {
                            addedDescendants.add(p);
                        }
                    } catch (IOException ex) {
                        if (getLog().isDebugEnabled()) {
                            getLog().debug("Unable to determine module path", ex);
                        }
                    }
                }
            }
            for (MavenProject addedDescendant : addedDescendants) {
                if (descendants.add(addedDescendant) && getLog().isDebugEnabled()) {
                    getLog().debug(String.format("Descendant module %s added", addedDescendant.getName()));
                }
            }
        }
    } while (size != 0 && size != descendants.size());
    if (getLog().isDebugEnabled()) {
        getLog().debug(String.format("%s has %d children", project, descendants.size()));
    }
    return descendants;
}

From source file:org.phpmaven.plugin.pear.PearCreatePomMojo.java

License:Apache License

/**
 * @inheritDoc//from w ww  .j av  a  2 s .co m
 */
public void execute() throws MojoExecutionException {
    if (this.pearGroupId == null) {
        this.pearGroupId = this.channelToGroupId(this.pearChannelAlias);
    }
    if (this.pearArtifactId == null) {
        this.pearArtifactId = this.pearPackage;
    }

    final String[] versions = this.pearPackageVersion.split(",");
    final File path = this.pomTargetFile;

    if (versions.length > 1 && this.pearPackageMavenVersion != null) {
        throw new MojoExecutionException(
                "Multiple versions cannot be used with option pearPackageMavenVersion");
    }

    final File commonPom = new File(this.pomTargetFile, this.pearGroupId + "/pom.xml");
    if (!commonPom.exists()) {
        final StringBuffer modulesXmlBuffer = new StringBuffer();
        modulesXmlBuffer.append("    <modules>\n");
        modulesXmlBuffer.append("    </modules>\n");

        String pomXml = COMMON_XML_TEMPLATE.replace("${PEAR.CHANNEL}", this.pearGroupId.replace(".", "_"));
        pomXml = pomXml.replace("${TARGET.NAME}", this.pearName);
        pomXml = pomXml.replace("${REPOS.ID}", this.deployRepsitoryId);
        pomXml = pomXml.replace("${REPOS.NAME}", escapeXml(this.deployRepositoryName));
        pomXml = pomXml.replace("${REPOS.URL}", this.deployRepositoryUrl);
        pomXml = pomXml.replace("${SNAPSHOTS.ID}", this.snapshotsRepositoryId);
        pomXml = pomXml.replace("${SNAPSHOTS.NAME}", escapeXml(this.snapshotsRepositoryName));
        pomXml = pomXml.replace("${SNAPSHOTS.URL}", this.snapshotsRepositoryUrl);
        pomXml = pomXml.replace("${MODULES}", modulesXmlBuffer.toString());

        this.createPomFile(pomXml, commonPom);
    }

    try {
        final MavenProject commonProject = this.getProjectFromPom(commonPom);

        for (final String version : versions) {
            if (versions.length > 1 || this.pearPackageMavenVersion == null) {
                this.pearPackageMavenVersion = version;
            }
            this.pearPackageVersion = version;
            final String moduleName = this.pearPackage + "-" + this.pearPackageMavenVersion;
            this.pomTargetFile = new File(path, this.pearGroupId + "/" + moduleName + "/pom.xml");
            this.createThePom();
            final List<String> modules = commonProject.getModules();
            if (!modules.contains(moduleName)) {
                commonProject.getModel().addModule(moduleName);
                commonProject.writeModel(new FileWriter(commonPom));
            }
        }
    } catch (ProjectBuildingException ex) {
        throw new MojoExecutionException("Unable to read common pom " + commonPom, ex);
    } catch (IOException ex) {
        throw new MojoExecutionException("Unable to write common pom " + commonPom, ex);
    }
}

From source file:org.phpmaven.sitemap.SitemapIndexMojo.java

License:Apache License

private void parseProject(final List<String> urls, final MavenProject project) throws ProjectBuildingException {
    for (final Plugin plugin : project.getBuild().getPlugins()) {
        if (plugin.getArtifactId().equals("sitemap-plugin")
                && plugin.getGroupId().equals("org.phpmaven.sites")) {
            urls.add(project.getUrl());/*from   w ww  . j  a v a2  s.c o m*/
        }
    }

    for (final String module : project.getModules()) {
        final File moduleFolder = new File(project.getBasedir(), module);
        final File pomFile = new File(moduleFolder, "pom.xml");
        if (pomFile.exists()) {
            this.parseProject(urls, this.getProjectFromPom(pomFile));
        }
    }
}

From source file:org.phpmaven.test.it.AbstractTestCase.java

License:Apache License

/**
 * Installs a local project into target directory
 * @param reposPath repos path//w  w w  .  j av a2  s  . c o  m
 * @param root root of the project to be installed
 * @param withModules
 * @throws Exception
 */
private void installLocalProject(final String reposPath, String root, boolean withModules) throws Exception {
    final String projectName = new File(root).getName();
    if (installedProjects.contains(projectName)) {
        return;
    }
    installedProjects.add(projectName);

    if (!projectName.equals("java-parent")) {
        //            this.installLocalProject(reposPath, new File(root).getParentFile().getParentFile().getParent() + "/var", false);
        this.installLocalProject(reposPath, new File(root).getParent() + "/java-parent", false);
    }

    // first install dependencies
    final File projectFile = new File(root, "pom.xml");
    final ProjectBuildingRequest buildingRequest = this.createProjectBuildingRequest().projectBuildingRequest;

    final MavenProject project = lookup(ProjectBuilder.class).build(projectFile, buildingRequest).getProject();

    for (final Dependency dep : project.getDependencies()) {
        if ("org.phpmaven".equals(dep.getGroupId())) {
            this.installLocalProject(reposPath, new File(root).getParent() + "/" + dep.getArtifactId(), false);
        }
    }

    // install the project itself
    final Verifier verifier = new Verifier(root, true);
    verifier.setLocalRepo(reposPath);
    verifier.setAutoclean(false);
    verifier.setForkJvm(true);
    final File target = new File(root, "target");
    if (!target.exists()) {
        target.mkdir();
    }
    verifier.setLogFileName("target/log.txt");
    verifier.addCliOption("-N");
    verifier.addCliOption("-nsu");
    verifier.addCliOption("-Dmaven.test.skip=true");
    verifier.executeGoal("install");
    verifier.verifyErrorFreeLog();
    verifier.resetStreams();

    if (withModules) {
        for (final String module : project.getModules()) {
            this.installLocalProject(reposPath, new File(root) + "/" + module, false);
        }
    }
}

From source file:org.seasar.kvasir.plust.KvasirPlugin.java

@SuppressWarnings("unchecked")
public void resolveClasspathEntries(Set<IClasspathEntry> libraryEntries, Set<String> moduleArtifacts,
        IFile pomFile, boolean recursive, boolean downloadSources, IProgressMonitor monitor) {
    monitor.beginTask("Reading " + pomFile.getLocation(), IProgressMonitor.UNKNOWN);
    try {/*from  w  ww  .  j  av  a 2 s  .c o m*/
        if (monitor.isCanceled()) {
            throw new OperationCanceledException();
        }

        final MavenProject mavenProject = getMavenProject(pomFile, new SubProgressMonitor(monitor, 1));
        if (mavenProject == null) {
            return;
        }

        deleteMarkers(pomFile);
        // TODO use version?
        moduleArtifacts.add(mavenProject.getGroupId() + ":" + mavenProject.getArtifactId());

        Set artifacts = mavenProject.getArtifacts();
        for (Iterator it = artifacts.iterator(); it.hasNext();) {
            if (monitor.isCanceled()) {
                throw new OperationCanceledException();
            }

            final Artifact a = (Artifact) it.next();

            monitor.subTask("Processing " + a.getId());

            if (!"jar".equals(a.getType())) {
                continue;
            }
            // TODO use version?
            if (!moduleArtifacts.contains(a.getGroupId() + ":" + a.getArtifactId()) &&
            // TODO verify if there is an Eclipse API to check that archive is acceptable
                    ("jar".equals(a.getType()) || "zip".equals(a.getType()))) {
                String artifactLocation = a.getFile().getAbsolutePath();

                // TODO add a lookup through workspace projects

                Path srcPath = null;
                File srcFile = new File(
                        artifactLocation.substring(0, artifactLocation.length() - 4) + "-sources.jar");
                if (srcFile.exists()) {
                    // XXX ugly hack to do not download any sources
                    srcPath = new Path(srcFile.getAbsolutePath());
                } else if (downloadSources && !isSourceChecked(a)) {
                    srcPath = (Path) executeInEmbedder(new MavenEmbedderCallback() {
                        public Object run(MavenEmbedder mavenEmbedder, IProgressMonitor monitor) {
                            monitor.beginTask("Resolve sources " + a.getId(), IProgressMonitor.UNKNOWN);
                            try {
                                Artifact src = mavenEmbedder.createArtifactWithClassifier(a.getGroupId(),
                                        a.getArtifactId(), a.getVersion(), "java-source", "sources");
                                if (src != null) {
                                    mavenEmbedder.resolve(src, mavenProject.getRemoteArtifactRepositories(),
                                            mavenEmbedder.getLocalRepository());
                                    return new Path(src.getFile().getAbsolutePath());
                                }
                            } catch (AbstractArtifactResolutionException ex) {
                                String name = ex.getGroupId() + ":" + ex.getArtifactId() + "-" + ex.getVersion()
                                        + "." + ex.getType();
                                getConsole().logMessage(ex.getOriginalMessage() + " " + name);
                            } finally {
                                monitor.done();
                            }
                            return null;
                        }
                    }, new SubProgressMonitor(monitor, 1));
                    setSourceChecked(a);
                }

                libraryEntries.add(JavaCore.newLibraryEntry(new Path(artifactLocation), srcPath, null));
            }
        }

        if (recursive) {
            IContainer parent = pomFile.getParent();

            List modules = mavenProject.getModules();
            for (Iterator it = modules.iterator(); it.hasNext() && !monitor.isCanceled();) {
                if (monitor.isCanceled()) {
                    throw new OperationCanceledException();
                }

                String module = (String) it.next();
                IResource memberPom = parent.findMember(module + "/" + IKvasirProject.POM_FILE_NAME);
                if (memberPom != null && memberPom.getType() == IResource.FILE) {
                    resolveClasspathEntries(libraryEntries, moduleArtifacts, (IFile) memberPom, true,
                            downloadSources, new SubProgressMonitor(monitor, 1));
                }
            }
        }
    } catch (OperationCanceledException ex) {
        throw ex;
    } catch (InvalidArtifactRTException ex) {
        addMarker(pomFile, ex.getBaseMessage(), 1, IMarker.SEVERITY_ERROR);
    } catch (Throwable ex) {
        addMarker(pomFile, ex.toString(), 1, IMarker.SEVERITY_ERROR);
    } finally {
        monitor.done();
    }
}

From source file:org.sonar.batch.maven.MavenProjectConverter.java

License:Open Source License

private void rebuildModuleHierarchy(Map<String, MavenProject> paths, Map<MavenProject, ProjectDefinition> defs)
        throws IOException {
    for (Map.Entry<String, MavenProject> entry : paths.entrySet()) {
        MavenProject pom = entry.getValue();
        for (Object m : pom.getModules()) {
            String moduleId = (String) m;
            File modulePath = new File(pom.getBasedir(), moduleId);
            MavenProject module = findMavenProject(modulePath, paths);

            ProjectDefinition parentProject = defs.get(pom);
            if (parentProject == null) {
                throw new IllegalStateException(UNABLE_TO_DETERMINE_PROJECT_STRUCTURE_EXCEPTION_MESSAGE);
            }/* w ww . ja  v  a  2  s .c om*/
            ProjectDefinition subProject = defs.get(module);
            if (subProject == null) {
                throw new IllegalStateException(UNABLE_TO_DETERMINE_PROJECT_STRUCTURE_EXCEPTION_MESSAGE);
            }
            parentProject.addSubProject(subProject);
        }
    }
}

From source file:org.sonar.batch.MavenProjectConverter.java

License:Open Source License

public static ProjectDefinition convert(List<MavenProject> poms, MavenProject root) {
    Map<String, MavenProject> paths = Maps.newHashMap(); // projects by canonical path
    Map<MavenProject, ProjectDefinition> defs = Maps.newHashMap();

    try {/*from  w ww  . j av a 2  s. c o m*/
        for (MavenProject pom : poms) {
            String basedir = pom.getBasedir().getCanonicalPath();
            paths.put(basedir, pom);
            defs.put(pom, convert(pom));
        }

        for (Map.Entry<String, MavenProject> entry : paths.entrySet()) {
            MavenProject pom = entry.getValue();
            for (Object moduleId : pom.getModules()) {
                File modulePath = new File(pom.getBasedir(), (String) moduleId);
                MavenProject module = paths.get(modulePath.getCanonicalPath());
                defs.get(pom).addSubProject(defs.get(module));
            }
        }
    } catch (IOException e) {
        throw new SonarException(e);
    }

    return defs.get(root);
}

From source file:org.sonar.batch.ProjectTree.java

License:Open Source License

public void start() throws IOException {
    projects = Lists.newArrayList();/*from w ww  . j ava2 s.  c o m*/
    Map<String, Project> paths = Maps.newHashMap(); // projects by canonical path

    for (MavenProject pom : poms) {
        Project project = projectBuilder.create(pom);
        projects.add(project);
        paths.put(pom.getBasedir().getCanonicalPath(), project);
    }

    for (Map.Entry<String, Project> entry : paths.entrySet()) {
        Project project = entry.getValue();
        MavenProject pom = project.getPom();
        for (Object moduleId : pom.getModules()) {
            File modulePath = new File(pom.getBasedir(), (String) moduleId);
            Project module = paths.get(modulePath.getCanonicalPath());
            if (module != null) {
                module.setParent(project);
            }
        }
    }

    configureProjects();
    applyModuleExclusions();
}

From source file:org.sonar.batch.scan.maven.MavenProjectConverter.java

License:Open Source License

public static ProjectDefinition convert(List<MavenProject> poms, MavenProject root) {
    // projects by canonical path to pom.xml
    Map<String, MavenProject> paths = Maps.newHashMap();
    Map<MavenProject, ProjectDefinition> defs = Maps.newHashMap();

    try {/*from  w w  w.j  av  a 2  s . co m*/
        for (MavenProject pom : poms) {
            paths.put(pom.getFile().getCanonicalPath(), pom);
            defs.put(pom, convert(pom));
        }

        for (Map.Entry<String, MavenProject> entry : paths.entrySet()) {
            MavenProject pom = entry.getValue();
            for (Object m : pom.getModules()) {
                String moduleId = (String) m;
                File modulePath = new File(pom.getBasedir(), moduleId);
                MavenProject module = findMavenProject(modulePath, paths);

                ProjectDefinition parentProject = defs.get(pom);
                if (parentProject == null) {
                    throw new IllegalStateException(UNABLE_TO_DETERMINE_PROJECT_STRUCTURE_EXCEPTION_MESSAGE);
                }
                ProjectDefinition subProject = defs.get(module);
                if (subProject == null) {
                    throw new IllegalStateException(UNABLE_TO_DETERMINE_PROJECT_STRUCTURE_EXCEPTION_MESSAGE);
                }
                parentProject.addSubProject(subProject);
            }
        }
    } catch (IOException e) {
        throw new SonarException(e);
    }

    ProjectDefinition rootProject = defs.get(root);
    if (rootProject == null) {
        throw new IllegalStateException(UNABLE_TO_DETERMINE_PROJECT_STRUCTURE_EXCEPTION_MESSAGE);
    }
    return rootProject;
}