implement cycle detection

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@191819 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-06-22 12:27:28 +00:00
parent 920c3d3060
commit 37007c1891
4 changed files with 55 additions and 29 deletions

View File

@ -54,6 +54,19 @@ public class ArtifactResolutionException
this.downloadUrl = downloadUrl;
}
public ArtifactResolutionException( String message, String groupId, String artifactId, String version, String type,
List remoteRepositories, String downloadUrl )
{
super( constructMessage( message, groupId, artifactId, version, type, remoteRepositories, downloadUrl ) );
this.groupId = groupId;
this.artifactId = artifactId;
this.type = type;
this.version = version;
this.remoteRepositories = remoteRepositories;
this.downloadUrl = downloadUrl;
}
private static final String LS = System.getProperty( "line.separator" );
private static String constructMessage( String message, String groupId, String artifactId, String version,
@ -65,18 +78,21 @@ public class ArtifactResolutionException
sb.append( LS );
sb.append( " " + groupId + ":" + artifactId + ":" + version + ":" + type );
sb.append( LS );
sb.append( LS );
sb.append( "from the specified remote repositories:" );
sb.append( LS + " " );
for ( Iterator i = remoteRepositories.iterator(); i.hasNext(); )
if ( remoteRepositories != null && !remoteRepositories.isEmpty() )
{
ArtifactRepository remoteRepository = (ArtifactRepository) i.next();
sb.append( LS );
sb.append( "from the specified remote repositories:" );
sb.append( LS + " " );
sb.append( remoteRepository.getUrl() );
if ( i.hasNext() )
for ( Iterator i = remoteRepositories.iterator(); i.hasNext(); )
{
sb.append( ", " );
ArtifactRepository remoteRepository = (ArtifactRepository) i.next();
sb.append( remoteRepository.getUrl() );
if ( i.hasNext() )
{
sb.append( ", " );
}
}
}
@ -110,6 +126,12 @@ public class ArtifactResolutionException
remoteRepositories, artifact.getDownloadUrl(), t );
}
public ArtifactResolutionException( String message, Artifact artifact )
{
this( message, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType(), null,
artifact.getDownloadUrl() );
}
public ArtifactResolutionException( String message, Throwable cause )
{
super( message, cause );

View File

@ -18,30 +18,17 @@ package org.apache.maven.artifact.resolver;
import org.apache.maven.artifact.Artifact;
import java.util.List;
/**
* Indiciates a cycle in the dependency graph.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
* @version $Id$
*/
public class CyclicDependencyException
public class CyclicDependencyException
extends ArtifactResolutionException
{
public CyclicDependencyException( String message, String groupId, String artifactId, String version, String type,
List remoteRepositories, String downloadUrl, Throwable t )
public CyclicDependencyException( String message, Artifact artifact )
{
super( message, groupId, artifactId, version, type, remoteRepositories, downloadUrl, t );
}
public CyclicDependencyException( String message, Artifact artifact, List remoteRepositories, Throwable t )
{
super( message, artifact, remoteRepositories, t );
}
public CyclicDependencyException( String message, Throwable cause )
{
super( message, cause );
super( message, artifact );
}
}

View File

@ -204,6 +204,8 @@ public class DefaultArtifactCollector
private List children = null;
private final Set parents;
private final int depth;
public ResolutionNode( Artifact artifact )
@ -211,6 +213,7 @@ public class DefaultArtifactCollector
this.artifact = artifact;
this.parent = null;
this.depth = 0;
this.parents = Collections.EMPTY_SET;
}
public ResolutionNode( Artifact artifact, ResolutionNode parent )
@ -218,6 +221,9 @@ public class DefaultArtifactCollector
this.artifact = artifact;
this.parent = parent;
this.depth = parent.depth + 1;
this.parents = new HashSet();
this.parents.add( parent.getKey() );
this.parents.addAll( parent.parents );
}
public Artifact getArtifact()
@ -231,6 +237,7 @@ public class DefaultArtifactCollector
}
public void addDependencies( Set artifacts, ArtifactFilter filter )
throws CyclicDependencyException
{
children = new ArrayList( artifacts.size() );
@ -240,6 +247,11 @@ public class DefaultArtifactCollector
if ( filter == null || filter.include( a ) )
{
if ( parents.contains( a.getDependencyConflictId() ) )
{
throw new CyclicDependencyException( "The dependency is present in a cycle", a );
}
children.add( new ResolutionNode( a, this ) );
}
}

View File

@ -65,12 +65,12 @@ public class DefaultArtifactCollectorTest
this.projectArtifact = createArtifact( "project", "1.0", null );
}
public void disabledtestCircularDependencyNotIncludingCurrentProject()
public void testCircularDependencyNotIncludingCurrentProject()
throws ArtifactResolutionException
{
ArtifactSpec a = createArtifact( "a", "1.0" );
ArtifactSpec b = a.addDependency( "b", "1.0" );
b.addDependency( "a", "1.0" );
b.addDependency( a );
try
{
collect( a );
@ -82,7 +82,7 @@ public class DefaultArtifactCollectorTest
}
}
public void disabledtestCircularDependencyIncludingCurrentProject()
public void testCircularDependencyIncludingCurrentProject()
throws ArtifactResolutionException
{
ArtifactSpec a = createArtifact( "a", "1.0" );
@ -321,9 +321,14 @@ public class DefaultArtifactCollectorTest
public ArtifactSpec addDependency( String id, String version, String scope )
{
ArtifactSpec dep = createArtifact( id, version, scope );
dependencies.add( dep.artifact );
addDependency( dep );
return dep;
}
public void addDependency( ArtifactSpec dep )
{
dependencies.add( dep.artifact );
}
}
private class Source