o Added back some code to actually throw exceptions and abort the normal control flow in case artifact resolution fails

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@750029 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-03-04 14:51:24 +00:00
parent 1971bae2f0
commit 96c06c4f1b
6 changed files with 169 additions and 23 deletions

View File

@ -44,7 +44,7 @@ public class ArtifactResolutionResult
// Exceptions
private List<Exception> exceptions = new ArrayList<Exception>();
private List<Exception> exceptions;
private List<Exception> versionRangeViolations;
@ -104,6 +104,11 @@ public class ArtifactResolutionResult
return requestedArtifacts;
}
public boolean hasMissingArtifacts()
{
return missingArtifacts != null && !missingArtifacts.isEmpty();
}
public List<Artifact> getMissingArtifacts()
{
return missingArtifacts == null ? Collections.<Artifact> emptyList() : missingArtifacts;
@ -131,12 +136,12 @@ public class ArtifactResolutionResult
public boolean hasExceptions()
{
return exceptions.size() > 0;
return exceptions != null && !exceptions.isEmpty();
}
public List<Exception> getExceptions()
{
return exceptions;
return exceptions == null ? Collections.<Exception> emptyList() : exceptions;
}
// ------------------------------------------------------------------------
@ -159,6 +164,8 @@ public class ArtifactResolutionResult
versionRangeViolations.add( e );
exceptions = initList( exceptions );
exceptions.add( e );
return this;
@ -169,9 +176,9 @@ public class ArtifactResolutionResult
return (OverConstrainedVersionException) versionRangeViolations.get( i );
}
public List getVersionRangeViolations()
public List<Exception> getVersionRangeViolations()
{
return versionRangeViolations == null ? Collections.EMPTY_LIST : versionRangeViolations;
return versionRangeViolations == null ? Collections.<Exception> emptyList() : versionRangeViolations;
}
// ------------------------------------------------------------------------
@ -189,6 +196,8 @@ public class ArtifactResolutionResult
metadataResolutionExceptions.add( e );
exceptions = initList( exceptions );
exceptions.add( e );
return this;
@ -199,9 +208,10 @@ public class ArtifactResolutionResult
return metadataResolutionExceptions.get( i );
}
public List getMetadataResolutionExceptions()
public List<ArtifactResolutionException> getMetadataResolutionExceptions()
{
return metadataResolutionExceptions == null ? Collections.EMPTY_LIST : metadataResolutionExceptions;
return metadataResolutionExceptions == null ? Collections.<ArtifactResolutionException> emptyList()
: metadataResolutionExceptions;
}
// ------------------------------------------------------------------------
@ -219,6 +229,8 @@ public class ArtifactResolutionResult
errorArtifactExceptions.add( e );
exceptions = initList( exceptions );
exceptions.add( e );
return this;
@ -249,6 +261,8 @@ public class ArtifactResolutionResult
circularDependencyExceptions.add( e );
exceptions = initList( exceptions );
exceptions.add( e );
return this;

View File

@ -0,0 +1,88 @@
package org.apache.maven.artifact.resolver;
/*
* 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.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.codehaus.plexus.component.annotations.Component;
/**
* @author Benjamin Bentmann
*/
@Component( role = ResolutionErrorHandler.class )
public class DefaultResolutionErrorHandler
implements ResolutionErrorHandler
{
public void throwErrors( ArtifactResolutionRequest request, ArtifactResolutionResult result )
throws ArtifactResolutionException
{
// Metadata cannot be found
if ( result.hasMetadataResolutionExceptions() )
{
throw result.getMetadataResolutionException( 0 );
}
// Metadata cannot be retrieved
// Cyclic Dependency Error
if ( result.hasCircularDependencyExceptions() )
{
throw result.getCircularDependencyException( 0 );
}
// Version Range Violation
if ( result.hasVersionRangeViolations() )
{
throw result.getVersionRangeViolation( 0 );
}
// Transfer Error
if ( result.hasErrorArtifactExceptions() )
{
throw result.getErrorArtifactExceptions().get( 0 );
}
if ( result.hasMissingArtifacts() )
{
throw new MultipleArtifactsNotFoundException( request.getArtifact(), toList( result.getArtifacts() ),
result.getMissingArtifacts(), request.getRemoteRepostories() );
}
// this should never happen since we checked all possible error sources before but better be sure
if ( result.hasExceptions() )
{
throw new ArtifactResolutionException( "Unknown error during artifact resolution, " + request + ", "
+ result.getExceptions(), request.getArtifact(), request.getRemoteRepostories() );
}
}
private static <T> List<T> toList( Collection<T> items )
{
return ( items != null ) ? new ArrayList<T>( items ) : null;
}
}

