Skip to content

Commit 5fa3693

Browse files
andrzejj0slawekjaranowski
authored andcommitted
Fixing #251: proper handling of InvalidSegmentException and InvalidVersionSpecificationException
1 parent 0db6ce4 commit 5fa3693

23 files changed

+593
-286
lines changed

src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
3838
import org.apache.maven.artifact.resolver.ArtifactResolver;
3939
import org.apache.maven.artifact.versioning.ArtifactVersion;
40+
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
4041
import org.apache.maven.artifact.versioning.VersionRange;
4142
import org.apache.maven.execution.MavenSession;
4243
import org.apache.maven.plugin.AbstractMojo;
@@ -54,6 +55,7 @@
5455
import org.codehaus.mojo.versions.api.PomHelper;
5556
import org.codehaus.mojo.versions.api.PropertyVersions;
5657
import org.codehaus.mojo.versions.api.VersionsHelper;
58+
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
5759
import org.codehaus.mojo.versions.recording.ChangeRecorder;
5860
import org.codehaus.mojo.versions.recording.ChangeRecorderNull;
5961
import org.codehaus.mojo.versions.recording.ChangeRecorderXML;
@@ -101,7 +103,7 @@ public abstract class AbstractVersionsUpdaterMojo
101103
* @since 1.0-alpha-1
102104
*/
103105
@Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
104-
protected List reactorProjects;
106+
protected List<MavenProject> reactorProjects;
105107

