[MNG-4298] Build MavenProject instances incrementally

Submitted by: Igor Fedorenko

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@803961 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-08-13 17:52:27 +00:00
parent 52d99a7ba9
commit b3611a4fdd
7 changed files with 110 additions and 107 deletions

View File

@ -23,7 +23,6 @@ import java.util.List;
import org.apache.maven.Maven;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.repository.DefaultRepositoryRequest;
import org.apache.maven.artifact.repository.RepositoryRequest;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
@ -84,67 +83,94 @@ public class DefaultProjectBuilder
public ProjectBuildingResult build( File pomFile, ProjectBuildingRequest configuration )
throws ProjectBuildingException
{
return build( pomFile, true, configuration, false );
return build( pomFile, true, configuration );
}
private ProjectBuildingResult build( File pomFile, boolean localProject, ProjectBuildingRequest configuration,
boolean resolveDependencies )
private ProjectBuildingResult build( File pomFile, boolean localProject, ProjectBuildingRequest configuration )
throws ProjectBuildingException
{
ModelBuildingRequest request = getModelBuildingRequest( configuration, null );
DefaultModelBuildingListener listener = new DefaultModelBuildingListener( projectBuildingHelper, configuration );
request.setModelBuildingListeners( Arrays.asList( listener ) );
if ( localProject )
{
request.setPomFile( pomFile );
}
else
{
request.setModelSource( new FileModelSource( pomFile ) );
}
ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
try
{
ModelBuildingResult result;
try
{
result = modelBuilder.build( request );
}
catch ( ModelBuildingException e )
{
throw new ProjectBuildingException( e.getModelId(), "Encountered POM errors", pomFile, e );
}
MavenProject project = configuration.getProject();
Model model = result.getEffectiveModel();
List<ModelProblem> modelProblems = null;
if ( localProject && !result.getProblems().isEmpty() && logger.isWarnEnabled() )
if ( project == null )
{
logger.warn( "" );
logger.warn( "Some problems were encountered while building the effective model for " + model.getId() );
for ( ModelProblem problem : result.getProblems() )
ModelBuildingRequest request = getModelBuildingRequest( configuration, null );
DefaultModelBuildingListener listener = new DefaultModelBuildingListener( projectBuildingHelper, configuration );
request.setModelBuildingListeners( Arrays.asList( listener ) );
if ( localProject )
{
logger.warn( problem.getMessage() );
request.setPomFile( pomFile );
}
else
{
request.setModelSource( new FileModelSource( pomFile ) );
}
ModelBuildingResult result;
try
{
result = modelBuilder.build( request );
}
catch ( ModelBuildingException e )
{
throw new ProjectBuildingException( e.getModelId(), "Encountered POM errors", pomFile, e );
}
logger.warn( "" );
logger.warn( "It is highly recommended to fix these problems"
+ " because they threaten the stability of your build." );
logger.warn( "" );
logger.warn( "For this reason, future Maven versions will no"
+ " longer support building such malformed projects." );
logger.warn( "" );
modelProblems = result.getProblems();
Model model = result.getEffectiveModel();
if ( localProject && !result.getProblems().isEmpty() && logger.isWarnEnabled() )
{
logger.warn( "" );
logger.warn( "Some problems were encountered while building the effective model for " + model.getId() );
for ( ModelProblem problem : result.getProblems() )
{
logger.warn( problem.getMessage() );
}
logger.warn( "" );
logger.warn( "It is highly recommended to fix these problems"
+ " because they threaten the stability of your build." );
logger.warn( "" );
logger.warn( "For this reason, future Maven versions will no"
+ " longer support building such malformed projects." );
logger.warn( "" );
}
project = toProject( result, configuration, listener );
}
MavenProject project = toProject( result, configuration, listener );
try
{
if ( configuration.isProcessPlugins() && configuration.isProcessPluginConfiguration() )
{
RepositoryRequest repositoryRequest = new DefaultRepositoryRequest();
repositoryRequest.setLocalRepository( configuration.getLocalRepository() );
repositoryRequest.setRemoteRepositories( project.getPluginArtifactRepositories() );
repositoryRequest.setCache( configuration.getRepositoryCache() );
repositoryRequest.setOffline( configuration.isOffline() );
lifecycle.populateDefaultConfigurationForPlugins( project.getModel().getBuild().getPlugins(), repositoryRequest );
}
}
catch ( LifecycleExecutionException e )
{
throw new ProjectBuildingException( project.getId(), e.getMessage(), e );
}
ArtifactResolutionResult artifactResult = null;
if ( resolveDependencies )
if ( configuration.isResolveDependencies() )
{
Artifact artifact = new ProjectArtifact( project );
@ -164,7 +190,7 @@ public class DefaultProjectBuilder
project.setArtifacts( artifactResult.getArtifacts() );
}
return new DefaultProjectBuildingResult( project, result.getProblems(), artifactResult );
return new DefaultProjectBuildingResult( project, modelProblems, artifactResult );
}
finally
{
@ -237,7 +263,7 @@ public class DefaultProjectBuilder
throw new ProjectBuildingException( artifact.getId(), "Error resolving project artifact.", e );
}
return build( artifact.getFile(), false, configuration, false );
return build( artifact.getFile(), false, configuration );
}
/**
@ -278,12 +304,6 @@ public class DefaultProjectBuilder
return new DefaultProjectBuildingResult( standaloneProject, result.getProblems(), null );
}
public ProjectBuildingResult buildProjectWithDependencies( File pomFile, ProjectBuildingRequest request )
throws ProjectBuildingException
{
return build( pomFile, true, request, true );
}
public List<ProjectBuildingResult> build( List<File> pomFiles, boolean recursive, ProjectBuildingRequest config )
throws ProjectBuildingException
{
@ -480,24 +500,6 @@ public class DefaultProjectBuilder
project.setClassRealm( listener.getProjectRealm() );
try
{
if ( configuration.isProcessPlugins() && configuration.isProcessPluginConfiguration() )
{
RepositoryRequest repositoryRequest = new DefaultRepositoryRequest();
repositoryRequest.setLocalRepository( configuration.getLocalRepository() );
repositoryRequest.setRemoteRepositories( project.getPluginArtifactRepositories() );
repositoryRequest.setCache( configuration.getRepositoryCache() );
repositoryRequest.setOffline( configuration.isOffline() );
lifecycle.populateDefaultConfigurationForPlugins( model.getBuild().getPlugins(), repositoryRequest );
}
}
catch ( LifecycleExecutionException e )
{
throw new ProjectBuildingException( project.getId(), e.getMessage(), e );
}
Build build = project.getBuild();
project.addScriptSourceRoot( build.getScriptSourceDirectory() );
project.addCompileSourceRoot( build.getSourceDirectory() );
@ -517,25 +519,6 @@ public class DefaultProjectBuilder
return project;
}
private static String safeVersionlessKey( String groupId, String artifactId )
{
String gid = groupId;
if ( StringUtils.isEmpty( gid ) )
{
gid = "unknown";
}
String aid = artifactId;
if ( StringUtils.isEmpty( aid ) )
{
aid = "unknown";
}
return ArtifactUtils.versionlessKey( gid, aid );
}
private String toSourceHint( Model model )
{
StringBuilder buffer = new StringBuilder( 192 );

View File

@ -46,7 +46,7 @@ public class DefaultProjectBuildingRequest
private List<ModelEventListener> listeners;
private MavenProject topProject;
private MavenProject project;
private int validationLevel = ModelBuildingRequest.VALIDATION_LEVEL_STRICT;
@ -66,6 +66,8 @@ public class DefaultProjectBuildingRequest
private Date buildStartTime;
private boolean resolveDependencies;
public DefaultProjectBuildingRequest()
{
processPlugins = true;
@ -79,14 +81,14 @@ public class DefaultProjectBuildingRequest
pluginArtifactRepositories = new ArrayList<ArtifactRepository>();
}
public MavenProject getTopLevelProjectFromReactor()
public MavenProject getProject()
{
return topProject;
return project;
}
public void setTopLevelProjectForReactor( MavenProject mavenProject )
public void setProject( MavenProject mavenProject )
{
this.topProject = mavenProject;
this.project = mavenProject;
}
public DefaultProjectBuildingRequest setOffline( boolean offline )
@ -234,6 +236,17 @@ public class DefaultProjectBuildingRequest
this.processPluginConfiguration = processPluginConfiguration;
return this;
}
public ProjectBuildingRequest setResolveDependencies( boolean resolveDependencies )
{
this.resolveDependencies = resolveDependencies;
return this;
}
public boolean isResolveDependencies()
{
return resolveDependencies;
}
public ProjectBuildingRequest setValidationLevel( int validationLevel )
{

View File

@ -35,12 +35,6 @@ public interface ProjectBuilder
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
ProjectBuildingResult buildProjectWithDependencies( File project, ProjectBuildingRequest request )
throws ProjectBuildingException;
/**
* Builds the projects for the specified POM files and optionally their children.
*

View File

@ -41,9 +41,9 @@ public interface ProjectBuilderConfiguration
Properties getSystemProperties();
void setTopLevelProjectForReactor(MavenProject mavenProject);
void setProject(MavenProject mavenProject);
MavenProject getTopLevelProjectFromReactor();
MavenProject getProject();
ProjectBuilderConfiguration setProcessPlugins( boolean processPlugins );

View File

@ -23,12 +23,10 @@ import java.util.Date;
import java.util.List;
import java.util.Properties;
import org.apache.maven.MavenTransferListener;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.RepositoryCache;
import org.apache.maven.model.Profile;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.wagon.events.TransferListener;
public interface ProjectBuildingRequest
{
@ -89,9 +87,9 @@ public interface ProjectBuildingRequest
*/
Properties getUserProperties();
void setTopLevelProjectForReactor(MavenProject mavenProject);
void setProject(MavenProject mavenProject);
MavenProject getTopLevelProjectFromReactor();
MavenProject getProject();
ProjectBuildingRequest setProcessPlugins( boolean processPlugins );
@ -101,6 +99,10 @@ public interface ProjectBuildingRequest
boolean isProcessPluginConfiguration();
ProjectBuildingRequest setResolveDependencies( boolean resolveDependencies );
boolean isResolveDependencies();
/**
* Controls the level of validation to perform on processed models. By default, models are validated in strict mode.
*

View File

@ -204,7 +204,7 @@ public class DefaultMavenMetadataCache
public ResolutionGroup get( Artifact artifact, boolean resolveManagedVersions, ArtifactRepository localRepository,
List<ArtifactRepository> remoteRepositories )
{
CacheKey cacheKey = new CacheKey( artifact, resolveManagedVersions, localRepository, remoteRepositories );
CacheKey cacheKey = newCacheKey( artifact, resolveManagedVersions, localRepository, remoteRepositories );
CacheRecord cacheRecord = cache.get( cacheKey );
@ -231,7 +231,17 @@ public class DefaultMavenMetadataCache
public void put( Artifact artifact, boolean resolveManagedVersions, ArtifactRepository localRepository,
List<ArtifactRepository> remoteRepositories, ResolutionGroup result )
{
CacheKey cacheKey = new CacheKey( artifact, resolveManagedVersions, localRepository, remoteRepositories );
put( newCacheKey( artifact, resolveManagedVersions, localRepository, remoteRepositories ), result );
}
protected CacheKey newCacheKey( Artifact artifact, boolean resolveManagedVersions,
ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories )
{
return new CacheKey( artifact, resolveManagedVersions, localRepository, remoteRepositories );
}
protected void put( CacheKey cacheKey, ResolutionGroup result )
{
CacheRecord cacheRecord =
new CacheRecord( result.getPomArtifact(), result.getRelocatedArtifact(), result.getArtifacts(),
result.getManagedVersions(), result.getResolutionRepositories() );

View File

@ -129,10 +129,11 @@ public abstract class AbstractMavenProjectTestCase
configuration.setLocalRepository( getLocalRepository() );
configuration.setRemoteRepositories( Arrays.asList( new ArtifactRepository[] {} ) );
configuration.setProcessPlugins( false );
configuration.setResolveDependencies( true );
try
{
return projectBuilder.buildProjectWithDependencies( pom, configuration ).getProject();
return projectBuilder.build( pom, configuration ).getProject();
}
catch ( Exception e )
{