Skip to content

Commit

Permalink
Fixed mojohaus#318
Browse files Browse the repository at this point in the history
 o Added support to filter dependencies while doing `display-dependency-updates`

 Now you can do something like
 ```
 mvn versions:display-dependency-updates -Dincludes=org.springframework.boot
 ```
 Supports both `includes` and `excludes` options. Code is refactored to avoid duplications
  • Loading branch information
Carlos Tasada committed Oct 2, 2019
1 parent 28134b4 commit a462069
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 77 deletions.
Expand Up @@ -21,7 +21,6 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

Expand All @@ -48,10 +47,6 @@ public abstract class AbstractVersionsDependencyUpdaterMojo
extends AbstractVersionsUpdaterMojo
{

private static final String END_RANGE_CHARS = "])";

private static final String START_RANGE_CHARS = "[(";

/**
* A comma separated list of artifact patterns to include. Follows the pattern
* "groupId:artifactId:type:classifier:version". Designed to allow specifing the set of includes from the command
Expand Down Expand Up @@ -386,7 +381,7 @@ protected boolean isIncluded( Artifact artifact )

/**
* Indicates whether any includes were specified via the 'includes' or 'includesList' options.
*
*
* @return true if includes were specified, false otherwise.
*/
protected boolean hasIncludes()
Expand Down Expand Up @@ -429,75 +424,4 @@ else if ( excludes != null )
}
return excludesFilter;
}

/**
* To handle multiple includes with version range like "group:artifact:jar:[1.0.0,2.2)", we have to use a parsing a
* little bit more complex than split().
*
* @param includeString the string to parse
* @return list of patterns
*/
protected List<String> separatePatterns( String includeString )
{
if ( includeString == null )
{
return Collections.emptyList();
}

List<String> patterns = new ArrayList<>();
int indexOf = nextCommaIndex( includeString );
while ( indexOf >= 0 )
{
patterns.add( includeString.substring( 0, indexOf ) );
includeString = includeString.substring( indexOf + 1 );
indexOf = nextCommaIndex( includeString );
}
patterns.add( includeString );

return patterns;
}

private int nextCommaIndex( final String includeString )
{

int indexOfComma = includeString.indexOf( ',' );
int nextRangeStartDelimiterIndex = findFirstChar( includeString, START_RANGE_CHARS );
if ( nextRangeStartDelimiterIndex >= 0 )
{
if ( !( indexOfComma >= 0 && indexOfComma < nextRangeStartDelimiterIndex ) )
{
int nextStopDelimiterIndex = findFirstChar( includeString, END_RANGE_CHARS );

// recursive call
int tmp = nextCommaIndex( includeString.substring( nextStopDelimiterIndex + 1 ) );
indexOfComma = ( tmp >= 0 ) ? nextStopDelimiterIndex + 1 + tmp : -1;
}
}
return indexOfComma;

}

private int findFirstChar( final String includeString, final String chars )
{
int nextRangeStartDelimiterIndex = -1;

char[] delimiters = chars.toCharArray();
for ( int i = 0; i < delimiters.length; i++ )
{
int index = includeString.indexOf( delimiters[i] );
if ( index >= 0 && nextRangeStartDelimiterIndex >= 0 )
{
nextRangeStartDelimiterIndex = Math.min( index, nextRangeStartDelimiterIndex );
}
else
{
if ( index >= 0 )
{
nextRangeStartDelimiterIndex = index;
}
}
}
return nextRangeStartDelimiterIndex;
}

}
Expand Up @@ -39,6 +39,8 @@
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.path.PathTranslator;
import org.apache.maven.settings.Settings;
import org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter;
import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.DefaultVersionsHelper;
import org.codehaus.mojo.versions.api.PomHelper;
Expand All @@ -55,6 +57,8 @@
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -66,6 +70,10 @@ public abstract class AbstractVersionsUpdaterMojo
extends AbstractMojo
{

private static final String END_RANGE_CHARS = "])";

private static final String START_RANGE_CHARS = "[(";

// ------------------------------ FIELDS ------------------------------

/**
Expand Down Expand Up @@ -178,6 +186,16 @@ public abstract class AbstractVersionsUpdaterMojo
*/
private VersionsHelper helper;

/**
* Artifact filter to determine if artifact should be included
*/
private PatternIncludesArtifactFilter includesFilter;

/**
* Artifact filter to determine if artifact should be excluded
*/
private PatternExcludesArtifactFilter excludesFilter;

/**
* The Maven Session.
*
Expand Down Expand Up @@ -510,4 +528,74 @@ else if ( PomHelper.setPropertyVersion( pom, version.getProfileId(), property.ge
getLog().info( "Updated ${" + property.getName() + "} from " + currentVersion + " to " + winner );
}
}

/**
* To handle multiple includes with version range like "group:artifact:jar:[1.0.0,2.2)", we have to use a parsing a
* little bit more complex than split().
*
* @param includeString the string to parse
* @return list of patterns
*/
protected List<String> separatePatterns( String includeString )
{
if ( includeString == null )
{
return Collections.emptyList();
}

List<String> patterns = new ArrayList<>();
int indexOf = nextCommaIndex( includeString );
while ( indexOf >= 0 )
{
patterns.add( includeString.substring( 0, indexOf ) );
includeString = includeString.substring( indexOf + 1 );
indexOf = nextCommaIndex( includeString );
}
patterns.add( includeString );

return patterns;
}

private int nextCommaIndex( final String includeString )
{

int indexOfComma = includeString.indexOf( ',' );
int nextRangeStartDelimiterIndex = findFirstChar( includeString, START_RANGE_CHARS );
if ( nextRangeStartDelimiterIndex >= 0 )
{
if ( !( indexOfComma >= 0 && indexOfComma < nextRangeStartDelimiterIndex ) )
{
int nextStopDelimiterIndex = findFirstChar( includeString, END_RANGE_CHARS );

// recursive call
int tmp = nextCommaIndex( includeString.substring( nextStopDelimiterIndex + 1 ) );
indexOfComma = ( tmp >= 0 ) ? nextStopDelimiterIndex + 1 + tmp : -1;
}
}
return indexOfComma;

}

private int findFirstChar( final String includeString, final String chars )
{
int nextRangeStartDelimiterIndex = -1;

char[] delimiters = chars.toCharArray();
for ( int i = 0; i < delimiters.length; i++ )
{
int index = includeString.indexOf( delimiters[i] );
if ( index >= 0 && nextRangeStartDelimiterIndex >= 0 )
{
nextRangeStartDelimiterIndex = Math.min( index, nextRangeStartDelimiterIndex );
}
else
{
if ( index >= 0 )
{
nextRangeStartDelimiterIndex = index;
}
}
}
return nextRangeStartDelimiterIndex;
}
}

0 comments on commit a462069

Please sign in to comment.