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

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

Introduction

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

Prototype

public void addLifecyclePhase(String lifecyclePhase) 

Source Link

Document

Warning: This is an internal utility method that is only public for technical reasons, it is not part of the public API.

Usage

From source file:org.sourcepit.common.maven.testing.EmbeddedMaven.java

License:Apache License

public MavenExecutionResult2 buildProject(File pom, Properties userProperties, boolean resolveDependencies)
        throws Exception {
    final MavenExecutionRequest request = newMavenExecutionRequest(pom, newSystemProperties(), userProperties,
            "compile");
    request.getProjectBuildingRequest().setProcessPlugins(false);
    request.getProjectBuildingRequest().setResolveDependencies(resolveDependencies);

    final MavenSession[] session = new MavenSession[1];
    request.setExecutionListener(new ChainedExecutionListener(request.getExecutionListener()) {
        @Override/*  w w  w  . j  a  v a 2  s. c  o  m*/
        public void sessionStarted(ExecutionEvent event) {
            super.sessionStarted(event);
            session[0] = event.getSession();
            throw new IllegalStateException();
        }
    });

    final MavenExecutionResult2 tmpResult = execute(request);
    if (session[0] == null) {
        if (tmpResult.hasExceptions()) {
            throw new IllegalStateException(tmpResult.getExceptions().get(0));
        }
    }

    final MavenExecutionResult2 result = new MavenExecutionResult2Impl(session[0], session[0].getResult());
    if (request.getProjectBuildingRequest().isResolveDependencies()) {
        Set<Artifact> projectArtifacts = new HashSet<Artifact>();

        for (MavenProject mavenProject : result.getTopologicallySortedProjects()) {
            File artifactFile = MavenProjectUtils.getOutputDir(mavenProject);
            if (artifactFile == null) {
                artifactFile = mavenProject.getBasedir();
            }
            mavenProject.getArtifact().setFile(artifactFile);
            mavenProject.getArtifact().setResolved(true);

            projectArtifacts.add(mavenProject.getArtifact());

            mavenProject.addLifecyclePhase("clean");
            mavenProject.addLifecyclePhase("process-resources");
            mavenProject.addLifecyclePhase("compile");

            ArrayList<String> scopesToCollect = new ArrayList<String>();
            Collections.addAll(scopesToCollect, "system", "compile", "provided", "runtime", "test");

            try {
                resolver.resolveProjectDependencies(mavenProject, scopesToCollect, scopesToCollect,
                        result.getSession(), true, Collections.<Artifact>emptySet());
            } catch (LifecycleExecutionException e) {
                result.addException(e);
            }

            mavenProject.setArtifactFilter(new CumulativeScopeArtifactFilter(scopesToCollect));
        }
    }
    if (result.hasExceptions()) {
        throw new IllegalStateException(result.getExceptions().get(0));
    }

    return result;
}