MNG-3905: merged all the artifact/repository handling bits into MavenTools

o clean up hand-written component descriptors



git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@726584 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jason van Zyl 2008-12-15 00:20:04 +00:00
parent 6581960c98
commit 3a689d4f4d
7 changed files with 189 additions and 348 deletions

View File

@ -22,34 +22,57 @@ package org.apache.maven;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.InvalidRepositoryException;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.model.DeploymentRepository;
import org.apache.maven.model.Model;
import org.apache.maven.model.Repository;
import org.apache.maven.model.RepositoryPolicy;
import org.apache.maven.project.MissingRepositoryElementException;
import org.apache.maven.project.ProjectBuildingException;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.StringUtils;
/**
* @author Jason van Zyl
*/
@Component(role = MavenTools.class)
public class DefaultMavenTools
implements MavenTools
implements MavenTools, LogEnabled
{
@Requirement
private ArtifactFactory artifactFactory;
@Requirement
private ArtifactResolver artifactResolver;
@Requirement
private ArtifactRepositoryFactory artifactRepositoryFactory;
@Requirement
private ArtifactRepositoryLayout defaultArtifactRepositoryLayout;
@Requirement
private Logger logger;
private static HashMap<String, Artifact> cache = new HashMap<String, Artifact>();
// ----------------------------------------------------------------------------
// Code snagged from ProjectUtils: this will have to be moved somewhere else
// but just trying to collect it all in one place right now.
@ -220,4 +243,135 @@ public class DefaultMavenTools
{
artifactRepositoryFactory.setGlobalChecksumPolicy( policy );
}
// Taken from RepositoryHelper
public void findModelFromRepository( Artifact artifact, List remoteArtifactRepositories, ArtifactRepository localRepository )
throws ProjectBuildingException
{
if ( cache.containsKey( artifact.getId() ) )
{
artifact.setFile( cache.get( artifact.getId() ).getFile() );
}
String projectId = safeVersionlessKey( artifact.getGroupId(), artifact.getArtifactId() );
remoteArtifactRepositories = normalizeToArtifactRepositories( remoteArtifactRepositories, projectId );
Artifact projectArtifact;
// if the artifact is not a POM, we need to construct a POM artifact based on the artifact parameter given.
if ( "pom".equals( artifact.getType() ) )
{
projectArtifact = artifact;
}
else
{
logger.debug( "Attempting to build MavenProject instance for Artifact (" + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() + ") of type: "
+ artifact.getType() + "; constructing POM artifact instead." );
projectArtifact = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getScope() );
}
try
{
artifactResolver.resolve( projectArtifact, remoteArtifactRepositories, localRepository );
File file = projectArtifact.getFile();
artifact.setFile( file );
cache.put( artifact.getId(), artifact );
}
catch ( ArtifactResolutionException e )
{
throw new ProjectBuildingException( projectId, "Error getting POM for '" + projectId + "' from the repository: " + e.getMessage(), e );
}
catch ( ArtifactNotFoundException e )
{
throw new ProjectBuildingException( projectId, "POM '" + projectId + "' not found in repository: " + e.getMessage(), e );
}
}
public List buildArtifactRepositories( Model model )
throws ProjectBuildingException
{
try
{
return buildArtifactRepositories( model.getRepositories() );
}
catch ( InvalidRepositoryException e )
{
String projectId = safeVersionlessKey( model.getGroupId(), model.getArtifactId() );
throw new ProjectBuildingException( projectId, e.getMessage(), e );
}
}
private List normalizeToArtifactRepositories( List remoteArtifactRepositories, String projectId )
throws ProjectBuildingException
{
List normalized = new ArrayList( remoteArtifactRepositories.size() );
boolean normalizationNeeded = false;
for ( Iterator it = remoteArtifactRepositories.iterator(); it.hasNext(); )
{
Object item = it.next();
if ( item instanceof ArtifactRepository )
{
normalized.add( item );
}
else if ( item instanceof Repository )
{
Repository repo = (Repository) item;
try
{
item = buildArtifactRepository( repo );
normalized.add( item );
normalizationNeeded = true;
}
catch ( InvalidRepositoryException e )
{
throw new ProjectBuildingException( projectId, "Error building artifact repository for id: " + repo.getId(), e );
}
}
else
{
throw new ProjectBuildingException( projectId, "Error building artifact repository from non-repository information item: " + item );
}
}
if ( normalizationNeeded )
{
return normalized;
}
else
{
return remoteArtifactRepositories;
}
}
private 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 );
}
public void enableLogging( Logger logger )
{
this.logger = logger;
}
}

