Skip to content

Commit

Permalink
Utilise version range for VersionsHelper where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejj0 committed Nov 27, 2022
1 parent d05e5cf commit 0042346
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.apache.maven.artifact.resolver.ArtifactResolver;
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 @@ -369,7 +370,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 @@ -383,11 +385,14 @@ public ArtifactVersions lookupArtifactVersions( Artifact artifact, boolean usePl
return new ArtifactVersions( artifact,
aetherRepositorySystem.resolveVersionRange( mavenSession.getRepositorySession(),
new VersionRangeRequest(
RepositoryUtils.toArtifact( artifact ).setVersion( "(,)" ),
usePluginRepositories
? mavenSession.getCurrentProject().getRemotePluginRepositories()
: mavenSession.getCurrentProject().getRemoteProjectRepositories(),
"lookupArtifactVersions" ) )
RepositoryUtils.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 @@ -431,6 +436,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 @@ -28,6 +28,7 @@
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
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 @@ -155,6 +156,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 @@ -24,12 +24,9 @@
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.resolver.ArtifactResolver;
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 @@ -41,9 +38,7 @@
import org.apache.maven.reporting.MavenReportException;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.settings.Settings;
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 @@ -269,35 +264,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 @@ -351,7 +351,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 0042346

Please sign in to comment.