106108
/**
107109
* The artifact metadata source to use.
@@ -216,8 +218,7 @@ public abstract class AbstractVersionsUpdaterMojo
216218

217219
// --------------------- GETTER / SETTER METHODS ---------------------
218220

219-
public VersionsHelper getHelper()
220-
throws MojoExecutionException
221+
public VersionsHelper getHelper() throws MojoExecutionException
221222
{
222223
if ( helper == null )
223224
{
@@ -514,13 +515,22 @@ protected boolean shouldApplyUpdate( Artifact artifact, String currentVersion, A
514515
}
515516

516517
/**
517-
* Based on the passed flags, determines which segment is unchangable. This can be used when determining an upper
518+
* <p>Based on the passed flags, determines which segment (0-based), which is not to be changed.</p>
519+
* <p>The method will return, depending on the first parameter on the list to be true:
520+
* <ul>
521+
* <li>{@code allowMajorUpdates}: -1</li>
522+
* <li>{@code allowMinorUpdates}: 0</li>
523+
* <li>{@code allowIncrementalUpdates}: 1</li>
524+
* <li>(none): 2</li>
525+
* </ul>
526+
*
527+
* This can be used when determining an upper
518528
* bound for the "latest" version.
519529
*
520530
* @param allowMajorUpdates Allow major updates
521531
* @param allowMinorUpdates Allow minor updates
522532
* @param allowIncrementalUpdates Allow incremental updates
523-
* @return Returns the segment that is unchangable. If any segment can change, returns -1.
533+
* @return Returns the segment (0-based) that is unchangable. If any segment can change, returns -1.
524534
*/
525535
protected int determineUnchangedSegment( boolean allowMajorUpdates, boolean allowMinorUpdates,
526536
boolean allowIncrementalUpdates )
@@ -553,7 +563,8 @@ else if ( allowIncrementalUpdates )
553563
protected ArtifactVersion updatePropertyToNewestVersion( ModifiedPomXMLEventReader pom, Property property,
554564
PropertyVersions version, String currentVersion,
555565
boolean allowDowngrade, int segment )
556-
throws MojoExecutionException, XMLStreamException
566+
throws XMLStreamException, InvalidVersionSpecificationException,
567+
InvalidSegmentException, MojoExecutionException
557568
{
558569
ArtifactVersion winner =
559570
version.getNewestVersion( currentVersion, property, this.allowSnapshots, this.reactorProjects,

src/main/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojo.java

+41-32
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828

2929
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
3030
import org.apache.maven.artifact.versioning.ArtifactVersion;
31+
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
3132
import org.apache.maven.plugin.MojoExecutionException;
3233
import org.apache.maven.plugin.MojoFailureException;
3334
import org.apache.maven.plugins.annotations.Mojo;
3435
import org.apache.maven.plugins.annotations.Parameter;
3536
import org.codehaus.mojo.versions.api.PropertyVersions;
37+
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
3638
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
3739

3840
/**
@@ -138,44 +140,51 @@ public void execute()
138140
}
139141

140142
int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );
141-
ArtifactVersion winner = version.getNewestVersion( currentVersion, property, this.allowSnapshots,
142-
this.reactorProjects, this.getHelper(), false, segment );
143-
144-
if ( winner != null && !currentVersion.equals( winner.toString() ) )
143+
try
145144
{
146-
StringBuilder buf = new StringBuilder();
147-
buf.append( "${" );
148-
buf.append( property.getName() );
149-
buf.append( "} " );
150-
final String newVersion = winner.toString();
151-
int padding =
152-
INFO_PAD_SIZE - currentVersion.length() - newVersion.length() - 4 + getOutputLineWidthOffset();
153-
while ( buf.length() < padding )
145+
ArtifactVersion winner = version.getNewestVersion( currentVersion, property, this.allowSnapshots,
146+
this.reactorProjects, this.getHelper(), false, segment );
147+
if ( winner != null && !currentVersion.equals( winner.toString() ) )
154148
{
155-
buf.append( '.' );
149+
StringBuilder buf = new StringBuilder();
150+
buf.append( "${" );
151+
buf.append( property.getName() );
152+
buf.append( "} " );
153+
final String newVersion = winner.toString();
154+
int padding =
155+
INFO_PAD_SIZE - currentVersion.length() - newVersion.length() - 4
156+
+ getOutputLineWidthOffset();
157+
while ( buf.length() < padding )
158+
{
159+
buf.append( '.' );
160+
}
161+
buf.append( ' ' );
162+
buf.append( currentVersion );
163+
buf.append( " -> " );
164+
buf.append( newVersion );
165+
updates.add( buf.toString() );
156166
}
157-
buf.append( ' ' );
158-
buf.append( currentVersion );
159-
buf.append( " -> " );
160-
buf.append( newVersion );
161-
updates.add( buf.toString() );
162-
}
163-
else
164-
{
165-
StringBuilder buf = new StringBuilder();
166-
buf.append( "${" );
167-
buf.append( property.getName() );
168-
buf.append( "} " );
169-
int padding = INFO_PAD_SIZE - currentVersion.length() + getOutputLineWidthOffset();
170-
while ( buf.length() < padding )
167+
else
171168
{
172-
buf.append( '.' );
169+
StringBuilder buf = new StringBuilder();
170+
buf.append( "${" );
171+
buf.append( property.getName() );
172+
buf.append( "} " );
173+
int padding = INFO_PAD_SIZE - currentVersion.length() + getOutputLineWidthOffset();
174+
while ( buf.length() < padding )
175+
{
176+
buf.append( '.' );
177+
}
178+
buf.append( ' ' );
179+
buf.append( currentVersion );
180+
current.add( buf.toString() );
173181
}
174-
buf.append( ' ' );
175-
buf.append( currentVersion );
176-
current.add( buf.toString() );
177182
}
178-
183+
catch ( InvalidSegmentException | InvalidVersionSpecificationException e )
184+
{
185+
getLog().warn( String.format( "Skipping the processing of %s:%s due to: %s", property.getName(),
186+
property.getVersion(), e.getMessage() ) );
187+
}
179188
}
180189

181190
logLine( false, "" );

src/main/java/org/codehaus/mojo/versions/PluginUpdatesDetails.java

+15
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,21 @@ public Map<Dependency, ArtifactVersions> getDependencyVersions()
5858
return dependencyVersions;
5959
}
6060

61+
/**
62+
* Returns true if a new version of the artifact fulfilling the criteria (whether to include snapshots) can be found
63+
*
64+
* @return true if a new version can be found
65+
*/
6166
public boolean isArtifactUpdateAvailable()
6267
{
6368
return artifactVersions.getAllUpdates( UpdateScope.ANY, includeSnapshots ).length > 0;
6469
}
6570

71+
/**
72+
* Returns true if a new version of the dependency can be found
73+
*
74+
* @return true if a new version can be found
75+
*/
6676
public boolean isDependencyUpdateAvailable()
6777
{
6878
for ( ArtifactVersions versions : dependencyVersions.values() )
@@ -76,6 +86,11 @@ public boolean isDependencyUpdateAvailable()
7686
return false;
7787
}
7888

89+
/**
90+
* Returns true if a new version of the dependency can be found
91+
*
92+
* @return true if a new version can be found
93+
*/
7994
public boolean isUpdateAvailable()
8095
{
8196
return isArtifactUpdateAvailable() || isDependencyUpdateAvailable();

src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
import org.apache.maven.artifact.Artifact;
3131
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
3232
import org.apache.maven.artifact.versioning.ArtifactVersion;
33+
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
3334
import org.apache.maven.model.Dependency;
3435
import org.apache.maven.plugin.MojoExecutionException;
3536
import org.apache.maven.plugin.MojoFailureException;
3637
import org.apache.maven.plugins.annotations.Mojo;
3738
import org.apache.maven.plugins.annotations.Parameter;
3839
import org.codehaus.mojo.versions.api.PomHelper;
3940
import org.codehaus.mojo.versions.api.PropertyVersions;
41+
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
4042
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
4143

4244
/**
@@ -294,8 +296,15 @@ private void resolvePropertyRanges( ModifiedPomXMLEventReader pom )
294296

295297
int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );
296298
// TODO: Check if we could add allowDowngrade ?
297-
updatePropertyToNewestVersion( pom, property, version, currentVersion, false, segment );
298-
299+
try
300+
{
301+
updatePropertyToNewestVersion( pom, property, version, currentVersion, false, segment );
302+
}
303+
catch ( InvalidSegmentException | InvalidVersionSpecificationException e )
304+
{
305+
getLog().warn( String.format( "Skipping the processing of %s:%s due to: %s", property.getName(),
306+
property.getVersion(), e.getMessage() ) );
307+
}
299308
}
300309
}
301310

src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java

+20-9
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
import java.util.Map;
2525

2626
import org.apache.maven.artifact.versioning.ArtifactVersion;
27+
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
2728
import org.apache.maven.plugin.MojoExecutionException;
2829
import org.apache.maven.plugin.MojoFailureException;
2930
import org.apache.maven.plugins.annotations.Mojo;
3031
import org.apache.maven.plugins.annotations.Parameter;
3132
import org.codehaus.mojo.versions.api.ArtifactAssociation;
3233
import org.codehaus.mojo.versions.api.PropertyVersions;
34+
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
3335
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
3436

3537
/**
@@ -166,21 +168,30 @@ protected void update( ModifiedPomXMLEventReader pom )
166168
{
167169
int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
168170
allowIncrementalUpdates );
169-
ArtifactVersion targetVersion = updatePropertyToNewestVersion( pom, property, version, currentVersion,
170-
allowDowngrade, segment );
171-
172-
if ( targetVersion != null )
171+
try
173172
{
174-
for ( final ArtifactAssociation association : version.getAssociations() )
173+
ArtifactVersion targetVersion =
174+
updatePropertyToNewestVersion( pom, property, version, currentVersion,
175+
allowDowngrade, segment );
176+
177+
if ( targetVersion != null )
175178
{
176-
if ( ( isIncluded( association.getArtifact() ) ) )
179+
for ( final ArtifactAssociation association : version.getAssociations() )
177180
{
178-
this.getChangeRecorder().recordUpdate( "updateProperty", association.getGroupId(),
179-
association.getArtifactId(), currentVersion,
180-
targetVersion.toString() );
181+
if ( ( isIncluded( association.getArtifact() ) ) )
182+
{
183+
this.getChangeRecorder().recordUpdate( "updateProperty", association.getGroupId(),
184+
association.getArtifactId(), currentVersion,
185+
targetVersion.toString() );
186+
}
181187
}
182188
}
183189
}
190+
catch ( InvalidSegmentException | InvalidVersionSpecificationException e )
191+
{
192+
getLog().warn( String.format( "Skipping the processing of %s:%s due to: %s", property.getName(),
193+
property.getVersion(), e.getMessage() ) );
194+
}
184195
}
185196

186197
}

src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java

+18-8
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
import java.util.Map;
2525

2626
import org.apache.maven.artifact.versioning.ArtifactVersion;
27+
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
2728
import org.apache.maven.plugin.MojoExecutionException;
2829
import org.apache.maven.plugin.MojoFailureException;
2930
import org.apache.maven.plugins.annotations.Mojo;
3031
import org.apache.maven.plugins.annotations.Parameter;
3132
import org.codehaus.mojo.versions.api.ArtifactAssociation;
3233
import org.codehaus.mojo.versions.api.PropertyVersions;
34+
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
3335
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
3436

3537
/**
@@ -146,18 +148,26 @@ protected void update( ModifiedPomXMLEventReader pom )
146148
}
147149

148150
int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );
149-
ArtifactVersion targetVersion = updatePropertyToNewestVersion( pom, property, version, currentVersion,
150-
allowDowngrade, segment );
151-
152-
if ( targetVersion != null )
151+
try
153152
{
154-
for ( final ArtifactAssociation association : version.getAssociations() )
153+
ArtifactVersion targetVersion = updatePropertyToNewestVersion( pom, property, version, currentVersion,
154+
allowDowngrade, segment );
155+
156+
if ( targetVersion != null )
155157
{
156-
this.getChangeRecorder().recordUpdate( "updateProperty", association.getGroupId(),
157-
association.getArtifactId(), currentVersion,
158-
targetVersion.toString() );
158+
for ( final ArtifactAssociation association : version.getAssociations() )
159+
{
160+
this.getChangeRecorder().recordUpdate( "updateProperty", association.getGroupId(),
161+
association.getArtifactId(), currentVersion,
162+
targetVersion.toString() );
163+
}
159164
}
160165
}
166+
catch ( InvalidSegmentException | InvalidVersionSpecificationException e )
167+
{
168+
getLog().warn( String.format( "Skipping the processing of %s:%s due to: %s", property.getName(),
169+
property.getVersion(), e.getMessage() ) );
170+
}
161171
}
162172
}
163173

0 commit comments

Comments
 (0)