Skip to content

Commit

Permalink
#704: Removing ArtifactRepository leftovers; using ranges for version…
Browse files Browse the repository at this point in the history
… resolution
  • Loading branch information
jarmoniuk committed Nov 30, 2022
1 parent d67f340 commit 081e19a
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 346 deletions.
Expand Up @@ -53,6 +53,7 @@
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Plugin;
Expand Down Expand Up @@ -355,7 +356,8 @@ public Log getLog()
}

@Override
public ArtifactVersions lookupArtifactVersions( Artifact artifact, boolean usePluginRepositories )
public ArtifactVersions lookupArtifactVersions( Artifact artifact, VersionRange versionRange,
boolean usePluginRepositories )
throws VersionRetrievalException
{
try
Expand All @@ -369,11 +371,14 @@ public ArtifactVersions lookupArtifactVersions( Artifact artifact, boolean usePl
return new ArtifactVersions( artifact,
aetherRepositorySystem.resolveVersionRange( mavenSession.getRepositorySession(),
new VersionRangeRequest(
toArtifact( artifact ).setVersion( "(,)" ),
usePluginRepositories
? mavenSession.getCurrentProject().getRemotePluginRepositories()
: mavenSession.getCurrentProject().getRemoteProjectRepositories(),
"lookupArtifactVersions" ) )
toArtifact( artifact ).setVersion(
versionRange != null
? versionRange.toString()
: "(,)" ),
usePluginRepositories
? mavenSession.getCurrentProject().getRemotePluginRepositories()
: mavenSession.getCurrentProject().getRemoteProjectRepositories(),
"lookupArtifactVersions" ) )
.getVersions()
.parallelStream()
.filter( v -> ignoredVersions.stream()
Expand Down Expand Up @@ -417,6 +422,13 @@ public ArtifactVersions lookupArtifactVersions( Artifact artifact, boolean usePl
}
}

@Override
public ArtifactVersions lookupArtifactVersions( Artifact artifact, boolean usePluginRepositories )
throws VersionRetrievalException
{
return lookupArtifactVersions( artifact, null, usePluginRepositories );
}

/**
* Returns a list of versions which should not be considered when looking for updates.
*
Expand Down
Expand Up @@ -1548,7 +1548,6 @@ public static MavenProject getStandaloneSuperProject( ProjectBuilder projectBuil
}
return result.getProject();
}

/**
* <p>Convenience method for creating a {@link ProjectBuildingRequest} instance based on maven session.</p>
* <p><u>Note:</u> The method initializes the remote repositories with the remote artifact repositories.
Expand Down
Expand Up @@ -27,6 +27,7 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.MojoExecutionException;
Expand Down Expand Up @@ -154,6 +155,22 @@ Artifact createDependencyArtifact( String groupId, String artifactId, String ver
ArtifactVersions lookupArtifactVersions( Artifact artifact, boolean usePluginRepositories )
throws VersionRetrievalException;

/**
* Looks up the versions of the specified artifact that are available in either the local repository, or the
* appropriate remote repositories.
*
* @param artifact The artifact to look for versions of.
* @param versionRange versionRange to restrict the search
* @param usePluginRepositories <code>true</code> will consult the pluginRepositories, while <code>false</code> will
* consult the repositories for normal dependencies.
* @return The details of the available artifact versions.
* @throws VersionRetrievalException thrown if version resolution fails
* @since 1.0-alpha-3
*/
ArtifactVersions lookupArtifactVersions( Artifact artifact, VersionRange versionRange,
boolean usePluginRepositories )
throws VersionRetrievalException;

/**
* Looks up the updates for a set of dependencies.
*
Expand Down
Expand Up @@ -20,14 +20,11 @@

import java.util.HashMap;

import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.testing.stubs.StubArtifactRepository;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.settings.Settings;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.junit.Test;
Expand Down Expand Up @@ -57,23 +54,17 @@ private static EnforcerRuleHelper mockRuleHelper( MavenProject mavenProject,
when( ruleHelper.evaluate( anyString() ) )
.then( ( a ) -> "${project}".equals( a.getArgument( 0 ) )
? mavenProject
: "${localRepository}".equals( a.getArgument( 0 ) )
? new StubArtifactRepository( "" )
: "${settings}".equals( a.getArgument( 0 ) )
? new Settings()
: "${session}".equals( a.getArgument( 0 ) )
? mockMavenSession()
: "${mojoExecution}".equals( a.getArgument( 0 ) )
? mock( MojoExecution.class )
: null );
: "${session}".equals( a.getArgument( 0 ) )
? mockMavenSession()
: "${mojoExecution}".equals( a.getArgument( 0 ) )
? mock( MojoExecution.class )
: null );
when( ruleHelper.getComponent( ArgumentMatchers.<Class<?>>any() ) )
.then( ( a ) -> a.getArgument( 0 ) == RepositorySystem.class
? mockRepositorySystem()
: a.getArgument( 0 ) == ArtifactResolver.class
? mock( ArtifactResolver.class )
: a.getArgument( 0 ) == org.eclipse.aether.RepositorySystem.class
? aetherRepositorySystem
: null );
: a.getArgument( 0 ) == org.eclipse.aether.RepositorySystem.class
? aetherRepositorySystem
: null );
return ruleHelper;
}

Expand Down
Expand Up @@ -20,15 +20,10 @@
*/

import java.io.File;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.doxia.sink.Sink;
import org.apache.maven.doxia.siterenderer.Renderer;
import org.apache.maven.execution.MavenSession;
Expand All @@ -39,9 +34,7 @@
import org.apache.maven.reporting.AbstractMavenReport;
import org.apache.maven.reporting.MavenReportException;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.DefaultVersionsHelper;
import org.codehaus.mojo.versions.api.VersionRetrievalException;
import org.codehaus.mojo.versions.api.VersionsHelper;
import org.codehaus.mojo.versions.model.RuleSet;
import org.codehaus.mojo.versions.reporting.ReportRendererFactory;
Expand Down Expand Up @@ -79,18 +72,6 @@ public abstract class AbstractVersionsReport<T>
*/
protected org.eclipse.aether.RepositorySystem aetherRepositorySystem;

/**
* @since 1.0-alpha-3
*/
@Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true )
protected List<ArtifactRepository> remoteArtifactRepositories;