View File

@ -19,12 +19,15 @@ package org.apache.maven;
* under the License.
*/
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.InvalidRepositoryException;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.model.DeploymentRepository;
import org.apache.maven.model.Model;
import org.apache.maven.model.Repository;
import org.apache.maven.project.ProjectBuildingException;
import java.io.IOException;
import java.util.List;
@ -57,4 +60,13 @@ public interface MavenTools
void setGlobalUpdatePolicy( String policy );
void setGlobalChecksumPolicy( String policy );
// Taken from RepositoryHelper
void findModelFromRepository( Artifact artifact, List remoteArtifactRepositories, ArtifactRepository localRepository )
throws ProjectBuildingException;
List buildArtifactRepositories( Model model )
throws ProjectBuildingException;
}

View File

@ -19,8 +19,20 @@ package org.apache.maven.project;
* under the License.
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.maven.MavenTools;
import org.apache.maven.shared.model.InterpolatorProperty;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.InvalidRepositoryException;
@ -41,9 +53,13 @@ import org.apache.maven.profiles.activation.ProfileActivationContext;
import org.apache.maven.profiles.activation.ProfileActivationException;
import org.apache.maven.profiles.build.ProfileAdvisor;
import org.apache.maven.project.artifact.InvalidDependencyVersionException;
import org.apache.maven.project.builder.*;
import org.apache.maven.project.builder.Interpolator;
import org.apache.maven.project.builder.PomArtifactResolver;
import org.apache.maven.project.builder.PomInterpolatorTag;
import org.apache.maven.project.builder.ProjectBuilder;
import org.apache.maven.project.validation.ModelValidationResult;
import org.apache.maven.project.validation.ModelValidator;
import org.apache.maven.shared.model.InterpolatorProperty;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.LogEnabled;
@ -54,15 +70,6 @@ import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.net.URL;
import java.util.*;
import java.text.SimpleDateFormat;
/**
* @version $Id$
@ -86,9 +93,6 @@ public class DefaultMavenProjectBuilder
@Requirement
private ProjectBuilder projectBuilder;
@Requirement
private RepositoryHelper repositoryHelper;
private MavenXpp3Reader modelReader;
private Logger logger;
@ -135,7 +139,7 @@ public class DefaultMavenProjectBuilder
public MavenProject build( File projectDescriptor, ProjectBuilderConfiguration config )
throws ProjectBuildingException
{
MavenProject project = readModelFromLocalPath( "unknown", projectDescriptor, new PomArtifactResolver( config.getLocalRepository(), repositoryHelper
MavenProject project = readModelFromLocalPath( "unknown", projectDescriptor, new PomArtifactResolver( config.getLocalRepository(), mavenTools
.buildArtifactRepositories( getSuperProject( config, projectDescriptor, true ).getModel() ), artifactResolver ), config );
project.setFile( projectDescriptor );
@ -189,12 +193,12 @@ public class DefaultMavenProjectBuilder
}
File f = (artifact.getFile() != null) ? artifact.getFile() : new File( localRepository.getBasedir(), localRepository.pathOf( artifact ) );
repositoryHelper.findModelFromRepository( artifact, remoteArtifactRepositories, localRepository );
mavenTools.findModelFromRepository( artifact, remoteArtifactRepositories, localRepository );
ProjectBuilderConfiguration config = new DefaultProjectBuilderConfiguration().setLocalRepository( localRepository );
List<ArtifactRepository> artifactRepositories = new ArrayList<ArtifactRepository>( remoteArtifactRepositories );
artifactRepositories.addAll( repositoryHelper.buildArtifactRepositories( getSuperProject( config, artifact.getFile(), false ).getModel() ) );
artifactRepositories.addAll( mavenTools.buildArtifactRepositories( getSuperProject( config, artifact.getFile(), false ).getModel() ) );
project = readModelFromLocalPath( "unknown", artifact.getFile(), new PomArtifactResolver( config.getLocalRepository(), artifactRepositories, artifactResolver ), config );
project = buildWithProfiles( project.getModel(), config, artifact.getFile(), project.getParentFile(), false );

View File

@ -1,226 +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 java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.apache.maven.MavenTools;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.InvalidRepositoryException;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.model.Model;
import org.apache.maven.model.Repository;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.profiles.build.ProfileAdvisor;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.codehaus.plexus.util.StringUtils;
/**
* This is a temporary class. These methods are originally from the DefaultMavenProjectHelper. This class will be
* eliminated when Mercury is integrated.
*/
@Component(role = RepositoryHelper.class)
public class DefaultRepositoryHelper
implements RepositoryHelper, Initializable, LogEnabled
{
@Requirement
private ArtifactFactory artifactFactory;
@Requirement
private ArtifactResolver artifactResolver;
@Requirement
private MavenTools mavenTools;
@Requirement
private ProfileAdvisor profileAdvisor;
private Logger logger;
public static final String MAVEN_MODEL_VERSION = "4.0.0";
private MavenXpp3Reader modelReader;
private static HashMap<String, Artifact> cache = new HashMap<String, Artifact>();
private Logger getLogger()
{
return logger;
}
public void findModelFromRepository( Artifact artifact, List remoteArtifactRepositories,
ArtifactRepository localRepository )
throws ProjectBuildingException
{
if(cache.containsKey(artifact.getId()))
{
artifact.setFile(cache.get(artifact.getId()).getFile());
}
String projectId = safeVersionlessKey( artifact.getGroupId(), artifact.getArtifactId() );
remoteArtifactRepositories = normalizeToArtifactRepositories( remoteArtifactRepositories, projectId );
Artifact projectArtifact;
// if the artifact is not a POM, we need to construct a POM artifact based on the artifact parameter given.
if ( "pom".equals( artifact.getType() ) )
{
projectArtifact = artifact;
}
else
{
getLogger().debug( "Attempting to build MavenProject instance for Artifact (" + artifact.getGroupId() + ":" +
artifact.getArtifactId() + ":" + artifact.getVersion() + ") of type: " + artifact.getType() +
"; constructing POM artifact instead." );
projectArtifact = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
artifact.getVersion(), artifact.getScope() );
}
try
{
artifactResolver.resolve( projectArtifact, remoteArtifactRepositories, localRepository );
File file = projectArtifact.getFile();
artifact.setFile( file );
cache.put(artifact.getId(), artifact);
}
catch ( ArtifactResolutionException e )
{
throw new ProjectBuildingException( projectId, "Error getting POM for '" + projectId +
"' from the repository: " + e.getMessage(), e );
}
catch ( ArtifactNotFoundException e )
{
throw new ProjectBuildingException( projectId,
"POM '" + projectId + "' not found in repository: " + e.getMessage(),
e );
}
}
public List buildArtifactRepositories( Model model )
throws ProjectBuildingException
{
try
{
return mavenTools.buildArtifactRepositories( model.getRepositories() );
}
catch ( InvalidRepositoryException e )
{
String projectId = safeVersionlessKey( model.getGroupId(), model.getArtifactId() );
throw new ProjectBuildingException( projectId, e.getMessage(), e );
}
}
private List normalizeToArtifactRepositories( List remoteArtifactRepositories, String projectId )
throws ProjectBuildingException
{
List normalized = new ArrayList( remoteArtifactRepositories.size() );
boolean normalizationNeeded = false;
for ( Iterator it = remoteArtifactRepositories.iterator(); it.hasNext(); )
{
Object item = it.next();
if ( item instanceof ArtifactRepository )
{
normalized.add( item );
}
else if ( item instanceof Repository )
{
Repository repo = (Repository) item;
try
{
item = mavenTools.buildArtifactRepository( repo );
normalized.add( item );
normalizationNeeded = true;
}
catch ( InvalidRepositoryException e )
{
throw new ProjectBuildingException( projectId,
"Error building artifact repository for id: " + repo.getId(),
e );
}
}
else
{
throw new ProjectBuildingException( projectId,
"Error building artifact repository from non-repository information item: " +
item );
}
}
if ( normalizationNeeded )
{
return normalized;
}
else
{
return remoteArtifactRepositories;
}
}
private 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 );
}
public void initialize()
throws InitializationException
{
modelReader = new MavenXpp3Reader();
}
public void enableLogging( Logger logger )
{
this.logger = logger;
}
}

