Skip to content

Commit a440adc

Browse files
andrzejj0slawekjaranowski
authored andcommitted
Implementing #213: new argument: allowDowngrade, only valid if allowSnapshots is false, will optionally downgrade the dependency version if its current version is a snapshot
1 parent 01dcc60 commit a440adc

File tree

5 files changed

+366
-91
lines changed

5 files changed

+366
-91
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@
264264
</dependency>
265265
<dependency>
266266
<groupId>org.mockito</groupId>
267-
<artifactId>mockito-core</artifactId>
267+
<artifactId>mockito-inline</artifactId>
268268
<version>4.7.0</version>
269269
<scope>test</scope>
270270
</dependency>

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@
2222
import javax.xml.stream.XMLStreamException;
2323

2424
import java.io.IOException;
25-
import java.util.ArrayList;
2625
import java.util.Collection;
27-
import java.util.List;
26+
import java.util.Collections;
2827

2928
import org.apache.maven.artifact.Artifact;
3029
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
@@ -75,8 +74,33 @@ public class UseLatestVersionsMojo
7574
@Parameter( property = "allowIncrementalUpdates", defaultValue = "true" )
7675
private boolean allowIncrementalUpdates;
7776

77+
/**
78+
* <p>Whether to downgrade a snapshot dependency if <code>allowSnapshots</code> is <code>false</code>
79+
* and there exists a non-snapshot version within the range fulfilling the criteria.</p>
80+
* <p>Only valid if <code>allowSnapshots</code> is <code>false</code>.</p>
81+
*
82+
* @since 2.12.0
83+
*/
84+
@Parameter( property = "allowDowngrade",
85+
defaultValue = "false" )
86+
private boolean allowDowngrade;
87+
7888
// ------------------------------ METHODS --------------------------
7989

