Example usage for org.apache.maven.project MavenProjectBuilder buildWithDependencies

List of usage examples for org.apache.maven.project MavenProjectBuilder buildWithDependencies

Introduction

In this page you can find the example usage for org.apache.maven.project MavenProjectBuilder buildWithDependencies.

Prototype

MavenProject buildWithDependencies(File pom, ArtifactRepository localRepository,
            ProfileManager globalProfileManager)
            throws ProjectBuildingException, ArtifactResolutionException, ArtifactNotFoundException;

Source Link

Usage

From source file:com.pongasoft.maven.ant.tasks.ResolveTask.java

License:Apache License

@SuppressWarnings("unchecked")
private void resolveTransitively(Artifact mainArtifact) {
    try {//from  w  w  w . j  a  v a 2  s. c o  m
        Artifact pomArtifact = resolveArtifact("pom", null);
        MavenProjectBuilder projectBuilder = (MavenProjectBuilder) lookup(MavenProjectBuilder.ROLE);
        MavenProject mavenProject = projectBuilder.buildWithDependencies(pomArtifact.getFile(), _localRepo,
                getProfileManager());

        Set<String> dependencies = new HashSet<String>();

        for (Dependency d : (List<Dependency>) mavenProject.getDependencies()) {
            dependencies.add(d.getGroupId() + ":" + d.getArtifactId() + ":" + d.getVersion() + ":"
                    + d.getClassifier() + ":" + d.getType() + ":" + d.getScope());
        }

        Collection<Artifact> directDependencies = new ArrayList<Artifact>();
        Collection<Artifact> transitiveDependencies = new ArrayList<Artifact>();
        Collection<Artifact> optionalDependencies = new ArrayList<Artifact>();
        Collection<Artifact> classpath = new ArrayList<Artifact>();
        classpath.add(mainArtifact);

        for (org.apache.maven.artifact.Artifact a : (Set<org.apache.maven.artifact.Artifact>) mavenProject
                .getArtifacts()) {
            String scope = a.getScope();
            if ("compile".equals(scope) || "runtime".equals(scope)) {
                Artifact artifact = createArtifact(a);

                if (a.isOptional() && _optional) {
                    classpath.add(artifact);
                    optionalDependencies.add(artifact);
                } else {
                    classpath.add(artifact);

                }
                if (dependencies.contains(a.getGroupId() + ":" + a.getArtifactId() + ":" + a.getVersion() + ":"
                        + a.getClassifier() + ":" + a.getType() + ":" + a.getScope())) {
                    directDependencies.add(artifact);
                } else {
                    transitiveDependencies.add(artifact);
                }
            }
        }

        _result.put("classpath", classpath);
        _result.put("transitiveDependencies", transitiveDependencies);
        _result.put("directDependencies", directDependencies);
        _result.put("optionalDependencies", optionalDependencies);
    } catch (ProjectBuildingException e) {
        throw new BuildException(e);
    } catch (ArtifactNotFoundException e) {
        throw new BuildException(e);
    } catch (ArtifactResolutionException e) {
        throw new BuildException(e);
    }
}

From source file:org.fornax.toolsupport.sculptor.maven.plugin.AbstractSculptorMojoTestCase.java

License:Apache License

/**
 * Returns a {@link MavenProject} created from the test projects in
 * <code>"src/test/projects/"</code> by given project name.
 *//*from  w w w .j  av  a 2  s  .co m*/
protected MavenProject createProject(String name) throws Exception {

    // Copy test project
    File srcProject = new File(getBasedir(), TEST_PROJECT_FOLDER + name);
    File targetProject = new File(getBasedir(), "target/test-projects/" + name);

    FileUtils.deleteDirectory(targetProject);
    FileUtils.copyDirectoryStructure(srcProject, targetProject);

    // Create Maven project from POM
    File pom = new File(targetProject, "pom.xml");
    MavenProjectBuilder builder = (MavenProjectBuilder) lookup(MavenProjectBuilder.ROLE);
    return builder.buildWithDependencies(pom, null, null);
}

From source file:org.universAAL.support.directives.checks.DependencyManagementCheckFix.java

License:Apache License

public static List<MavenProject> getChildrenModules(MavenProject mavenProject, MavenProjectBuilder mpb,
        ArtifactRepository localRepository, ProfileManager pm) {
    List<MavenProject> children = new ArrayList<MavenProject>();
    List<String> modules = mavenProject.getModules();
    for (String mod : modules) {
        try {//w  w w.j av  a 2 s  .co  m
            children.add(mpb.buildWithDependencies(new File(mavenProject.getBasedir(), mod + "/pom.xml"),
                    localRepository, pm));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return children;
}