View File

@ -0,0 +1,31 @@
package org.apache.maven.artifact.resolver;
/*
* 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.
*/
/**
* @author Benjamin Bentmann
*/
public interface ResolutionErrorHandler
{
public void throwErrors( ArtifactResolutionRequest request, ArtifactResolutionResult result )
throws ArtifactResolutionException;
}

View File

@ -43,6 +43,7 @@ import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.resolver.ResolutionErrorHandler;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
@ -139,6 +140,9 @@ public class DefaultPluginManager
@Requirement
protected MavenRepositorySystem repositorySystem;
@Requirement
private ResolutionErrorHandler resolutionErrorHandler;
@Requirement
protected RuntimeInformation runtimeInformation;
@ -445,11 +449,7 @@ public class DefaultPluginManager
.setMetadataSource( repositorySystem );
ArtifactResolutionResult result = repositorySystem.resolve( request );
if ( result.hasExceptions() )
{
result.getExceptions().get( 0 ).printStackTrace();
}
resolutionErrorHandler.throwErrors( request, result );
Set<Artifact> resolved = new LinkedHashSet<Artifact>();
@ -1494,7 +1494,7 @@ public class DefaultPluginManager
ArtifactResolutionResult result = repositorySystem.resolve( request );
if ( result.hasErrorArtifactExceptions() )
if ( result.hasMissingArtifacts() )
{
/*
@ -1503,15 +1503,14 @@ public class DefaultPluginManager
all we can do is warn and skip it. A better fix can be inserted into 2.1
*/
if ( isAggregator && checkMissingArtifactsInReactor( context.getSortedProjects(), result.getMissingArtifacts() ) )
if ( isAggregator
&& checkMissingArtifactsInReactor( context.getSortedProjects(), result.getMissingArtifacts() ) )
{
}
else
{
//we can't find all the artifacts in the reactor so bubble the exception up.
throw result.getErrorArtifactExceptions().get( 0 );
// all found, so clear up the result state to prevent the error handler from blowing up
result.setUnresolvedArtifacts( null );
}
}
resolutionErrorHandler.throwErrors( request, result );
project.setArtifacts( result.getArtifacts() );
}

View File

@ -27,6 +27,8 @@ 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.ArtifactResolutionRequest;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.resolver.ResolutionErrorHandler;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
@ -40,7 +42,6 @@ import org.apache.maven.plugin.version.PluginVersionResolutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.artifact.InvalidDependencyVersionException;
import org.apache.maven.realm.RealmManagementException;
import org.apache.maven.realm.RealmScanningUtils;
import org.apache.maven.repository.MavenRepositorySystem;
@ -59,6 +60,9 @@ public class DefaultPluginManagerSupport
{
@Requirement
private MavenRepositorySystem repositorySystem;
@Requirement
private ResolutionErrorHandler resolutionErrorHandler;
@Requirement
private MavenProjectBuilder mavenProjectBuilder;
@ -112,7 +116,10 @@ public class DefaultPluginManagerSupport
pluginArtifact = project.replaceWithActiveArtifact( pluginArtifact );
repositorySystem.resolve( new ArtifactResolutionRequest( pluginArtifact, localRepository, remoteRepositories ) );
ArtifactResolutionRequest request =
new ArtifactResolutionRequest( pluginArtifact, localRepository, remoteRepositories );
ArtifactResolutionResult result = repositorySystem.resolve( request );
resolutionErrorHandler.throwErrors( request, result );
return pluginArtifact;
}

View File

@ -41,6 +41,7 @@ import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.artifact.resolver.ResolutionErrorHandler;
import org.apache.maven.artifact.resolver.filter.AndArtifactFilter;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
@ -84,7 +85,10 @@ public class LegacyMavenRepositorySystem
@Requirement
private MirrorBuilder mirrorBuilder;
@Requirement
private ResolutionErrorHandler resolutionErrorHandler;
@Requirement
private Logger logger;
@ -464,7 +468,10 @@ public class LegacyMavenRepositorySystem
projectArtifact = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getScope() );
}
resolve( new ArtifactResolutionRequest( projectArtifact, localRepository, remoteArtifactRepositories ) );
ArtifactResolutionRequest request =
new ArtifactResolutionRequest( projectArtifact, localRepository, remoteArtifactRepositories );
ArtifactResolutionResult result = resolve( request );
resolutionErrorHandler.throwErrors( request, result );
File file = projectArtifact.getFile();
artifact.setFile( file );