Skip to content

Commit

Permalink
mojohaus#704: Removing ArtifactMetadataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejj0 committed Nov 23, 2022
1 parent acc5dbd commit 49ed727
Show file tree
Hide file tree
Showing 59 changed files with 906 additions and 602 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
Expand Down Expand Up @@ -90,6 +89,9 @@
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.resolution.VersionRangeRequest;
import org.eclipse.aether.resolution.VersionRangeResolutionException;

/**
* Helper class that provides common functionality required by both the mojos and the reports.
Expand All @@ -115,13 +117,6 @@ public class DefaultVersionsHelper
*/
private RuleSet ruleSet;

/**
* The artifact metadata source to use.
*
* @since 1.0-alpha-3
*/
private ArtifactMetadataSource artifactMetadataSource;

/**
* The local repository to consult.
*
Expand All @@ -145,6 +140,10 @@ public class DefaultVersionsHelper

private RepositorySystem repositorySystem;

private org.eclipse.aether.RepositorySystem aetherRepositorySystem;

private RepositorySystemSession repositorySystemSession;

/**
* The {@link Log} to send log messages to.
*
Expand Down Expand Up @@ -388,61 +387,64 @@ public Log getLog()

@Override
public ArtifactVersions lookupArtifactVersions( Artifact artifact, boolean usePluginRepositories )
throws ArtifactMetadataRetrievalException
throws VersionRetrievalException
{
List<ArtifactRepository> remoteRepositories = usePluginRepositories
? remotePluginRepositories : remoteArtifactRepositories;
final List<ArtifactVersion> versions =
artifactMetadataSource.retrieveAvailableVersions( artifact, localRepository, remoteRepositories );
final List<IgnoreVersion> ignoredVersions = getIgnoredVersions( artifact );
if ( !ignoredVersions.isEmpty() )
try
{
if ( getLog().isDebugEnabled() )
Collection<IgnoreVersion> ignoredVersions = getIgnoredVersions( artifact );
if ( !ignoredVersions.isEmpty() && getLog().isDebugEnabled() )
{
getLog().debug( "Found ignored versions: " + showIgnoredVersions( ignoredVersions ) );
}

final Iterator<ArtifactVersion> i = versions.iterator();
while ( i.hasNext() )
{
final String version = i.next().toString();
for ( final IgnoreVersion ignoreVersion : ignoredVersions )
{
if ( TYPE_REGEX.equals( ignoreVersion.getType() ) )
{
Pattern p = Pattern.compile( ignoreVersion.getVersion() );
if ( p.matcher( version ).matches() )
{
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Version " + version + " for artifact "
+ ArtifactUtils.versionlessKey( artifact )
+ " found on ignore list: "
+ ignoreVersion );
}
i.remove();
break;
}
}
else if ( TYPE_EXACT.equals( ignoreVersion.getType() ) )
{
if ( version.equals( ignoreVersion.getVersion() ) )
{
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Version " + version + " for artifact "
+ ArtifactUtils.versionlessKey( artifact )
+ " found on ignore list: "
+ ignoreVersion );
}
i.remove();
break;
}
}
}
getLog().debug( "Found ignored versions: " + ignoredVersions.stream()
.map( IgnoreVersion::toString ).collect( Collectors.joining( ", " ) ) );
}
return new ArtifactVersions( artifact,
aetherRepositorySystem.resolveVersionRange( repositorySystemSession, new VersionRangeRequest(
RepositoryUtils.toArtifact( artifact ).setVersion( "(,)" ),
RepositoryUtils.toRepos( remoteRepositories ),
"lookupArtifactVersions" ) )
.getVersions()
.parallelStream()
.filter( v -> ignoredVersions.stream()
.noneMatch( i ->
{
if ( TYPE_REGEX.equals( i.getType() )
&& Pattern.compile( i.getVersion() ).matcher( v.toString() ).matches() )
{
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Version " + v + " for artifact "
+ ArtifactUtils.versionlessKey( artifact )
+ " found on ignore list: "
+ i );
}
return true;
}

if ( TYPE_EXACT.equals( i.getType() )
&& i.getVersion().equals( v.toString() ) )
{
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Version " + v + " for artifact "
+ ArtifactUtils.versionlessKey( artifact )
+ " found on ignore list: "
+ i );
}
return true;
}

return false;
} ) )
.map( v -> new DefaultArtifactVersion( v.toString() ) )
.collect( Collectors.toList() ),
getVersionComparator( artifact ) );
}
catch ( VersionRangeResolutionException e )
{
throw new VersionRetrievalException( e.getMessage(), e );
}
return new ArtifactVersions( artifact, versions, getVersionComparator( artifact ) );
}

/**
Expand Down Expand Up @@ -491,28 +493,6 @@ private List<IgnoreVersion> getIgnoredVersions( Artifact artifact )
return ret;
}

/**
* Pretty print a list of ignored versions.
*
* @param ignoredVersions A list of ignored versions
* @return A String representation of the list
*/
private String showIgnoredVersions( List<IgnoreVersion> ignoredVersions )
{
StringBuilder buf = new StringBuilder();
Iterator<IgnoreVersion> iterator = ignoredVersions.iterator();
while ( iterator.hasNext() )
{
IgnoreVersion ignoreVersion = iterator.next();
buf.append( ignoreVersion );
if ( iterator.hasNext() )
{
buf.append( ", " );
}
}
return buf.toString();
}

