decouple project stuff from maven-core

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@164205 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-04-22 09:01:47 +00:00
parent a803fc26b4
commit 9774d3d1cb
3 changed files with 58 additions and 55 deletions

View File

@ -17,8 +17,8 @@ package org.apache.maven.project;
*/ */
import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.MavenMetadataSource;
import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
@ -109,20 +109,40 @@ public class DefaultMavenProjectBuilder
// MavenProjectBuilder Implementation // MavenProjectBuilder Implementation
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
public MavenProject buildWithDependencies( File projectDescriptor, ArtifactRepository localRepository ) public MavenProject buildWithDependencies( File projectDescriptor, ArtifactRepository localRepository,
throws ProjectBuildingException ArtifactMetadataSource artifactMetadataSource )
throws ProjectBuildingException, ArtifactResolutionException
{ {
return buildFromSourceFile( projectDescriptor, localRepository, true ); MavenProject project = buildFromSourceFile( projectDescriptor, localRepository );
// ----------------------------------------------------------------------
// Typically when the project builder is being used from maven proper
// the transitive dependencies will not be resolved here because this
// requires a lot of work when we may only be interested in running
// something simple like 'm2 clean'. So the artifact collector is used
// in the dependency resolution phase if it is required by any of the
// goals being executed. But when used as a component in another piece
// of code people may just want to build maven projects and have the
// dependencies resolved for whatever reason: this is why we keep
// this snippet of code here.
// ----------------------------------------------------------------------
ArtifactResolutionResult result = artifactResolver.resolveTransitively( project.getArtifacts(),
project.getRemoteArtifactRepositories(),
localRepository,
artifactMetadataSource );
project.addArtifacts( result.getArtifacts().values(), artifactFactory );
return project;
} }
public MavenProject build( File projectDescriptor, ArtifactRepository localRepository ) public MavenProject build( File projectDescriptor, ArtifactRepository localRepository )
throws ProjectBuildingException throws ProjectBuildingException
{ {
return buildFromSourceFile( projectDescriptor, localRepository, false ); return buildFromSourceFile( projectDescriptor, localRepository );
} }
private MavenProject buildFromSourceFile( File projectDescriptor, ArtifactRepository localRepository, private MavenProject buildFromSourceFile( File projectDescriptor, ArtifactRepository localRepository )
boolean resolveDependencies )
throws ProjectBuildingException throws ProjectBuildingException
{ {
Model model = readModel( projectDescriptor ); Model model = readModel( projectDescriptor );
@ -130,7 +150,7 @@ public class DefaultMavenProjectBuilder
// Always cache files in the source tree over those in the repository // Always cache files in the source tree over those in the repository
modelCache.put( createCacheKey( model.getGroupId(), model.getArtifactId(), model.getVersion() ), model ); modelCache.put( createCacheKey( model.getGroupId(), model.getArtifactId(), model.getVersion() ), model );
MavenProject project = build( model, localRepository, resolveDependencies ); MavenProject project = build( model, localRepository );
// Only translate the base directory for files in the source tree // Only translate the base directory for files in the source tree
pathTranslator.alignToBaseDirectory( project.getModel(), projectDescriptor ); pathTranslator.alignToBaseDirectory( project.getModel(), projectDescriptor );
@ -153,7 +173,7 @@ public class DefaultMavenProjectBuilder
Model model = findModelFromRepository( artifact, remoteArtifactRepositories, localRepository ); Model model = findModelFromRepository( artifact, remoteArtifactRepositories, localRepository );
return build( model, localRepository, false ); return build( model, localRepository );
} }
private Model findModelFromRepository( Artifact artifact, List remoteArtifactRepositories, private Model findModelFromRepository( Artifact artifact, List remoteArtifactRepositories,
@ -176,7 +196,7 @@ public class DefaultMavenProjectBuilder
return model; return model;
} }
private MavenProject build( Model model, ArtifactRepository localRepository, boolean resolveDependencies ) private MavenProject build( Model model, ArtifactRepository localRepository )
throws ProjectBuildingException throws ProjectBuildingException
{ {
Model superModel = getSuperModel(); Model superModel = getSuperModel();
@ -200,18 +220,12 @@ public class DefaultMavenProjectBuilder
try try
{ {
project = processProjectLogic( project, localRepository, aggregatedRemoteWagonRepositories, project = processProjectLogic( project, aggregatedRemoteWagonRepositories );
resolveDependencies );
} }
catch ( ModelInterpolationException e ) catch ( ModelInterpolationException e )
{ {
throw new ProjectBuildingException( "Error building project: " + model.getId(), e ); throw new ProjectBuildingException( "Error building project: " + model.getId(), e );
} }
catch ( ArtifactResolutionException e )
{
throw new ProjectBuildingException( "Error building project: " + model.getId(), e );
}
return project; return project;
} }
@ -222,9 +236,8 @@ public class DefaultMavenProjectBuilder
* the resolved source roots, etc for the parent - that occurs for the parent when it is constructed independently * the resolved source roots, etc for the parent - that occurs for the parent when it is constructed independently
* and projects are not cached or reused * and projects are not cached or reused
*/ */
private MavenProject processProjectLogic( MavenProject project, ArtifactRepository localRepository, private MavenProject processProjectLogic( MavenProject project, List remoteRepositories )
List remoteRepositories, boolean resolveDependencies ) throws ProjectBuildingException, ModelInterpolationException
throws ProjectBuildingException, ModelInterpolationException, ArtifactResolutionException
{ {
Model model = project.getModel(); Model model = project.getModel();
String key = createCacheKey( model.getGroupId(), model.getArtifactId(), model.getVersion() ); String key = createCacheKey( model.getGroupId(), model.getArtifactId(), model.getVersion() );
@ -255,30 +268,6 @@ public class DefaultMavenProjectBuilder
project.setRemoteArtifactRepositories( remoteRepositories ); project.setRemoteArtifactRepositories( remoteRepositories );
project.setArtifacts( createArtifacts( project.getDependencies() ) ); project.setArtifacts( createArtifacts( project.getDependencies() ) );
// ----------------------------------------------------------------------
// Typically when the project builder is being used from maven proper
// the transitive dependencies will not be resolved here because this
// requires a lot of work when we may only be interested in running
// something simple like 'm2 clean'. So the artifact collector is used
// in the dependency resolution phase if it is required by any of the
// goals being executed. But when used as a component in another piece
// of code people may just want to build maven projects and have the
// dependencies resolved for whatever reason: this is why we keep
// this snippet of code here.
// ----------------------------------------------------------------------
if ( resolveDependencies )
{
// TODO: comes from Maven CORE
MavenMetadataSource sourceReader = new MavenMetadataSource( artifactResolver, this );
ArtifactResolutionResult result = artifactResolver.resolveTransitively( project.getArtifacts(),
remoteRepositories,
localRepository, sourceReader );
project.addArtifacts( result.getArtifacts().values(), artifactFactory );
}
ModelValidationResult validationResult = validator.validate( model ); ModelValidationResult validationResult = validator.validate( model );
if ( validationResult.getMessageCount() > 0 ) if ( validationResult.getMessageCount() > 0 )
@ -495,7 +484,7 @@ public class DefaultMavenProjectBuilder
List remoteRepositories = buildArtifactRepositories( superModel.getRepositories() ); List remoteRepositories = buildArtifactRepositories( superModel.getRepositories() );
project = processProjectLogic( project, localRepository, remoteRepositories, false ); project = processProjectLogic( project, remoteRepositories );
return project; return project;
} }
@ -503,10 +492,15 @@ public class DefaultMavenProjectBuilder
{ {
throw new ProjectBuildingException( "Error building super-project", e ); throw new ProjectBuildingException( "Error building super-project", e );
} }
catch ( ArtifactResolutionException e ) }
{
throw new ProjectBuildingException( "Error building super-project", e ); /**
} * @return
* @todo remove
*/
public ArtifactResolver getArtifactResolver()
{
return artifactResolver;
} }
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------