90+
91+
/**
92+
* {@inheritDoc}
93+
*/
94+
@Override
95+
public void execute() throws MojoExecutionException, MojoFailureException
96+
{
97+
if ( allowDowngrade && allowSnapshots )
98+
{
99+
throw new MojoExecutionException( "allowDowngrade is only valid with allowSnapshots equal to false" );
100+
}
101+
super.execute();
102+
}
103+
80104
/**
81105
* @param pom the pom to update.
82106
* @throws org.apache.maven.plugin.MojoExecutionException when things go wrong
@@ -109,9 +133,7 @@ protected void update( ModifiedPomXMLEventReader pom )
109133
dependency.setGroupId( getProject().getParent().getGroupId() );
110134
dependency.setVersion( getProject().getParent().getVersion() );
111135
dependency.setType( "pom" );
112-
List list = new ArrayList();
113-
list.add( dependency );
114-
useLatestVersions( pom, list );
136+
useLatestVersions( pom, Collections.singletonList( dependency ) );
115137
}
116138
}
117139
catch ( ArtifactMetadataRetrievalException | IOException e )
@@ -154,7 +176,8 @@ private void useLatestVersions( ModifiedPomXMLEventReader pom, Collection<Depend
154176
getLog().debug( "Looking for newer versions of " + toString( dep ) );
155177
ArtifactVersions versions = getHelper().lookupArtifactVersions( artifact, false );
156178

157-
ArtifactVersion[] newerVersions = versions.getNewerVersions( version, segment, allowSnapshots );
179+
ArtifactVersion[] newerVersions = versions.getNewerVersions( version, segment, allowSnapshots,
180+
allowDowngrade );
158181

159182
ArtifactVersion[] filteredVersions = majorMinorIncfilter.filter( selectedVersion, newerVersions );
160183
if ( filteredVersions.length > 0 )

src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java

Lines changed: 84 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.maven.artifact.versioning.ArtifactVersion;
2727
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
2828
import org.apache.maven.artifact.versioning.VersionRange;
29+
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
2930
import org.codehaus.mojo.versions.ordering.VersionComparator;
3031

3132
/**
@@ -135,30 +136,6 @@ public final ArtifactVersion[] getVersions( ArtifactVersion currentVersion, Arti
135136
return getVersions( currentVersion, upperBound, includeSnapshots, false, false );
136137
}
137138

138-
/**
139-
* Gets newer versions of the specified artifact version.
140-
*
141-
* @param version The current version of the artifact.
142-
* @param upperBoundFixedSegment Indicates the segment in the version number that cannot be changed. For example, a
143-
* value of 0 indicates that the major version number cannot be changed. A value of -1 indicates any
144-
* segment value can be changed.
145-
* @param includeSnapshots Whether to include snapshot versions.
146-
* @return Returns the newer artifact versions.
147-
*/
148-
private ArtifactVersion[] getNewerVersions( ArtifactVersion version, int upperBoundFixedSegment,
149-
boolean includeSnapshots )
150-
{
151-
ArtifactVersion lowerBound = version;
152-
ArtifactVersion upperBound = null;
153-
154-
if ( upperBoundFixedSegment != -1 )
155-
{
156-
upperBound = getVersionComparator().incrementSegment( lowerBound, upperBoundFixedSegment );
157-
}
158-
159-
return getVersions( version, upperBound, includeSnapshots, false, false );
160-
}
161-
162139
private ArtifactVersion[] getNewerVersions( ArtifactVersion version, boolean includeSnapshots )
163140
{
164141
return getVersions( version, null, includeSnapshots, false, true );
@@ -243,9 +220,43 @@ public final ArtifactVersion[] getNewerVersions( String version, boolean include
243220
return getNewerVersions( new DefaultArtifactVersion( version ), includeSnapshots );
244221
}
245222

223+
/**
224+
* Returns an array of newer versions than the given version, given the upper bound segment and whether snapshots
225+
* should be included.
226+
*
227+
* @param version current version
228+
* @param upperBoundSegment the upper bound segment
229+
* @param includeSnapshots whether snapshot versions should be included
230+
* @deprecated please use {@link AbstractVersionDetails#getNewerVersions(String, int, boolean, boolean)} instead
231+
* @return array of newer versions fulfilling the criteria
232+
*/
233+
@Deprecated
246234
public final ArtifactVersion[] getNewerVersions( String version, int upperBoundSegment, boolean includeSnapshots )
247235
{
248-
return getNewerVersions( new DefaultArtifactVersion( version ), upperBoundSegment, includeSnapshots );
236+
return getNewerVersions( version, upperBoundSegment, includeSnapshots, false );
237+
}
238+
239+
/**
240+
* Returns an array of newer versions than the given version, given the upper bound segment and whether snapshots
241+
* should be included.
242+
*
243+
* @param versionString current version
244+
* @param upperBoundSegment the upper bound segment
245+
* @param includeSnapshots whether snapshot versions should be included
246+
* @param allowDowngrade whether to allow downgrading if the current version is a snapshots and snapshots
247+
* are disallowed
248+
* @return array of newer versions fulfilling the criteria
249+
*/
250+
public final ArtifactVersion[] getNewerVersions( String versionString, int upperBoundSegment,
251+
boolean includeSnapshots, boolean allowDowngrade )
252+
{
253+
ArtifactVersion currentVersion = new DefaultArtifactVersion( versionString );
254+
ArtifactVersion lowerBound =
255+
allowDowngrade ? getLowerBoundArtifactVersion( currentVersion, upperBoundSegment ) : currentVersion;
256+
ArtifactVersion upperBound = upperBoundSegment == -1 ? null
257+
: getVersionComparator().incrementSegment( lowerBound, upperBoundSegment );
258+
259+
return getVersions( lowerBound, upperBound, includeSnapshots, allowDowngrade, allowDowngrade );
249260
}
250261

251262
public final ArtifactVersion getOldestVersion( ArtifactVersion lowerBound, ArtifactVersion upperBound )
@@ -483,4 +494,52 @@ public ArtifactVersion[] getAllUpdates( VersionRange versionRange, boolean inclu
483494
{
484495
return getVersions( versionRange, getCurrentVersion(), null, includeSnapshots, false, true );
485496
}
497+
498+
protected ArtifactVersion getLowerBoundArtifactVersion( ArtifactVersion version, int segment )
499+
{
500+
String lowerBound = getLowerBound( version, segment );
501+
return lowerBound != null ? new DefaultArtifactVersion( lowerBound ) : null;
502+
}
503+
504+
protected String getLowerBound( ArtifactVersion version, int segment )
505+
{
506+
if ( segment < 0 )
507+
{
508+
return null;
509+
}
510+
511+
int segmentCount = getVersionComparator().getSegmentCount( version );
512+
if ( segment > segmentCount )
513+
{
514+
throw new InvalidSegmentException( segment, segmentCount,
515+
version.toString() );
516+
}
517+
518+
StringBuilder newVersion = new StringBuilder();
519+
newVersion.append( version.getMajorVersion() );
520+
if ( segmentCount > 0 )
521+
{
522+
newVersion.append( "." )
523+
.append( segment >= 1 ? version.getMinorVersion() : 0 );
524+
}
525+
if ( segmentCount > 1 )
526+
{
527+
newVersion.append( "." )
528+
.append( segment >= 2 ? version.getIncrementalVersion() : 0 );
529+
}
530+
if ( segmentCount > 2 )
531+
{
532+
if ( version.getQualifier() != null )
533+
{
534+
newVersion.append( "-" )
535+
.append( segment >= 3 ? version.getQualifier() : "0" );
536+
}
537+
else
538+
{
539+
newVersion.append( "-" )
540+
.append( segment >= 3 ? version.getBuildNumber() : "0" );
541+
}
542+
}
543+
return newVersion.toString();
544+
}
486545
}

src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.Arrays;
2424
import java.util.Collection;
2525
import java.util.Collections;
26-
import java.util.HashSet;
2726
import java.util.Iterator;
2827
import java.util.List;
2928
import java.util.Set;
@@ -39,7 +38,6 @@
3938
import org.apache.maven.artifact.versioning.VersionRange;
4039
import org.apache.maven.plugin.MojoExecutionException;
4140
import org.codehaus.mojo.versions.Property;
42-
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
4341
import org.codehaus.mojo.versions.ordering.VersionComparator;
4442

4543
/**
@@ -145,12 +143,8 @@ public ArtifactAssociation[] getAssociations()
145143

146144
private VersionComparator[] lookupComparators()
147145
{
148-
Set<VersionComparator> result = new HashSet();
149-
for ( ArtifactAssociation association : associations )
150-
{
151-
result.add( helper.getVersionComparator( association.getArtifact() ) );
152-
}
153-
return result.toArray( new VersionComparator[0] );
146+
return associations.stream().map( association -> helper.getVersionComparator( association.getArtifact() ) )
147+
.distinct().toArray( VersionComparator[]::new );
154148
}
155149

156150
/**
@@ -346,19 +340,15 @@ public ArtifactVersion getNewestVersion( String currentVersion, Property propert
346340
throw new MojoExecutionException( e.getMessage(), e );
347341
}
348342

349-
ArtifactVersion lowerBoundArtifactVersion = null;
343+
ArtifactVersion lowerBoundArtifactVersion = helper.createArtifactVersion( currentVersion );
350344
if ( allowDowngrade )
351345
{
352-
if ( segment != -1 )
353-
{
354-
lowerBoundArtifactVersion = getLowerBound( helper, currentVersion, segment );
355-
}
356-
helper.getLog().debug( "lowerBoundArtifactVersion is null based on allowDowngrade:" + allowDowngrade );
346+
String updatedVersion = getLowerBound( lowerBoundArtifactVersion, segment );
347+
lowerBoundArtifactVersion = updatedVersion != null ? helper.createArtifactVersion( updatedVersion ) : null;
357348
}
358-
else
349+
if ( helper.getLog().isDebugEnabled() )
359350
{
360-
lowerBoundArtifactVersion = helper.createArtifactVersion( currentVersion );
361-
helper.getLog().debug( "lowerBoundArtifactVersion: " + lowerBoundArtifactVersion.toString() );
351+
helper.getLog().debug( "lowerBoundArtifactVersion: " + lowerBoundArtifactVersion );
362352
}
363353

364354
ArtifactVersion upperBound = null;
@@ -375,7 +365,7 @@ public ArtifactVersion getNewestVersion( String currentVersion, Property propert
375365
if ( property.isSearchReactor() )
376366
{
377367
helper.getLog().debug( "Property ${" + property.getName() + "}: Searching reactor for a valid version..." );
378-
Collection reactorArtifacts = helper.extractArtifacts( reactorProjects );
368+
Set<Artifact> reactorArtifacts = helper.extractArtifacts( reactorProjects );
379369
ArtifactVersion[] reactorVersions = getVersions( reactorArtifacts );
380370
helper.getLog().debug( "Property ${" + property.getName()
381371
+ "}: Set of valid available versions from the reactor is " + Arrays.asList(
@@ -527,45 +517,4 @@ public ArtifactVersion incrementSegment( ArtifactVersion v, int segment )
527517
}
528518

529519
}
530-
531-
532-
private ArtifactVersion getLowerBound( VersionsHelper helper,
533-
String currentVersion, int segment )
534-
{
535-
ArtifactVersion version = helper.createArtifactVersion( currentVersion );
536-
int segmentCount = getVersionComparator().getSegmentCount( version );
537-
if ( segment < 0 || segment > segmentCount )
538-
{
539-
throw new InvalidSegmentException( segment, segmentCount,
540-
currentVersion );
541-
}
542-
543-
StringBuilder newVersion = new StringBuilder();
544-
newVersion.append( segment >= 0 ? version.getMajorVersion() : 0 );
545-
if ( segmentCount > 0 )
546-
{
547-
newVersion.append( "." )
548-
.append( segment >= 1 ? version.getMinorVersion() : 0 );
549-
}
550-
if ( segmentCount > 1 )
551-
{
552-
newVersion.append( "." )
553-
.append( segment >= 2 ? version.getIncrementalVersion() : 0 );
554-
}
555-
if ( segmentCount > 2 )
556-
{
557-
if ( version.getQualifier() != null )
558-
{
559-
newVersion.append( "-" )
560-
.append( segment >= 3 ? version.getQualifier() : "0" );
561-
}
562-
else
563-
{
564-
newVersion.append( "-" )
565-
.append( segment >= 3 ? version.getBuildNumber() : "0" );
566-
}
567-
}
568-
return helper.createArtifactVersion( newVersion.toString() );
569-
}
570-
571520
}

0 commit comments

Comments
 (0)