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 Dec 2, 2022
1 parent f61270b commit e63eab1
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 331 deletions.
Original file line number Diff line number Diff line change
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 @@ -175,7 +176,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 @@ -189,11 +191,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 @@ -237,6 +242,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
import java.util.Map;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
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 @@ -38,9 +35,7 @@
import org.apache.maven.reporting.MavenReportException;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.wagon.Wagon;
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 @@ -243,35 +238,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
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,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
Loading

0 comments on commit e63eab1

Please sign in to comment.