diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java index fb89ddec60..15f48f2997 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java @@ -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 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 build( List 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 ); diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingRequest.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingRequest.java index 9e7f614feb..dce4548880 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingRequest.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingRequest.java @@ -46,7 +46,7 @@ public class DefaultProjectBuildingRequest private List 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(); } - 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 ) { diff --git a/maven-core/src/main/java/org/apache/maven/project/ProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/ProjectBuilder.java index 2100134307..770855a1fb 100644 --- a/maven-core/src/main/java/org/apache/maven/project/ProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/ProjectBuilder.java @@ -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. * diff --git a/maven-core/src/main/java/org/apache/maven/project/ProjectBuilderConfiguration.java b/maven-core/src/main/java/org/apache/maven/project/ProjectBuilderConfiguration.java index babb6a7897..f48bc320f5 100644 --- a/maven-core/src/main/java/org/apache/maven/project/ProjectBuilderConfiguration.java +++ b/maven-core/src/main/java/org/apache/maven/project/ProjectBuilderConfiguration.java @@ -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 ); diff --git a/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingRequest.java b/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingRequest.java index d6cd0dee68..3f6986c7de 100644 --- a/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingRequest.java +++ b/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingRequest.java @@ -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. * diff --git a/maven-core/src/main/java/org/apache/maven/project/artifact/DefaultMavenMetadataCache.java b/maven-core/src/main/java/org/apache/maven/project/artifact/DefaultMavenMetadataCache.java index 2750ab4045..95bbc61579 100644 --- a/maven-core/src/main/java/org/apache/maven/project/artifact/DefaultMavenMetadataCache.java +++ b/maven-core/src/main/java/org/apache/maven/project/artifact/DefaultMavenMetadataCache.java @@ -204,7 +204,7 @@ public class DefaultMavenMetadataCache public ResolutionGroup get( Artifact artifact, boolean resolveManagedVersions, ArtifactRepository localRepository, List 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 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 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() ); diff --git a/maven-core/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java b/maven-core/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java index c964efb1ac..44e9c9cfda 100644 --- a/maven-core/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java +++ b/maven-core/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java @@ -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 ) {