Skip to content

Commit

Permalink
#499: Add segment selection to update parent
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmoniuk authored and slawekjaranowski committed Oct 8, 2022
1 parent 736f87b commit 97c6333
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 257 deletions.
1 change: 0 additions & 1 deletion src/it/it-update-parent-002/invoker.properties

This file was deleted.

50 changes: 0 additions & 50 deletions src/it/it-update-parent-002/pom.xml

This file was deleted.

33 changes: 0 additions & 33 deletions src/it/it-update-parent-002/verify.bsh

This file was deleted.

1 change: 0 additions & 1 deletion src/it/it-update-parent-003/invoker.properties

This file was deleted.

50 changes: 0 additions & 50 deletions src/it/it-update-parent-003/pom.xml

This file was deleted.

32 changes: 0 additions & 32 deletions src/it/it-update-parent-003/verify.bsh

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -345,32 +345,10 @@ public void execute()
protected ArtifactVersion findLatestVersion( Artifact artifact, VersionRange versionRange,
Boolean allowingSnapshots, boolean usePluginRepositories )
throws ArtifactMetadataRetrievalException, MojoExecutionException
{
return findLatestVersion( artifact, versionRange, allowingSnapshots, usePluginRepositories, false );
}

/**
* 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
* @param allowDowngrade whether downgrades should be allowed
* @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 ArtifactMetadataRetrievalException If the artifact metadata could not be found.
* @throws MojoExecutionException if something goes wrong.
* @since 1.0-alpha-1
*/
protected ArtifactVersion findLatestVersion( Artifact artifact, VersionRange versionRange,
Boolean allowingSnapshots, boolean usePluginRepositories,
boolean allowDowngrade )
throws ArtifactMetadataRetrievalException, MojoExecutionException
{
boolean includeSnapshots = allowingSnapshots != null ? allowingSnapshots : this.allowSnapshots;
final ArtifactVersions artifactVersions = getHelper().lookupArtifactVersions( artifact, usePluginRepositories );
return artifactVersions.getNewestVersion( versionRange, null, includeSnapshots, allowDowngrade );
return artifactVersions.getNewestVersion( versionRange, null, includeSnapshots, false );
}

/**
Expand Down
83 changes: 73 additions & 10 deletions src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
import javax.inject.Inject;
import javax.xml.stream.XMLStreamException;

import java.util.Arrays;
import java.util.Collections;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
Expand All @@ -37,7 +42,10 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.Segment;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.mojo.versions.utils.DependencyBuilder;

Expand Down Expand Up @@ -95,6 +103,30 @@ public class UpdateParentMojo extends AbstractVersionsUpdaterMojo
defaultValue = "false" )
protected boolean allowDowngrade;

/**
* Whether to allow the major version number to be changed.
*
* @since 2.13.0
*/
@Parameter( property = "allowMajorUpdates", defaultValue = "true" )
protected boolean allowMajorUpdates = true;

/**
* Whether to allow the minor version number to be changed.
*
* @since 2.13.0
*/
@Parameter( property = "allowMinorUpdates", defaultValue = "true" )
protected boolean allowMinorUpdates = true;

/**
* Whether to allow the incremental version number to be changed.
*
* @since 2.13.0
*/
@Parameter( property = "allowIncrementalUpdates", defaultValue = "true" )
protected boolean allowIncrementalUpdates = true;

// -------------------------- OTHER METHODS --------------------------

@Inject
Expand Down Expand Up @@ -135,10 +167,13 @@ protected void update( ModifiedPomXMLEventReader pom )
throw new MojoExecutionException( "skipResolution is only valid if parentVersion is set" );
}

String initialVersion = parentVersion == null ? getProject().getParent().getVersion() : parentVersion;
String initialVersion = parentVersion == null
? getProject().getParent().getVersion()
: parentVersion;
try
{
ArtifactVersion artifactVersion = skipResolution ? new DefaultArtifactVersion( parentVersion )
ArtifactVersion artifactVersion = skipResolution
? new DefaultArtifactVersion( parentVersion )
: resolveTargetVersion( initialVersion );
if ( artifactVersion != null )
{
Expand Down Expand Up @@ -166,10 +201,15 @@ protected void update( ModifiedPomXMLEventReader pom )
{
throw new MojoExecutionException( e.getMessage(), e );
}
catch ( InvalidSegmentException e )
{
throw new MojoExecutionException( "Invalid segment specification for version " + initialVersion, e );
}
}

private ArtifactVersion resolveTargetVersion( String initialVersion )
throws MojoExecutionException, ArtifactMetadataRetrievalException, InvalidVersionSpecificationException
protected ArtifactVersion resolveTargetVersion( String initialVersion )
throws MojoExecutionException, ArtifactMetadataRetrievalException, InvalidVersionSpecificationException,
InvalidSegmentException
{
Artifact artifact = getHelper().createDependencyArtifact( DependencyBuilder
.newBuilder()
Expand All @@ -186,14 +226,37 @@ private ArtifactVersion resolveTargetVersion( String initialVersion )
VersionRange.createFromVersionSpec( "[" + targetVersionRange.getRecommendedVersion() + ",)" ) );
}

ArtifactVersion artifactVersion = findLatestVersion( artifact, targetVersionRange, null,
false, allowDowngrade );
if ( !shouldApplyUpdate( artifact, getProject().getParent().getVersion(), artifactVersion, forceUpdate ) )
final ArtifactVersions versions = getHelper().lookupArtifactVersions( artifact, false );
Optional<Segment> unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
allowIncrementalUpdates );

// currentVersion (set to parentVersion here) is not included in the version range for searching upgrades
// unless we set allowDowngrade to true
for ( ArtifactVersion candidate : reverse( versions.getNewerVersions( initialVersion, unchangedSegment,
allowSnapshots, !isBlank( parentVersion ) || allowDowngrade ) ) )
{
getLog().debug( "Update not applied. Exiting." );
return null;
if ( allowDowngrade
|| targetVersionRange == null
|| ArtifactVersions.isVersionInRange( candidate, targetVersionRange ) )
{
if ( shouldApplyUpdate( artifact, getProject().getParent().getVersion(), candidate, forceUpdate ) )
{
return candidate;
}
else
{
getLog().debug( "Update not applied. Exiting." );
return null;
}
}
}
return artifactVersion;

getLog().info( "No versions found" );
return null;
}

private static <T> Iterable<T> reverse( T[] array )
{
return Arrays.stream( array ).sorted( Collections.reverseOrder() ).collect( Collectors.toList() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,19 @@ public final ArtifactVersion getNewestVersion( VersionRange versionRange, Restri
// so we only need to find the first candidate fulfilling the criteria
for ( ArtifactVersion candidate : reverse( getVersions( includeSnapshots ) ) )
{
if ( !allowDowngrade && versionRange != null
&& !ArtifactVersions.isVersionInRange( candidate, versionRange ) )
if ( allowDowngrade || versionRange == null
|| ArtifactVersions.isVersionInRange( candidate, versionRange ) )
{
continue;
}
if ( restriction != null && !isVersionInRestriction( restriction, candidate ) )
{
continue;
}
if ( !includeSnapshots && ArtifactUtils.isSnapshot( candidate.toString() ) )
{
continue;
if ( restriction != null && !isVersionInRestriction( restriction, candidate ) )
{
continue;
}
if ( !includeSnapshots && ArtifactUtils.isSnapshot( candidate.toString() ) )
{
continue;
}
return candidate;
}
return candidate;
}
return null;
}
Expand Down

0 comments on commit 97c6333

Please sign in to comment.