Skip to content

Commit

Permalink
Remove old and deprecated items, use Resolver API (#336)
Browse files Browse the repository at this point in the history
* Make up some years of debt

As part of user reported issue https://issues.apache.org/jira/browse/MRESOLVER-322 and
for upcoming 1.9.5 resolver release went to look what it is about, only to figure out
that this plugin accumulate several terrible lag, bad practice and many other things.

Whenever you see "inappropriate dependency" (ie. using MAT in plugin that declares
itself as a 3.2.5+ plugin), use of deprecated components (ArtifactFactory and
relatives), or doing hoops and loops to get dependnecy graph ONLY to do
something about it, step up and fix it.

These changes makes plugin work as expected from declared "minimum" version
(3.2.5) to current stable (3.9.0) with addition that it will support
split repository and any other modern feature incoming....

Co-authored-by: Slawomir Jaranowski <s.jaranowski@gmail.com>
  • Loading branch information
cstamas and slawekjaranowski committed Mar 17, 2023
1 parent 1f0e61d commit 5b757ae
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 334 deletions.
11 changes: 3 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,20 +185,15 @@
</dependency>

<!-- other -->
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-dependency-tree</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-artifact-transfer</artifactId>
<version>0.13.1</version>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-interpolation</artifactId>
<version>1.26</version>
</dependency>

<!-- testing -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Test fails on Maven < 3.5.2
invoker.maven.version = 3.5.2+
262 changes: 135 additions & 127 deletions src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,29 @@
import org.codehaus.plexus.interpolation.SimpleRecursionInterceptor;
import org.codehaus.plexus.interpolation.ValueSource;

/*
/**
* Based on StringSearchInterpolator from plexus-interpolation,
* see {@link org.codehaus.plexus.interpolation.StringSearchInterpolator}.
* This interpolates only the Maven CI Friendly variables revision, sha1 and changelist.
*/
public class CiInterpolatorImpl implements Interpolator
{

private Map existingAnswers = new HashMap();
private final Map existingAnswers = new HashMap();

private List<ValueSource> valueSources = new ArrayList<>();
private final List<ValueSource> valueSources = new ArrayList<>();

private List<InterpolationPostProcessor> postProcessors = new ArrayList<>();
private final List<InterpolationPostProcessor> postProcessors = new ArrayList<>();

private boolean cacheAnswers = false;

public static final String DEFAULT_START_EXPR = "${";

public static final String DEFAULT_END_EXPR = "}";

private String startExpr;

private String endExpr;
private final String startExpr;

private String escapeString;
private final String endExpr;

public CiInterpolatorImpl()
{
Expand All @@ -76,6 +74,7 @@ public CiInterpolatorImpl( String startExpr, String endExpr )
/**
* {@inheritDoc}
*/
@Override
public void addValueSource( ValueSource valueSource )
{
valueSources.add( valueSource );
Expand All @@ -84,6 +83,7 @@ public void addValueSource( ValueSource valueSource )
/**
* {@inheritDoc}
*/
@Override
public void removeValuesSource( ValueSource valueSource )
{
valueSources.remove( valueSource );
Expand All @@ -92,6 +92,7 @@ public void removeValuesSource( ValueSource valueSource )
/**
* {@inheritDoc}
*/
@Override
public void addPostProcessor( InterpolationPostProcessor postProcessor )
{
postProcessors.add( postProcessor );
Expand All @@ -100,23 +101,27 @@ public void addPostProcessor( InterpolationPostProcessor postProcessor )
/**
* {@inheritDoc}
*/
@Override
public void removePostProcessor( InterpolationPostProcessor postProcessor )
{
postProcessors.remove( postProcessor );
}

@Override
public String interpolate( String input, String thisPrefixPattern )
throws InterpolationException
{
return interpolate( input, new SimpleRecursionInterceptor() );
}

@Override
public String interpolate( String input, String thisPrefixPattern, RecursionInterceptor recursionInterceptor )
throws InterpolationException
{
return interpolate( input, recursionInterceptor );
}

@Override
public String interpolate( String input )
throws InterpolationException
{
Expand All @@ -127,6 +132,7 @@ public String interpolate( String input )
* Entry point for recursive resolution of an expression and all of its
* nested expressions.
*/
@Override
public String interpolate( String input, RecursionInterceptor recursionInterceptor )
throws InterpolationException
{
Expand Down Expand Up @@ -168,21 +174,6 @@ private String interpolate( String input, RecursionInterceptor recursionIntercep
final String wholeExpr = input.substring( startIdx, endIdx + endExpr.length() );
String realExpr = wholeExpr.substring( startExpr.length(), wholeExpr.length() - endExpr.length() );

if ( startIdx >= 0 && escapeString != null && escapeString.length() > 0 )
{
int startEscapeIdx = startIdx == 0 ? 0 : startIdx - escapeString.length();
if ( startEscapeIdx >= 0 )
{
String escape = input.substring( startEscapeIdx, startIdx );
if ( escape != null && escapeString.equals( escape ) )
{
result.append( wholeExpr );
result.replace( startEscapeIdx, startEscapeIdx + escapeString.length(), "" );
continue;
}
}
}

boolean resolved = false;
if ( !unresolvable.contains( wholeExpr ) )
{
Expand Down Expand Up @@ -235,7 +226,7 @@ private String interpolate( String input, RecursionInterceptor recursionIntercep
{
value = interpolate( String.valueOf( value ), recursionInterceptor, unresolvable );

if ( postProcessors != null && !postProcessors.isEmpty() )
if ( !postProcessors.isEmpty() )
{
for ( InterpolationPostProcessor postProcessor : postProcessors )
{
Expand All @@ -252,7 +243,7 @@ private String interpolate( String input, RecursionInterceptor recursionIntercep
// result = matcher.replaceFirst( stringValue );
// but this could result in multiple lookups of stringValue, and replaceAll is not correct
// behaviour
result.append( String.valueOf( value ) );
result.append( value );
resolved = true;
}
else
Expand All @@ -271,10 +262,7 @@ private String interpolate( String input, RecursionInterceptor recursionIntercep
result.append( wholeExpr );
}

if ( endIdx > -1 )
{
endIdx += endExpr.length() - 1;
}
endIdx += endExpr.length() - 1;
}

if ( endIdx == -1 && startIdx > -1 )
Expand All @@ -298,6 +286,7 @@ else if ( endIdx < input.length() )
* @return a {@link List} that may be interspersed with {@link String} and
* {@link Throwable} instances.
*/
@Override
public List getFeedback()
{
List<?> messages = new ArrayList();
Expand All @@ -316,6 +305,7 @@ public List getFeedback()
/**
* Clear the feedback messages from previous interpolate(..) calls.
*/
@Override
public void clearFeedback()
{
for ( ValueSource vs : valueSources )
Expand All @@ -324,30 +314,23 @@ public void clearFeedback()
}
}

@Override
public boolean isCacheAnswers()
{
return cacheAnswers;
}

@Override
public void setCacheAnswers( boolean cacheAnswers )
{
this.cacheAnswers = cacheAnswers;
}

@Override
public void clearAnswers()
{
existingAnswers.clear();
}

public String getEscapeString()
{
return escapeString;
}

public void setEscapeString( String escapeString )
{
this.escapeString = escapeString;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -47,8 +45,6 @@
import org.apache.maven.model.interpolation.ModelInterpolator;
import org.apache.maven.model.path.PathTranslator;
import org.apache.maven.model.path.UrlNormalizer;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.interpolation.AbstractValueSource;
import org.codehaus.plexus.interpolation.InterpolationException;
import org.codehaus.plexus.interpolation.InterpolationPostProcessor;
Expand All @@ -62,23 +58,22 @@
import org.codehaus.plexus.interpolation.ValueSource;
import org.codehaus.plexus.interpolation.util.ValueSourceUtils;

/*
* Based on StringSearchModelInterpolator in maven-model-builder
/**
* Based on StringSearchModelInterpolator in maven-model-builder.
*
* IMPORTANT: this is a legacy Plexus component, with manually authored descriptor in src/main/resources!
*/
@Component( role = CiInterpolator.class )
public class CiModelInterpolator implements CiInterpolator, ModelInterpolator
{

public CiModelInterpolator()
{
interpolator = createInterpolator();
recursionInterceptor = new PrefixAwareRecursionInterceptor( PROJECT_PREFIXES );
}

private static final List<String> PROJECT_PREFIXES = Arrays.asList( "pom.", "project." );

private static final Collection<String> TRANSLATED_PATH_EXPRESSIONS;

private static final Map<Class<?>, InterpolateObjectAction.CacheItem> CACHED_ENTRIES = new ConcurrentHashMap<>(
80, 0.75f, 2 );
// Empirical data from 3.x, actual =40

static
{
Collection<String> translatedPrefixes = new HashSet<>();
Expand All @@ -101,18 +96,20 @@ public CiModelInterpolator()
TRANSLATED_PATH_EXPRESSIONS = translatedPrefixes;
}

private Interpolator interpolator;
private final Interpolator interpolator;

// There is a protected setter for this one?
private RecursionInterceptor recursionInterceptor;

@Requirement
private PathTranslator pathTranslator;

@Requirement
private UrlNormalizer urlNormalizer;

private static final Map<Class<?>, InterpolateObjectAction.CacheItem> CACHED_ENTRIES = new ConcurrentHashMap<>(
80, 0.75f, 2 );
// Empirical data from 3.x, actual =40
public CiModelInterpolator()
{
this.interpolator = createInterpolator();
this.recursionInterceptor = new PrefixAwareRecursionInterceptor( PROJECT_PREFIXES );
}

public Model interpolateModel( Model model, File projectDir, ModelBuildingRequest config,
ModelProblemCollector problems )
Expand All @@ -131,10 +128,7 @@ protected void interpolateObject( Object obj, Model model, File projectDir, Mode
List<? extends InterpolationPostProcessor> postProcessors =
createPostProcessors( model, projectDir, config );

InterpolateObjectAction action = new InterpolateObjectAction( obj, valueSources, postProcessors, this,
problems );

AccessController.doPrivileged( action );
new InterpolateObjectAction( obj, valueSources, postProcessors, this, problems ).run();
}
finally
{
Expand Down Expand Up @@ -205,7 +199,7 @@ protected Interpolator createInterpolator()
return interpolator;
}

private static final class InterpolateObjectAction implements PrivilegedAction<Object>
private static final class InterpolateObjectAction implements Runnable
{

private final LinkedList<Object> interpolationTargets;
Expand Down Expand Up @@ -234,16 +228,15 @@ private static final class InterpolateObjectAction implements PrivilegedAction<O
this.problems = problems;
}

public Object run()
@Override
public void run()
{
while ( !interpolationTargets.isEmpty() )
{
Object obj = interpolationTargets.removeFirst();

traverseObjectWithParents( obj.getClass(), obj );
}

return null;
}

private String interpolate( String value )
Expand Down

0 comments on commit 5b757ae

Please sign in to comment.