View File

@ -18,6 +18,10 @@ package org.apache.maven.project;
*/ */
import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.MavenMetadataSource;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.settings.Settings; import org.apache.maven.settings.Settings;
@ -37,8 +41,9 @@ public interface MavenProjectBuilder
MavenProject build( File project, ArtifactRepository localRepository ) MavenProject build( File project, ArtifactRepository localRepository )
throws ProjectBuildingException; throws ProjectBuildingException;
MavenProject buildWithDependencies( File project, ArtifactRepository localRepository ) MavenProject buildWithDependencies( File project, ArtifactRepository localRepository,
throws ProjectBuildingException; ArtifactMetadataSource artifactMetadataSource )
throws ProjectBuildingException, ArtifactResolutionException;
/** /**
* Build the artifact from the local repository, resolving it if necessary. * Build the artifact from the local repository, resolving it if necessary.
@ -55,4 +60,6 @@ public interface MavenProjectBuilder
MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository ) MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository )
throws ProjectBuildingException; throws ProjectBuildingException;
ArtifactResolver getArtifactResolver();
} }

View File

@ -16,12 +16,12 @@ package org.apache.maven;
* limitations under the License. * limitations under the License.
*/ */
import org.apache.maven.artifact.MavenMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.plugin.PluginManager; import org.apache.maven.plugin.PluginManager;
import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder; import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.settings.MavenSettingsBuilder;
import org.codehaus.plexus.ArtifactEnabledPlexusTestCase; import org.codehaus.plexus.ArtifactEnabledPlexusTestCase;
import java.io.File; import java.io.File;
@ -54,7 +54,7 @@ public abstract class MavenTestCase
protected File getLocalRepositoryPath() protected File getLocalRepositoryPath()
{ {
File markerFile = getFileForClasspathResource( "local-repo/marker.txt" ); File markerFile = getFileForClasspathResource( "local-repo/marker.txt" );
return markerFile.getAbsoluteFile().getParentFile(); return markerFile.getAbsoluteFile().getParentFile();
} }
@ -93,7 +93,9 @@ public abstract class MavenTestCase
protected MavenProject getProjectWithDependencies( File pom ) protected MavenProject getProjectWithDependencies( File pom )
throws Exception throws Exception
{ {
return projectBuilder.buildWithDependencies( pom, getLocalRepository() ); return projectBuilder.buildWithDependencies( pom, getLocalRepository(),
new MavenMetadataSource( projectBuilder.getArtifactResolver(),
projectBuilder ) );
} }
protected MavenProject getProject( File pom ) protected MavenProject getProject( File pom )