o Refactored project builder to support better error reporting

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@803598 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-08-12 16:52:49 +00:00
parent d5c8697e97
commit d198f32117
17 changed files with 80 additions and 145 deletions

View File

@ -236,7 +236,7 @@ public class DefaultMaven
//
if ( request.getPom() == null || !request.getPom().exists() )
{
MavenProject project = projectBuilder.buildStandaloneSuperProject( request.getProjectBuildingRequest() );
MavenProject project = projectBuilder.buildStandaloneSuperProject( request.getProjectBuildingRequest() ).getProject();
projects.add( project );
request.setProjectPresent( false );
return projects;

View File

@ -51,7 +51,7 @@ public class DefaultMavenProjectBuilder
public MavenProject build( File pomFile, ProjectBuilderConfiguration configuration )
throws ProjectBuildingException
{
return projectBuilder.build( pomFile, configuration );
return projectBuilder.build( pomFile, configuration ).getProject();
}
public MavenProject buildFromRepository( Artifact artifact, ProjectBuilderConfiguration configuration )
@ -59,7 +59,7 @@ public class DefaultMavenProjectBuilder
{
normalizeToArtifactRepositories( configuration );
return projectBuilder.build( artifact, configuration );
return projectBuilder.build( artifact, configuration ).getProject();
}
private void normalizeToArtifactRepositories( ProjectBuilderConfiguration configuration )
@ -143,13 +143,7 @@ public class DefaultMavenProjectBuilder
public MavenProject buildStandaloneSuperProject( ProjectBuilderConfiguration config )
throws ProjectBuildingException
{
return projectBuilder.buildStandaloneSuperProject( config );
}
public MavenProjectBuildingResult buildProjectWithDependencies( File pomFile, ProjectBuilderConfiguration configuration )
throws ProjectBuildingException
{
return projectBuilder.buildProjectWithDependencies( pomFile, configuration );
return projectBuilder.buildStandaloneSuperProject( config ).getProject();
}
}

View File