@Override
public void resolveArtifact( Artifact artifact, boolean usePluginRepositories )
throws ArtifactResolutionException, ArtifactNotFoundException
Expand Down Expand Up @@ -674,12 +654,11 @@ public ArtifactVersion createArtifactVersion( String version )
* @param dependencies The set of {@link Dependency} instances to look up.
* @param usePluginRepositories Search the plugin repositories.
* @return map containing the ArtifactVersions object per dependency
* @throws ArtifactMetadataRetrievalException if the lookup does not succeed
*/
@Override
public Map<Dependency, ArtifactVersions> lookupDependenciesUpdates( Set<Dependency> dependencies,
boolean usePluginRepositories )
throws ArtifactMetadataRetrievalException
throws VersionRetrievalException
{
ExecutorService executor = Executors.newFixedThreadPool( LOOKUP_PARALLEL_THREADS );
try
Expand All @@ -699,8 +678,8 @@ public Map<Dependency, ArtifactVersions> lookupDependenciesUpdates( Set<Dependen
}
catch ( ExecutionException | InterruptedException ie )
{
throw new ArtifactMetadataRetrievalException( "Unable to acquire metadata for dependencies " + dependencies
+ ": " + ie.getMessage(), ie, null );
throw new VersionRetrievalException( "Unable to acquire metadata for dependencies "
+ dependencies + ": " + ie.getMessage(), ie );
}
finally
{
Expand All @@ -710,7 +689,7 @@ public Map<Dependency, ArtifactVersions> lookupDependenciesUpdates( Set<Dependen

@Override
public ArtifactVersions lookupDependencyUpdates( Dependency dependency, boolean usePluginRepositories )
throws ArtifactMetadataRetrievalException
throws VersionRetrievalException
{
ArtifactVersions allVersions = lookupArtifactVersions( createDependencyArtifact( dependency ),
usePluginRepositories );
Expand All @@ -720,7 +699,7 @@ public ArtifactVersions lookupDependencyUpdates( Dependency dependency, boolean

@Override
public Map<Plugin, PluginUpdatesDetails> lookupPluginsUpdates( Set<Plugin> plugins, boolean allowSnapshots )
throws ArtifactMetadataRetrievalException
throws VersionRetrievalException
{
ExecutorService executor = Executors.newFixedThreadPool( LOOKUP_PARALLEL_THREADS );
try
Expand All @@ -740,8 +719,8 @@ public Map<Plugin, PluginUpdatesDetails> lookupPluginsUpdates( Set<Plugin> plugi
}
catch ( ExecutionException | InterruptedException ie )
{
throw new ArtifactMetadataRetrievalException( "Unable to acquire metadata for plugins " + plugins + ": "
+ ie.getMessage(), ie, null );
throw new VersionRetrievalException( "Unable to acquire metadata for plugins " + plugins + ": "
+ ie.getMessage(), ie );
}
finally
{
Expand All @@ -751,7 +730,7 @@ public Map<Plugin, PluginUpdatesDetails> lookupPluginsUpdates( Set<Plugin> plugi

@Override
public PluginUpdatesDetails lookupPluginUpdates( Plugin plugin, boolean allowSnapshots )
throws ArtifactMetadataRetrievalException
throws VersionRetrievalException
{
String version = plugin.getVersion() != null
? plugin.getVersion()
Expand Down Expand Up @@ -885,7 +864,7 @@ else if ( !excludePropertiesList.isEmpty() && excludePropertiesList.contains( pr
.getProperty( property.getName() ) );
propertyVersions.put( property, versions );
}
catch ( ArtifactMetadataRetrievalException e )
catch ( VersionRetrievalException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
Expand Down Expand Up @@ -918,7 +897,6 @@ public static class Builder
{
private RepositorySystem repositorySystem;
private ArtifactResolver artifactResolver;
private ArtifactMetadataSource artifactMetadataSource;
private List<ArtifactRepository> remoteArtifactRepositories;
private List<ArtifactRepository> remotePluginRepositories;
private ArtifactRepository localRepository;
Expand All @@ -931,6 +909,8 @@ public static class Builder
private Log log;
private MavenSession mavenSession;
private MojoExecution mojoExecution;
private org.eclipse.aether.RepositorySystem aetherRepositorySystem;
private RepositorySystemSession repositorySystemSession;

public Builder()
{
Expand All @@ -948,12 +928,6 @@ public Builder withArtifactResolver( ArtifactResolver artifactResolver )
return this;
}

public Builder withArtifactMetadataSource( ArtifactMetadataSource artifactMetadataSource )
{
this.artifactMetadataSource = artifactMetadataSource;
return this;
}

public Builder withRemoteArtifactRepositories(
List<ArtifactRepository> remoteArtifactRepositories )
{
Expand Down Expand Up @@ -1028,6 +1002,18 @@ public Builder withMojoExecution( MojoExecution mojoExecution )
return this;
}

public Builder withAetherRepositorySystem( org.eclipse.aether.RepositorySystem aetherRepositorySystem )
{
this.aetherRepositorySystem = aetherRepositorySystem;
return this;
}

public Builder withRepositorySystemSession( RepositorySystemSession repositorySystemSession )
{
this.repositorySystemSession = repositorySystemSession;
return this;
}

/**
* Builds the constructed {@linkplain DefaultVersionsHelper} object
* @return constructed {@linkplain DefaultVersionsHelper}
Expand Down Expand Up @@ -1061,7 +1047,8 @@ public DefaultVersionsHelper build() throws MojoExecutionException
{
instance.ruleSet = enrichRuleSet( ignoredVersions, instance.ruleSet );
}
instance.artifactMetadataSource = artifactMetadataSource;
instance.aetherRepositorySystem = aetherRepositorySystem;
instance.repositorySystemSession = repositorySystemSession;
instance.localRepository = localRepository;
instance.remoteArtifactRepositories = remoteArtifactRepositories;
instance.remotePluginRepositories = remotePluginRepositories;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
Expand Down Expand Up @@ -74,7 +73,7 @@ public class PropertyVersions
private final PropertyVersions.PropertyVersionComparator comparator;

PropertyVersions( String profileId, String name, VersionsHelper helper, Set<ArtifactAssociation> associations )
throws ArtifactMetadataRetrievalException
throws VersionRetrievalException
{
this.profileId = profileId;
this.name = name;
Expand All @@ -88,7 +87,7 @@ public class PropertyVersions
private static SortedSet<ArtifactVersion> resolveAssociatedVersions( VersionsHelper helper,
Set<ArtifactAssociation> associations,
VersionComparator versionComparator )
throws ArtifactMetadataRetrievalException
throws VersionRetrievalException
{
SortedSet<ArtifactVersion> versions = null;
for ( ArtifactAssociation association : associations )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.TreeSet;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.codehaus.mojo.versions.ordering.VersionComparator;

Expand Down Expand Up @@ -91,7 +90,7 @@ public ArtifactAssociation[] getAssociations()
}

public PropertyVersions newPropertyVersions()
throws ArtifactMetadataRetrievalException
throws VersionRetrievalException
{
return new PropertyVersions( profileId, name, helper, associations );
}
Expand Down
Loading

0 comments on commit 49ed727

Please sign in to comment.