/**
* @since 1.0-alpha-3
*/
@Parameter( defaultValue = "${project.pluginArtifactRepositories}", readonly = true )
protected List<ArtifactRepository> remotePluginRepositories;

/**
* @since 1.0-alpha-3
*/
Expand Down Expand Up @@ -254,35 +235,6 @@ protected void executeReport( Locale locale )
protected abstract void doGenerateReport( Locale locale, Sink sink )
throws MavenReportException, MojoExecutionException;

/**
* Finds the latest version of the specified artifact that matches the version range.
*
* @param artifact The artifact.
* @param versionRange The version range.
* @param allowingSnapshots <code>null</code> for no override, otherwise the local override to apply.
* @param usePluginRepositories Use plugin repositories
* @return The latest version of the specified artifact that matches the specified version range or
* <code>null</code> if no matching version could be found.
* @throws MavenReportException If the artifact metadata could not be found.
* @since 1.0-alpha-1
*/
protected ArtifactVersion findLatestVersion( Artifact artifact, VersionRange versionRange,
Boolean allowingSnapshots, boolean usePluginRepositories )
throws MavenReportException
{
boolean includeSnapshots = allowingSnapshots != null ? allowingSnapshots : this.allowSnapshots;
try
{
final ArtifactVersions artifactVersions =
getHelper().lookupArtifactVersions( artifact, usePluginRepositories );
return artifactVersions.getNewestVersion( versionRange, includeSnapshots );
}
catch ( VersionRetrievalException e )
{
throw new MavenReportException( e.getMessage(), e );
}
}

@Override
protected MavenProject getProject()
{
Expand Down
Expand Up @@ -306,7 +306,8 @@ protected ArtifactVersion findLatestVersion( Artifact artifact, VersionRange ver
throws MojoExecutionException, VersionRetrievalException
{
boolean includeSnapshots = allowingSnapshots != null ? allowingSnapshots : this.allowSnapshots;
final ArtifactVersions artifactVersions = getHelper().lookupArtifactVersions( artifact, usePluginRepositories );
final ArtifactVersions artifactVersions = getHelper().lookupArtifactVersions( artifact, versionRange,
usePluginRepositories );
return artifactVersions.getNewestVersion( versionRange, null, includeSnapshots, false );
}

Expand Down

0 comments on commit 081e19a

Please sign in to comment.