@ -81,13 +81,14 @@ public class DefaultProjectBuilder
// MavenProjectBuilder Implementation
// ----------------------------------------------------------------------
public MavenProject build( File pomFile, ProjectBuildingRequest configuration )
public ProjectBuildingResult build( File pomFile, ProjectBuildingRequest configuration )
throws ProjectBuildingException
{
return build( pomFile, true, configuration );
return build( pomFile, true, configuration, false );
}
private MavenProject build( File pomFile, boolean localProject, ProjectBuildingRequest configuration )
private DefaultProjectBuildingResult build( File pomFile, boolean localProject,
ProjectBuildingRequest configuration, boolean resolveDependencies )
throws ProjectBuildingException
{
ModelBuildingRequest request = getModelBuildingRequest( configuration, null );
@ -141,7 +142,29 @@ public class DefaultProjectBuilder
MavenProject project = toProject( result, configuration, listener );
return project;
ArtifactResolutionResult artifactResult = null;
if ( resolveDependencies )
{
Artifact artifact = new ProjectArtifact( project );
ArtifactResolutionRequest artifactRequest = new ArtifactResolutionRequest()
.setArtifact( artifact )
.setResolveRoot( false )
.setResolveTransitively( true )
.setCache( configuration.getRepositoryCache() )
.setLocalRepository( configuration.getLocalRepository() )
.setRemoteRepositories( project.getRemoteArtifactRepositories() )
.setOffline( configuration.isOffline() )
.setManagedVersionMap( project.getManagedVersionMap() );
// FIXME setTransferListener
artifactResult = repositorySystem.resolve( artifactRequest );
project.setArtifacts( artifactResult.getArtifacts() );
}
return new DefaultProjectBuildingResult( project, result.getProblems(), artifactResult );
}
finally
{
@ -188,7 +211,7 @@ public class DefaultProjectBuilder
return request;
}
public MavenProject build( Artifact artifact, ProjectBuildingRequest configuration )
public ProjectBuildingResult build( Artifact artifact, ProjectBuildingRequest configuration )
throws ProjectBuildingException
{
if ( !artifact.getType().equals( "pom" ) )
@ -214,7 +237,7 @@ public class DefaultProjectBuilder
throw new ProjectBuildingException( artifact.getId(), "Error resolving project artifact.", e );
}
return build( artifact.getFile(), false, configuration );
return build( artifact.getFile(), false, configuration, false );
}
/**
@ -223,7 +246,7 @@ public class DefaultProjectBuilder
* I am taking out the profile handling and the interpolation of the base directory until we
* spec this out properly.
*/
public MavenProject buildStandaloneSuperProject( ProjectBuildingRequest config )
public ProjectBuildingResult buildStandaloneSuperProject( ProjectBuildingRequest config )
throws ProjectBuildingException
{
ModelBuildingRequest request = getModelBuildingRequest( config, null );
@ -252,55 +275,13 @@ public class DefaultProjectBuilder
standaloneProject.setExecutionRoot( true );
return standaloneProject;
return new DefaultProjectBuildingResult( standaloneProject, result.getProblems(), null );
}
public MavenProjectBuildingResult buildProjectWithDependencies( File pomFile, ProjectBuildingRequest request )
public ProjectBuildingResult buildProjectWithDependencies( File pomFile, ProjectBuildingRequest request )
throws ProjectBuildingException
{
MavenProject project = build( pomFile, request );
Artifact artifact = new ProjectArtifact( project );
ArtifactResolutionRequest artifactRequest = new ArtifactResolutionRequest()
.setArtifact( artifact )
.setResolveRoot( false )
.setResolveTransitively( true )
.setCache( request.getRepositoryCache() )
.setLocalRepository( request.getLocalRepository() )
.setRemoteRepositories( project.getRemoteArtifactRepositories() )
.setOffline( request.isOffline() )
.setManagedVersionMap( project.getManagedVersionMap() );
// FIXME setTransferListener
ArtifactResolutionResult result;
ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
try
{
if ( project.getClassRealm() != null )
{
Thread.currentThread().setContextClassLoader( project.getClassRealm() );
}
result = repositorySystem.resolve( artifactRequest );
}
finally
{
Thread.currentThread().setContextClassLoader( oldContextClassLoader );
}
if ( result.hasExceptions() )
{
Exception e = result.getExceptions().get( 0 );
throw new ProjectBuildingException( safeVersionlessKey( project.getGroupId(), project.getArtifactId() ), "Unable to build project due to an invalid dependency version: " + e.getMessage(),
pomFile, e );
}
project.setArtifacts( result.getArtifacts() );
return new MavenProjectBuildingResult( project, result );
return build( pomFile, true, request, true );
}
public List<ProjectBuildingResult> build( List<File> pomFiles, boolean recursive, ProjectBuildingRequest config )
@ -334,7 +315,7 @@ public class DefaultProjectBuilder
MavenProject project = toProject( result, config, interimResult.listener );
results.add( new DefaultProjectBuildingResult( project, result.getProblems() ) );
results.add( new DefaultProjectBuildingResult( project, result.getProblems(), null ) );
}
catch ( ModelBuildingException e )
{

View File

@ -23,6 +23,7 @@ import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.model.building.ModelProblem;
/**
@ -42,18 +43,24 @@ class DefaultProjectBuildingResult
private List<ModelProblem> problems;
private ArtifactResolutionResult artifactResolutionResult;
/**
* Creates a new result with the specified contents.
*
* @param project The project that was built, may be {@code null}.
* @param problems The problems that were encouterned, may be {@code null}.
* @param dependencyResolutionResult The result of the artifact resolution for the project dependencies, may be
* {@code null}.
*/
public DefaultProjectBuildingResult( MavenProject project, List<ModelProblem> problems )
public DefaultProjectBuildingResult( MavenProject project, List<ModelProblem> problems,
ArtifactResolutionResult dependencyResolutionResult )
{
this.projectId = ( project != null ) ? project.getId() : "";
this.pomFile = ( project != null ) ? project.getFile() : null;
this.project = project;
this.problems = problems;
this.artifactResolutionResult = dependencyResolutionResult;
}
/**
@ -95,4 +102,9 @@ class DefaultProjectBuildingResult
return problems;
}
public ArtifactResolutionResult getArtifactResolutionResult()
{
return artifactResolutionResult;
}
}

View File

@ -351,7 +351,7 @@ public class MavenProject
{
try
{
parent = mavenProjectBuilder.build( parentFile, projectBuilderConfiguration );
parent = mavenProjectBuilder.build( parentFile, projectBuilderConfiguration ).getProject();
}
catch ( ProjectBuildingException e )
{
@ -363,7 +363,7 @@ public class MavenProject
{
try
{
parent = mavenProjectBuilder.build( getParentArtifact(), projectBuilderConfiguration );
parent = mavenProjectBuilder.build( getParentArtifact(), projectBuilderConfiguration ).getProject();
}
catch ( ProjectBuildingException e )
{

View File

@ -41,11 +41,6 @@ public interface MavenProjectBuilder
MavenProject buildFromRepository( Artifact artifact, List<ArtifactRepository> remoteRepositories, ArtifactRepository localRepository, boolean force )
throws ProjectBuildingException;
// TODO: This also doesn't really belong here as it's a mix of project builder and artifact resolution and belongs
// in an integration component like the embedder.
MavenProjectBuildingResult buildProjectWithDependencies( File project, ProjectBuilderConfiguration configuration )
throws ProjectBuildingException;
// TODO: this is only to provide a project for plugins that don't need a project to execute but need some
// of the values from a MavenProject. Ideally this should be something internal and nothing outside Maven
// would ever need this so it should not be exposed in a public API

View File

@ -1,50 +0,0 @@
package org.apache.maven.project;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
/**
* @author Jason van Zyl
* @version $Id$
*/
public class MavenProjectBuildingResult
{
private MavenProject project;
private ArtifactResolutionResult artifactResolutionResult;
public MavenProjectBuildingResult( MavenProject project, ArtifactResolutionResult artifactResolutionResult )
{
this.project = project;
this.artifactResolutionResult = artifactResolutionResult;
}
public MavenProject getProject()
{
return project;
}
public ArtifactResolutionResult getArtifactResolutionResult()
{
return artifactResolutionResult;
}
}

View File

@ -23,22 +23,22 @@ import org.apache.maven.artifact.Artifact;
public interface ProjectBuilder
{
MavenProject build( File projectFile, ProjectBuildingRequest request )
ProjectBuildingResult build( File projectFile, ProjectBuildingRequest request )
throws ProjectBuildingException;
MavenProject build( Artifact projectArtifact, ProjectBuildingRequest request )
ProjectBuildingResult build( Artifact projectArtifact, ProjectBuildingRequest request )
throws ProjectBuildingException;
// TODO: this is only to provide a project for plugins that don't need a project to execute but need some
// of the values from a MavenProject. Ideally this should be something internal and nothing outside Maven
// would ever need this so it should not be exposed in a public API
MavenProject buildStandaloneSuperProject( ProjectBuildingRequest request )
ProjectBuildingResult buildStandaloneSuperProject( ProjectBuildingRequest request )
throws ProjectBuildingException;
// TODO: This also doesn't really belong here as it's a mix of project builder and artifact resolution and belongs
// in an integration component like the embedder.
@Deprecated
MavenProjectBuildingResult buildProjectWithDependencies( File project, ProjectBuildingRequest request )
ProjectBuildingResult buildProjectWithDependencies( File project, ProjectBuildingRequest request )
throws ProjectBuildingException;
/**

View File

@ -22,6 +22,7 @@ package org.apache.maven.project;
import java.io.File;
import java.util.List;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.model.building.ModelProblem;
/**
@ -51,7 +52,8 @@ public interface ProjectBuildingResult
/**
* Gets the project that was built.
*
* @return The project that was built or {@code null} if an error occurred.
* @return The project that was built or {@code null} if an error occurred and this result accompanies a
* {@link ProjectBuildingException}.
*/
MavenProject getProject();
@ -62,4 +64,12 @@ public interface ProjectBuildingResult
*/
List<ModelProblem> getProblems();
/**
* Gets the result of the dependency resolution for the project.
*
* @return The result of the dependency resolution for the project or {@code null} if the project dependencies were
* not requested.
*/
ArtifactResolutionResult getArtifactResolutionResult();
}

View File

@ -474,7 +474,7 @@ public class MavenMetadataSource
configuration.setProcessPlugins( false );
configuration.setSystemProperties( System.getProperties() );
project = getProjectBuilder().build( pomArtifact, configuration );
project = getProjectBuilder().build( pomArtifact, configuration ).getProject();
}
catch ( ProjectBuildingException e )
{

View File

@ -121,7 +121,7 @@ public abstract class AbstractCoreMavenComponentTestCase
if ( pom != null )
{
project = projectBuilder.build( pom, configuration );
project = projectBuilder.build( pom, configuration ).getProject();
}
else
{

View File

@ -158,7 +158,7 @@ public abstract class AbstractMavenProjectTestCase
ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
configuration.setLocalRepository( getLocalRepository() );
return projectBuilder.build( pom, configuration );
return projectBuilder.build( pom, configuration ).getProject();
}
}