View File

@ -1,23 +0,0 @@
package org.apache.maven.project;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.model.Model;
import org.apache.maven.profiles.activation.ProfileActivationContext;
import java.io.File;
import java.util.LinkedHashSet;
import java.util.List;
/**
* This is a temporary class. These methods are originally from the DefaultMavenProjectHelper. This
* class will be eliminated when Mercury is integrated.
*/
public interface RepositoryHelper
{
void findModelFromRepository( Artifact artifact, List remoteArtifactRepositories, ArtifactRepository localRepository )
throws ProjectBuildingException;
List buildArtifactRepositories( Model model )
throws ProjectBuildingException;
}

View File

@ -19,41 +19,6 @@ under the License.
<plexus>
<components>
<!--Eliminate this component after Mercury Integration-->
<component>
<role>org.apache.maven.project.RepositoryHelper</role>
<implementation>org.apache.maven.project.DefaultRepositoryHelper</implementation>
<requirements>
<requirement>
<role>org.apache.maven.profiles.build.ProfileAdvisor</role>
<role-hint>default</role-hint>
</requirement>
<requirement>
<role>org.apache.maven.artifact.factory.ArtifactFactory</role>
</requirement>
<requirement>
<role>org.apache.maven.artifact.resolver.ArtifactResolver</role>
<role-hint>default</role-hint>
</requirement>
<requirement>
<role>org.apache.maven.MavenTools</role>
</requirement>
</requirements>
</component>
<!-- Due to PLX-108, have to work around it...
<component>
<role>org.apache.maven.artifact.ArtifactResolver</role>
<implementation>org.apache.maven.project.TestArtifactResolvertion>
<requirements>
<requirement>
<role>org.apache.maven.artifact.manager.WagonManager</role>
</requirement>
<requirement>
<role>org.apache.maven.artifact.factory.ArtifactFactory</role>
</requirement>
</requirements>
</component>
-->
<component>
<role>org.apache.maven.profiles.injection.ProfileInjector</role>
<role-hint>test</role-hint>
@ -80,11 +45,6 @@ under the License.
<role>org.apache.maven.project.TestArtifactResolver</role>
<implementation>org.apache.maven.project.TestArtifactResolver</implementation>
<requirements>
<!--
<requirement>
<role>org.apache.maven.artifact.manager.ArtifactManager</role>
</requirement>
-->
<requirement>
<role>org.apache.maven.artifact.factory.ArtifactFactory</role>
</requirement>
@ -114,9 +74,6 @@ under the License.
<role-hint>test</role-hint>
<implementation>org.apache.maven.project.TestProjectBuilder</implementation>
<requirements>
<requirement>
<role>org.apache.maven.project.RepositoryHelper</role>
</requirement>
<requirement>
<role>org.apache.maven.project.builder.ProjectBuilder</role>
</requirement>

