Artifact artifact = request.getArtifact();
String version = artifact.getVersion();
VersionResult result = new VersionResult( request );
Key cacheKey = null;
RepositoryCache cache = session.getCache();
if ( cache != null && !ConfigUtils.getBoolean( session, false, "aether.versionResolver.noCache" ) )
{
cacheKey = new Key( session, request );
Object obj = cache.get( session, cacheKey );
if ( obj instanceof Record )
{
Record record = (Record) obj;
result.setVersion( record.version );
result.setRepository( CacheUtils.getRepository( session, request.getRepositories(), record.repoClass,
record.repoId ) );
return result;
}
}
Metadata metadata;
if ( RELEASE.equals( version ) )
{
metadata =
new DefaultMetadata( artifact.getGroupId(), artifact.getArtifactId(), MAVEN_METADATA_XML,
Metadata.Nature.RELEASE );
}
else if ( LATEST.equals( version ) )
{
metadata =
new DefaultMetadata( artifact.getGroupId(), artifact.getArtifactId(), MAVEN_METADATA_XML,
Metadata.Nature.RELEASE_OR_SNAPSHOT );
}
else if ( version.endsWith( SNAPSHOT ) )
{
WorkspaceReader workspace = session.getWorkspaceReader();
if ( workspace != null && workspace.findVersions( artifact ).contains( version ) )
{
metadata = null;
result.setRepository( workspace.getRepository() );
}
else
{
metadata =
new DefaultMetadata( artifact.getGroupId(), artifact.getArtifactId(), version, MAVEN_METADATA_XML,
Metadata.Nature.SNAPSHOT );
}
}
else
{
metadata = null;
}
if ( metadata == null )
{
result.setVersion( version );
}
else
{
List<MetadataRequest> metadataReqs = new ArrayList<MetadataRequest>( request.getRepositories().size() );
metadataReqs.add( new MetadataRequest( metadata, null, request.getRequestContext() ) );
for ( RemoteRepository repository : request.getRepositories() )
{
MetadataRequest metadataRequest =
new MetadataRequest( metadata, repository, request.getRequestContext() );
metadataRequest.setDeleteLocalCopyIfMissing( true );
metadataRequest.setFavorLocalRepository( true );
metadataRequest.setTrace( trace );
metadataReqs.add( metadataRequest );
}
List<MetadataResult> metadataResults = metadataResolver.resolveMetadata( session, metadataReqs );
Map<String, VersionInfo> infos = new HashMap<String, VersionInfo>();
for ( MetadataResult metadataResult : metadataResults )
{
result.addException( metadataResult.getException() );
ArtifactRepository repository = metadataResult.getRequest().getRepository();
if ( repository == null )
{
repository = session.getLocalRepository();
}
Versioning v = readVersions( session, trace, metadataResult.getMetadata(), repository, result );
merge( artifact, infos, v, repository );
}
if ( RELEASE.equals( version ) )
{
resolve( result, infos, RELEASE );
}
else if ( LATEST.equals( version ) )
{
if ( !resolve( result, infos, LATEST ) )
{
resolve( result, infos, RELEASE );
}
if ( result.getVersion() != null && result.getVersion().endsWith( SNAPSHOT ) )
{
VersionRequest subRequest = new VersionRequest();
subRequest.setArtifact( artifact.setVersion( result.getVersion() ) );
if ( result.getRepository() instanceof RemoteRepository )
{
RemoteRepository r = (RemoteRepository) result.getRepository();
subRequest.setRepositories( Collections.singletonList( r ) );
}
else
{
subRequest.setRepositories( request.getRepositories() );
}
VersionResult subResult = resolveVersion( session, subRequest );
result.setVersion( subResult.getVersion() );
result.setRepository( subResult.getRepository() );
for ( Exception exception : subResult.getExceptions() )
{
result.addException( exception );
}
}
}