View File

@ -1696,7 +1696,7 @@ public class PomConstructionTest
config.setValidationLevel( lenientValidation ? ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0
: ModelBuildingRequest.VALIDATION_LEVEL_STRICT );
return new PomTestWrapper( pomFile, projectBuilder.build( pomFile, config ) );
return new PomTestWrapper( pomFile, projectBuilder.build( pomFile, config ).getProject() );
}
protected void assertModelEquals( PomTestWrapper pom, Object expected, String expression )

View File

@ -17,7 +17,7 @@ public class TestProjectBuilder
{
@Override
public MavenProject build( Artifact artifact, ProjectBuildingRequest request )
public ProjectBuildingResult build( Artifact artifact, ProjectBuildingRequest request )
throws ProjectBuildingException
{
if ( "maven-test".equals( artifact.getGroupId() ) )
@ -37,20 +37,20 @@ public class TestProjectBuilder
{
MavenProject project = new MavenProject();
project.setArtifact( artifact );
return project;
return new DefaultProjectBuildingResult( project, null, null );
}
return build( artifact.getFile(), request );
}
@Override
public MavenProject build( File pomFile, ProjectBuildingRequest configuration )
public ProjectBuildingResult build( File pomFile, ProjectBuildingRequest configuration )
throws ProjectBuildingException
{
MavenProject project = super.build( pomFile, configuration );
ProjectBuildingResult result = super.build( pomFile, configuration );
project.setRemoteArtifactRepositories( Collections.<ArtifactRepository> emptyList() );
result.getProject().setRemoteArtifactRepositories( Collections.<ArtifactRepository> emptyList() );
return project;
return result;
}
}

View File

@ -90,7 +90,7 @@ public class PomConstructionWithSettingsTest
config.setLocalRepository( repositorySystem.createArtifactRepository( "local", localRepoUrl, new DefaultRepositoryLayout(), null, null ) );
config.setActiveProfileIds( settings.getActiveProfiles() );
return new PomTestWrapper( pomFile, projectBuilder.build( pomFile, config ) );
return new PomTestWrapper( pomFile, projectBuilder.build( pomFile, config ).getProject() );
}
private static Settings readSettingsFile(File settingsFile)

View File

@ -25,21 +25,14 @@ import java.io.Reader;
import java.io.Writer;
import org.apache.maven.Maven;
import org.apache.maven.MavenExecutionException;
import org.apache.maven.MissingModuleException;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.embedder.execution.MavenExecutionRequestPopulator;
import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.DefaultMavenExecutionResult;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.ModelReader;
import org.apache.maven.model.io.ModelWriter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuildingResult;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.SettingsConfigurationException;

View File

@ -127,7 +127,7 @@ public abstract class AbstractCoreMavenComponentTestCase
if ( pom != null )
{
project = projectBuilder.build( pom, configuration );
project = projectBuilder.build( pom, configuration ).getProject();
}
else
{