Skip to content

Commit

Permalink
Fixing #251: proper handling of InvalidSegmentException and InvalidVe…
Browse files Browse the repository at this point in the history
…rsionSpecificationException
  • Loading branch information
andrzejj0 authored and slawekjaranowski committed Sep 6, 2022
1 parent 0db6ce4 commit 5fa3693
Show file tree
Hide file tree
Showing 23 changed files with 593 additions and 286 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
Expand All @@ -54,6 +55,7 @@
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.PropertyVersions;
import org.codehaus.mojo.versions.api.VersionsHelper;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.recording.ChangeRecorder;
import org.codehaus.mojo.versions.recording.ChangeRecorderNull;
import org.codehaus.mojo.versions.recording.ChangeRecorderXML;
Expand Down Expand Up @@ -101,7 +103,7 @@ public abstract class AbstractVersionsUpdaterMojo
* @since 1.0-alpha-1
*/
@Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
protected List reactorProjects;
protected List<MavenProject> reactorProjects;

/**
* The artifact metadata source to use.
Expand Down Expand Up @@ -216,8 +218,7 @@ public abstract class AbstractVersionsUpdaterMojo

// --------------------- GETTER / SETTER METHODS ---------------------

public VersionsHelper getHelper()
throws MojoExecutionException
public VersionsHelper getHelper() throws MojoExecutionException
{
if ( helper == null )
{
Expand Down Expand Up @@ -514,13 +515,22 @@ protected boolean shouldApplyUpdate( Artifact artifact, String currentVersion, A
}

/**
* Based on the passed flags, determines which segment is unchangable. This can be used when determining an upper
* <p>Based on the passed flags, determines which segment (0-based), which is not to be changed.</p>
* <p>The method will return, depending on the first parameter on the list to be true:
* <ul>
* <li>{@code allowMajorUpdates}: -1</li>
* <li>{@code allowMinorUpdates}: 0</li>
* <li>{@code allowIncrementalUpdates}: 1</li>
* <li>(none): 2</li>
* </ul>
*
* This can be used when determining an upper
* bound for the "latest" version.
*
* @param allowMajorUpdates Allow major updates
* @param allowMinorUpdates Allow minor updates
* @param allowIncrementalUpdates Allow incremental updates
* @return Returns the segment that is unchangable. If any segment can change, returns -1.
* @return Returns the segment (0-based) that is unchangable. If any segment can change, returns -1.
*/
protected int determineUnchangedSegment( boolean allowMajorUpdates, boolean allowMinorUpdates,
boolean allowIncrementalUpdates )
Expand Down Expand Up @@ -553,7 +563,8 @@ else if ( allowIncrementalUpdates )
protected ArtifactVersion updatePropertyToNewestVersion( ModifiedPomXMLEventReader pom, Property property,
PropertyVersions version, String currentVersion,
boolean allowDowngrade, int segment )
throws MojoExecutionException, XMLStreamException
throws XMLStreamException, InvalidVersionSpecificationException,
InvalidSegmentException, MojoExecutionException
{
ArtifactVersion winner =
version.getNewestVersion( currentVersion, property, this.allowSnapshots, this.reactorProjects,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@

import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.mojo.versions.api.PropertyVersions;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

/**
Expand Down Expand Up @@ -138,44 +140,51 @@ public void execute()
}

int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );
ArtifactVersion winner = version.getNewestVersion( currentVersion, property, this.allowSnapshots,
this.reactorProjects, this.getHelper(), false, segment );

if ( winner != null && !currentVersion.equals( winner.toString() ) )
try
{
StringBuilder buf = new StringBuilder();
buf.append( "${" );
buf.append( property.getName() );
buf.append( "} " );
final String newVersion = winner.toString();
int padding =
INFO_PAD_SIZE - currentVersion.length() - newVersion.length() - 4 + getOutputLineWidthOffset();
while ( buf.length() < padding )
ArtifactVersion winner = version.getNewestVersion( currentVersion, property, this.allowSnapshots,
this.reactorProjects, this.getHelper(), false, segment );
if ( winner != null && !currentVersion.equals( winner.toString() ) )
{
buf.append( '.' );
StringBuilder buf = new StringBuilder();
buf.append( "${" );
buf.append( property.getName() );
buf.append( "} " );
final String newVersion = winner.toString();
int padding =
INFO_PAD_SIZE - currentVersion.length() - newVersion.length() - 4
+ getOutputLineWidthOffset();
while ( buf.length() < padding )
{
buf.append( '.' );
}
buf.append( ' ' );
buf.append( currentVersion );
buf.append( " -> " );
buf.append( newVersion );
updates.add( buf.toString() );
}
buf.append( ' ' );
buf.append( currentVersion );
buf.append( " -> " );
buf.append( newVersion );
updates.add( buf.toString() );
}
else
{
StringBuilder buf = new StringBuilder();
buf.append( "${" );
buf.append( property.getName() );
buf.append( "} " );
int padding = INFO_PAD_SIZE - currentVersion.length() + getOutputLineWidthOffset();
while ( buf.length() < padding )
else
{
buf.append( '.' );
StringBuilder buf = new StringBuilder();
buf.append( "${" );
buf.append( property.getName() );
buf.append( "} " );
int padding = INFO_PAD_SIZE - currentVersion.length() + getOutputLineWidthOffset();
while ( buf.length() < padding )
{
buf.append( '.' );
}
buf.append( ' ' );
buf.append( currentVersion );
current.add( buf.toString() );
}
buf.append( ' ' );
buf.append( currentVersion );
current.add( buf.toString() );
}

catch ( InvalidSegmentException | InvalidVersionSpecificationException e )
{
getLog().warn( String.format( "Skipping the processing of %s:%s due to: %s", property.getName(),
property.getVersion(), e.getMessage() ) );
}
}

logLine( false, "" );
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/codehaus/mojo/versions/PluginUpdatesDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,21 @@ public Map<Dependency, ArtifactVersions> getDependencyVersions()
return dependencyVersions;
}

/**
* Returns true if a new version of the artifact fulfilling the criteria (whether to include snapshots) can be found
*
* @return true if a new version can be found
*/
public boolean isArtifactUpdateAvailable()
{
return artifactVersions.getAllUpdates( UpdateScope.ANY, includeSnapshots ).length > 0;
}

/**
* Returns true if a new version of the dependency can be found
*
* @return true if a new version can be found
*/
public boolean isDependencyUpdateAvailable()
{
for ( ArtifactVersions versions : dependencyVersions.values() )
Expand All @@ -76,6 +86,11 @@ public boolean isDependencyUpdateAvailable()
return false;
}

/**
* Returns true if a new version of the dependency can be found
*
* @return true if a new version can be found
*/
public boolean isUpdateAvailable()
{
return isArtifactUpdateAvailable() || isDependencyUpdateAvailable();
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.PropertyVersions;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

/**
Expand Down Expand Up @@ -294,8 +296,15 @@ private void resolvePropertyRanges( ModifiedPomXMLEventReader pom )

int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );
// TODO: Check if we could add allowDowngrade ?
updatePropertyToNewestVersion( pom, property, version, currentVersion, false, segment );

try
{
updatePropertyToNewestVersion( pom, property, version, currentVersion, false, segment );
}
catch ( InvalidSegmentException | InvalidVersionSpecificationException e )
{
getLog().warn( String.format( "Skipping the processing of %s:%s due to: %s", property.getName(),
property.getVersion(), e.getMessage() ) );
}
}
}

Expand Down
29 changes: 20 additions & 9 deletions src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import java.util.Map;

import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.mojo.versions.api.ArtifactAssociation;
import org.codehaus.mojo.versions.api.PropertyVersions;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

/**
Expand Down Expand Up @@ -166,21 +168,30 @@ protected void update( ModifiedPomXMLEventReader pom )
{
int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
allowIncrementalUpdates );
ArtifactVersion targetVersion = updatePropertyToNewestVersion( pom, property, version, currentVersion,
allowDowngrade, segment );

if ( targetVersion != null )
try
{
for ( final ArtifactAssociation association : version.getAssociations() )
ArtifactVersion targetVersion =
updatePropertyToNewestVersion( pom, property, version, currentVersion,
allowDowngrade, segment );

if ( targetVersion != null )
{
if ( ( isIncluded( association.getArtifact() ) ) )
for ( final ArtifactAssociation association : version.getAssociations() )
{
this.getChangeRecorder().recordUpdate( "updateProperty", association.getGroupId(),
association.getArtifactId(), currentVersion,
targetVersion.toString() );
if ( ( isIncluded( association.getArtifact() ) ) )
{
this.getChangeRecorder().recordUpdate( "updateProperty", association.getGroupId(),
association.getArtifactId(), currentVersion,
targetVersion.toString() );
}
}
}
}
catch ( InvalidSegmentException | InvalidVersionSpecificationException e )
{
getLog().warn( String.format( "Skipping the processing of %s:%s due to: %s", property.getName(),
property.getVersion(), e.getMessage() ) );
}
}

}
Expand Down
26 changes: 18 additions & 8 deletions src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import java.util.Map;

import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.mojo.versions.api.ArtifactAssociation;
import org.codehaus.mojo.versions.api.PropertyVersions;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

/**
Expand Down Expand Up @@ -146,18 +148,26 @@ protected void update( ModifiedPomXMLEventReader pom )
}

int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );
ArtifactVersion targetVersion = updatePropertyToNewestVersion( pom, property, version, currentVersion,
allowDowngrade, segment );

if ( targetVersion != null )
try
{
for ( final ArtifactAssociation association : version.getAssociations() )
ArtifactVersion targetVersion = updatePropertyToNewestVersion( pom, property, version, currentVersion,
allowDowngrade, segment );

if ( targetVersion != null )
{
this.getChangeRecorder().recordUpdate( "updateProperty", association.getGroupId(),
association.getArtifactId(), currentVersion,
targetVersion.toString() );
for ( final ArtifactAssociation association : version.getAssociations() )
{
this.getChangeRecorder().recordUpdate( "updateProperty", association.getGroupId(),
association.getArtifactId(), currentVersion,
targetVersion.toString() );
}
}
}
catch ( InvalidSegmentException | InvalidVersionSpecificationException e )
{
getLog().warn( String.format( "Skipping the processing of %s:%s due to: %s", property.getName(),
property.getVersion(), e.getMessage() ) );
}
}
}

Expand Down
Loading

0 comments on commit 5fa3693

Please sign in to comment.