[MNG-7103] VersionScheme provider (#563)

This PR makes VersionScheme a component, is injected where needed
(instead of ad-hoc instantiation), but provides room for
different schemas, as GenericVersionScheme is "default"
but now nothing stops to add other schemes as well.
This commit is contained in:
Tamas Cservenak 2021-10-04 11:55:18 +02:00 committed by GitHub
parent 7934f73f28
commit 69ee0c8b59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 118 additions and 56 deletions

View File

@ -59,7 +59,6 @@ import org.eclipse.aether.repository.ArtifactRepository;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.MetadataRequest;
import org.eclipse.aether.resolution.MetadataResult;
import org.eclipse.aether.util.version.GenericVersionScheme;
import org.eclipse.aether.version.InvalidVersionSpecificationException;
import org.eclipse.aether.version.Version;
import org.eclipse.aether.version.VersionScheme;
@ -85,18 +84,22 @@ public class DefaultPluginVersionResolver
private final RepositorySystem repositorySystem;
private final MetadataReader metadataReader;
private final MavenPluginManager pluginManager;
private final VersionScheme versionScheme;
@Inject
public DefaultPluginVersionResolver(
RepositorySystem repositorySystem,
MetadataReader metadataReader,
MavenPluginManager pluginManager )
MavenPluginManager pluginManager,
VersionScheme versionScheme )
{
this.repositorySystem = repositorySystem;
this.metadataReader = metadataReader;
this.pluginManager = pluginManager;
this.versionScheme = versionScheme;
}
@Override
public PluginVersionResult resolve( PluginVersionRequest request )
throws PluginVersionResolutionException
{
@ -199,8 +202,6 @@ public class DefaultPluginVersionResolver
if ( version == null )
{
VersionScheme versionScheme = new GenericVersionScheme();
TreeSet<Version> releases = new TreeSet<>( Collections.reverseOrder() );
TreeSet<Version> snapshots = new TreeSet<>( Collections.reverseOrder() );

View File

@ -22,7 +22,6 @@ package org.apache.maven.rtinfo.internal;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.apache.maven.rtinfo.RuntimeInformation;
import org.eclipse.aether.util.version.GenericVersionScheme;
import org.eclipse.aether.version.InvalidVersionSpecificationException;
import org.eclipse.aether.version.Version;
import org.eclipse.aether.version.VersionConstraint;
@ -34,6 +33,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
@ -47,60 +47,69 @@ public class DefaultRuntimeInformation
{
private final Logger logger = LoggerFactory.getLogger( getClass() );
private String mavenVersion;
private final VersionScheme versionScheme;
private final String mavenVersion;
@Inject
public DefaultRuntimeInformation( final VersionScheme versionScheme )
{
this.versionScheme = versionScheme;
this.mavenVersion = loadMavenVersion();
}
@Override
public String getMavenVersion()
{
if ( mavenVersion == null )
{
Properties props = new Properties();
String resource = "META-INF/maven/org.apache.maven/maven-core/pom.properties";
try ( InputStream is = DefaultRuntimeInformation.class.getResourceAsStream( "/" + resource ) )
{
if ( is != null )
{
props.load( is );
}
else
{
logger.warn(
"Could not locate " + resource + " on classpath, Maven runtime information not available" );
}
}
catch ( IOException e )
{
String msg = "Could not parse " + resource + ", Maven runtime information not available";
if ( logger.isDebugEnabled() )
{
logger.warn( msg, e );
}
else
{
logger.warn( msg );
}
}
String version = props.getProperty( "version", "" ).trim();
if ( !version.startsWith( "${" ) )
{
mavenVersion = version;
}
else
{
mavenVersion = "";
}
}
return mavenVersion;
}
private String loadMavenVersion()
{
Properties props = new Properties();
String resource = "META-INF/maven/org.apache.maven/maven-core/pom.properties";
try ( InputStream is = DefaultRuntimeInformation.class.getResourceAsStream( "/" + resource ) )
{
if ( is != null )
{
props.load( is );
}
else
{
logger.warn(
"Could not locate " + resource + " on classpath, Maven runtime information not available" );
}
}
catch ( IOException e )
{
String msg = "Could not parse " + resource + ", Maven runtime information not available";
if ( logger.isDebugEnabled() )
{
logger.warn( msg, e );
}
else
{
logger.warn( msg );
}
}
String version = props.getProperty( "version", "" ).trim();
if ( !version.startsWith( "${" ) )
{
return version;
}
else
{
return "";
}
}
@Override
public boolean isMavenVersion( String versionRange )
{
VersionScheme versionScheme = new GenericVersionScheme();
Validate.notBlank( versionRange, "versionRange can neither be null, empty nor blank" );
VersionConstraint constraint;

View File

@ -40,7 +40,6 @@ import org.eclipse.aether.resolution.VersionRangeRequest;
import org.eclipse.aether.resolution.VersionRangeResolutionException;
import org.eclipse.aether.resolution.VersionRangeResult;
import org.eclipse.aether.spi.synccontext.SyncContextFactory;
import org.eclipse.aether.util.version.GenericVersionScheme;
import org.eclipse.aether.version.InvalidVersionSpecificationException;
import org.eclipse.aether.version.Version;
import org.eclipse.aether.version.VersionConstraint;
@ -73,23 +72,25 @@ public class DefaultVersionRangeResolver
private final MetadataResolver metadataResolver;
private final SyncContextFactory syncContextFactory;
private final RepositoryEventDispatcher repositoryEventDispatcher;
private final VersionScheme versionScheme;
@Inject
public DefaultVersionRangeResolver( MetadataResolver metadataResolver, SyncContextFactory syncContextFactory,
RepositoryEventDispatcher repositoryEventDispatcher )
public DefaultVersionRangeResolver( MetadataResolver metadataResolver,
SyncContextFactory syncContextFactory,
RepositoryEventDispatcher repositoryEventDispatcher,
VersionScheme versionScheme )
{
this.metadataResolver = Objects.requireNonNull( metadataResolver, "metadataResolver cannot be null" );
this.syncContextFactory = Objects.requireNonNull( syncContextFactory, "syncContextFactory cannot be null" );
this.repositoryEventDispatcher = Objects.requireNonNull( repositoryEventDispatcher,
"repositoryEventDispatcher cannot be null" );
this.versionScheme = Objects.requireNonNull( versionScheme, "versionScheme cannot be null" );
}
public VersionRangeResult resolveVersionRange( RepositorySystemSession session, VersionRangeRequest request )
throws VersionRangeResolutionException
{
VersionRangeResult result = new VersionRangeResult( request );
VersionScheme versionScheme = new GenericVersionScheme();
VersionConstraint versionConstraint;
try
{

View File

@ -0,0 +1,49 @@
package org.apache.maven.repository.internal;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.eclipse.aether.util.version.GenericVersionScheme;
import org.eclipse.aether.version.VersionScheme;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;
/**
* Default version scheme provider: provides singleton {@link GenericVersionScheme} instance.
*/
@Singleton
@Named
public final class DefaultVersionSchemeProvider
implements Provider<VersionScheme>
{
private final GenericVersionScheme genericVersionScheme;
public DefaultVersionSchemeProvider()
{
this.genericVersionScheme = new GenericVersionScheme();
}
@Override
public VersionScheme get()
{
return genericVersionScheme;
}
}

View File

@ -34,6 +34,7 @@ import org.eclipse.aether.impl.MetadataGeneratorFactory;
import org.eclipse.aether.impl.VersionRangeResolver;
import org.eclipse.aether.impl.VersionResolver;
import org.eclipse.aether.impl.guice.AetherModule;
import org.eclipse.aether.version.VersionScheme;
/**
* MavenResolverModule
@ -46,6 +47,7 @@ public final class MavenResolverModule
protected void configure()
{
install( new AetherModule() );
bind( VersionScheme.class ).toProvider( new DefaultVersionSchemeProvider() );
bind( ArtifactDescriptorReader.class ).to( DefaultArtifactDescriptorReader.class ).in( Singleton.class );
bind( VersionResolver.class ).to( DefaultVersionResolver.class ).in( Singleton.class );
bind( VersionRangeResolver.class ).to( DefaultVersionRangeResolver.class ).in( Singleton.class );