View File

@ -19,40 +19,6 @@ under the License.
<plexus>
<components><!--Eliminate this component after Mercury Integration-->
<component>
<role>org.apache.maven.project.RepositoryHelper</role>
<implementation>org.apache.maven.project.DefaultRepositoryHelper</implementation>
<requirements>
<requirement>
<role>org.apache.maven.profiles.build.ProfileAdvisor</role>
</requirement>
<requirement>
<role>org.apache.maven.artifact.factory.ArtifactFactory</role>
</requirement>
<requirement>
<role>org.apache.maven.artifact.resolver.ArtifactResolver</role>
<role-hint>default</role-hint>
</requirement>
<requirement>
<role>org.apache.maven.MavenTools</role>
</requirement>
</requirements>
</component>
<!-- Due to PLX-108, have to work around it...
<component>
<role>org.apache.maven.artifact.ArtifactResolver</role>
<implementation>org.apache.maven.project.TestArtifactResolvertion>
<requirements>
<requirement>
<role>org.apache.maven.artifact.manager.WagonManager</role>
</requirement>
<requirement>
<role>org.apache.maven.artifact.factory.ArtifactFactory</role>
</requirement>
</requirements>
</component>
-->
<component>
<role>org.apache.maven.profiles.injection.ProfileInjector</role>
<role-hint>test</role-hint>
@ -111,9 +77,6 @@ under the License.
<role-hint>test</role-hint>
<implementation>org.apache.maven.project.TestProjectBuilder</implementation>
<requirements>
<requirement>
<role>org.apache.maven.project.RepositoryHelper</role>
</requirement>
<requirement>
<role>org.apache.maven.project.builder.ProjectBuilder</role>
</requirement>