[MNG-7395] Support interpolation in extensions.xml

This adds support for property interpolation in extensions.xml to allow
advanced use cases where one wants to contribute certain things via the
commandline.

This closes #665
This commit is contained in:
Christoph Läubrich 2022-01-24 14:02:13 +01:00 committed by Michael Osipov
parent 62cf29bab3
commit 2798ee9196
1 changed files with 31 additions and 10 deletions

View File

@ -40,6 +40,10 @@ import org.codehaus.plexus.DefaultPlexusContainer;
import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.classworlds.ClassWorld; import org.codehaus.plexus.classworlds.ClassWorld;
import org.codehaus.plexus.classworlds.realm.ClassRealm; import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.codehaus.plexus.interpolation.InterpolationException;
import org.codehaus.plexus.interpolation.Interpolator;
import org.codehaus.plexus.interpolation.MapBasedValueSource;
import org.codehaus.plexus.interpolation.StringSearchInterpolator;
import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact; import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.graph.DependencyFilter; import org.eclipse.aether.graph.DependencyFilter;
@ -83,14 +87,16 @@ public class BootstrapCoreExtensionManager
{ {
RepositorySystemSession repoSession = repositorySystemSessionFactory.newRepositorySession( request ); RepositorySystemSession repoSession = repositorySystemSessionFactory.newRepositorySession( request );
List<RemoteRepository> repositories = RepositoryUtils.toRepos( request.getPluginArtifactRepositories() ); List<RemoteRepository> repositories = RepositoryUtils.toRepos( request.getPluginArtifactRepositories() );
Interpolator interpolator = createInterpolator( request );
return resolveCoreExtensions( repoSession, repositories, providedArtifacts, extensions ); return resolveCoreExtensions( repoSession, repositories, providedArtifacts, extensions, interpolator );
} }
private List<CoreExtensionEntry> resolveCoreExtensions( RepositorySystemSession repoSession, private List<CoreExtensionEntry> resolveCoreExtensions( RepositorySystemSession repoSession,
List<RemoteRepository> repositories, List<RemoteRepository> repositories,
Set<String> providedArtifacts, Set<String> providedArtifacts,
List<CoreExtension> configuration ) List<CoreExtension> configuration,
Interpolator interpolator )
throws Exception throws Exception
{ {
List<CoreExtensionEntry> extensions = new ArrayList<>(); List<CoreExtensionEntry> extensions = new ArrayList<>();
@ -99,7 +105,8 @@ public class BootstrapCoreExtensionManager
for ( CoreExtension extension : configuration ) for ( CoreExtension extension : configuration )
{ {
List<Artifact> artifacts = resolveExtension( extension, repoSession, repositories, dependencyFilter ); List<Artifact> artifacts = resolveExtension( extension, repoSession, repositories,
dependencyFilter, interpolator );
if ( !artifacts.isEmpty() ) if ( !artifacts.isEmpty() )
{ {
extensions.add( createExtension( extension, artifacts ) ); extensions.add( createExtension( extension, artifacts ) );
@ -127,18 +134,20 @@ public class BootstrapCoreExtensionManager
} }
private List<Artifact> resolveExtension( CoreExtension extension, RepositorySystemSession repoSession, private List<Artifact> resolveExtension( CoreExtension extension, RepositorySystemSession repoSession,
List<RemoteRepository> repositories, DependencyFilter dependencyFilter ) List<RemoteRepository> repositories, DependencyFilter dependencyFilter,
Interpolator interpolator )
throws ExtensionResolutionException throws ExtensionResolutionException
{ {
try try
{ {
// TODO: enhance the PluginDependenciesResolver to provide a /* TODO: Enhance the PluginDependenciesResolver to provide a
// TODO: resolveCoreExtension method which uses a CoreExtension * resolveCoreExtension method which uses a CoreExtension
// TODO: object instead of a Plugin as this makes no sense * object instead of a Plugin as this makes no sense.
*/
Plugin plugin = new Plugin(); Plugin plugin = new Plugin();
plugin.setGroupId( extension.getGroupId() ); plugin.setGroupId( interpolator.interpolate( extension.getGroupId() ) );
plugin.setArtifactId( extension.getArtifactId() ); plugin.setArtifactId( interpolator.interpolate( extension.getArtifactId() ) );
plugin.setVersion( extension.getVersion() ); plugin.setVersion( interpolator.interpolate( extension.getVersion() ) );
DependencyNode root = pluginDependenciesResolver DependencyNode root = pluginDependenciesResolver
.resolveCoreExtension( plugin, dependencyFilter, repositories, repoSession ); .resolveCoreExtension( plugin, dependencyFilter, repositories, repoSession );
@ -151,6 +160,18 @@ public class BootstrapCoreExtensionManager
{ {
throw new ExtensionResolutionException( extension, e.getCause() ); throw new ExtensionResolutionException( extension, e.getCause() );
} }
catch ( InterpolationException e )
{
throw new ExtensionResolutionException( extension, e );
}
}
private static Interpolator createInterpolator( MavenExecutionRequest request )
{
StringSearchInterpolator interpolator = new StringSearchInterpolator();
interpolator.addValueSource( new MapBasedValueSource( request.getUserProperties() ) );
interpolator.addValueSource( new MapBasedValueSource( request.getSystemProperties() ) );
return interpolator;
} }
} }