Skip to content

Commit

Permalink
[MNG-7974] Update Resolver 2.0.0-alpha-5 (#1337)
Browse files Browse the repository at this point in the history
Update to Resolver alpha-5 and apply some cleanups.

Notable changes:
* update resolver to 2.0.0-alpha-5
* detach model-builder from maven-artifact (this is important)
* introduce model builder own VersionParser iface (implemented in resolver-provider)
* API VersionParser implementation reuses VersionParser from resolver-provider to implement the service
* various other cleanups, removal of old plexus, etc

---

https://issues.apache.org/jira/browse/MNG-7974
  • Loading branch information
cstamas authored Dec 18, 2023
1 parent e34afc8 commit a1fdd89
Show file tree
Hide file tree
Showing 13 changed files with 413 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,42 @@
*/
package org.apache.maven.internal.impl;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import java.util.Objects;
import java.util.regex.Pattern;

import org.apache.maven.api.Version;
import org.apache.maven.api.VersionRange;
import org.apache.maven.api.services.VersionParser;
import org.apache.maven.api.services.VersionParserException;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;

import static org.apache.maven.artifact.versioning.VersionRange.createFromVersionSpec;
import static org.apache.maven.internal.impl.Utils.nonNull;

/**
* A wrapper class around a resolver version that works as model version parser as well.
*/
@Named
@Singleton
public class DefaultVersionParser implements VersionParser {
private static final String SNAPSHOT = "SNAPSHOT";
private static final Pattern SNAPSHOT_TIMESTAMP = Pattern.compile("^(.*-)?([0-9]{8}\\.[0-9]{6}-[0-9]+)$");

private final org.apache.maven.model.version.VersionParser modelVersionParser;

@Inject
public DefaultVersionParser(org.apache.maven.model.version.VersionParser modelVersionParser) {
this.modelVersionParser = nonNull(modelVersionParser, "modelVersionParser");
}

@Override
public Version parseVersion(String version) {
return new DefaultVersion(new DefaultArtifactVersion(nonNull(version, "version")));
return modelVersionParser.parseVersion(version);
}

@Override
public VersionRange parseVersionRange(String range) {
try {
return new DefaultVersionRange(createFromVersionSpec(nonNull(range, "version")));
} catch (InvalidVersionSpecificationException e) {
throw new VersionParserException("Unable to parse version range: " + range, e);
}
return modelVersionParser.parseVersionRange(range);
}

@Override
Expand All @@ -63,75 +64,4 @@ public boolean isSnapshot(String version) {
static boolean checkSnapshot(String version) {
return version.endsWith(SNAPSHOT) || SNAPSHOT_TIMESTAMP.matcher(version).matches();
}

static class DefaultVersion implements Version {
private final ArtifactVersion delegate;

DefaultVersion(ArtifactVersion delegate) {
this.delegate = delegate;
}

@Override
public int compareTo(Version o) {
if (o instanceof DefaultVersion) {
return delegate.compareTo(((DefaultVersion) o).delegate);
} else {
return delegate.compareTo(new DefaultArtifactVersion(o.toString()));
}
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DefaultVersion that = (DefaultVersion) o;
return delegate.equals(that.delegate);
}

@Override
public int hashCode() {
return Objects.hash(delegate);
}

@Override
public String asString() {
return delegate.toString();
}

@Override
public String toString() {
return asString();
}
}

static class DefaultVersionRange implements VersionRange {
private final org.apache.maven.artifact.versioning.VersionRange delegate;

DefaultVersionRange(org.apache.maven.artifact.versioning.VersionRange delegate) {
this.delegate = delegate;
}

@Override
public boolean contains(Version version) {
if (version instanceof DefaultVersion) {
return delegate.containsVersion(((DefaultVersion) version).delegate);
} else {
return delegate.containsVersion(new DefaultArtifactVersion(version.toString()));
}
}

@Override
public String asString() {
return delegate.toString();
}

@Override
public String toString() {
return asString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;

import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -62,35 +63,82 @@
import org.apache.maven.model.superpom.SuperPomProvider;
import org.apache.maven.model.v4.MavenModelVersion;
import org.apache.maven.model.validation.ModelValidator;
import org.apache.maven.model.version.VersionParser;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.project.ProjectModelResolver;
import org.apache.maven.repository.internal.ModelCacheFactory;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.RequestTrace;
import org.eclipse.aether.impl.RemoteRepositoryManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named
class DefaultConsumerPomBuilder implements ConsumerPomBuilder {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultConsumerPomBuilder.class);

private static final String BOM_PACKAGING = "bom";

public static final String POM_PACKAGING = "pom";

@Inject
PlexusContainer container;
private ModelCacheFactory modelCacheFactory;

@Inject
private ProfileInjector profileInjector;

@Inject
private InheritanceAssembler inheritanceAssembler;

@Inject
private DependencyManagementImporter dependencyManagementImporter;

@Inject
private DependencyManagementInjector dependencyManagementInjector;

@Inject
private LifecycleBindingsInjector lifecycleBindingsInjector;

@Inject
private ModelInterpolator modelInterpolator;

@Inject
private ModelNormalizer modelNormalizer;

@Inject
private ModelPathTranslator modelPathTranslator;

@Inject
private ModelProcessor modelProcessor;

@Inject
ModelCacheFactory modelCacheFactory;
private ModelUrlNormalizer modelUrlNormalizer;

public Model build(RepositorySystemSession session, MavenProject project, Path src)
throws ModelBuildingException, ComponentLookupException {
@Inject
private ModelValidator modelValidator;

@Inject
private PluginConfigurationExpander pluginConfigurationExpander;

@Inject
private PluginManagementInjector pluginManagementInjector;

@Inject
private ReportConfigurationExpander reportConfigurationExpander;

@Inject
private SuperPomProvider superPomProvider;

@Inject
private VersionParser versionParser;

// To break circular dependency
@Inject
private Provider<RepositorySystem> repositorySystem;

@Inject
private RemoteRepositoryManager remoteRepositoryManager;

@Override
public Model build(RepositorySystemSession session, MavenProject project, Path src) throws ModelBuildingException {
Model model = project.getModel().getDelegate();
String packaging = model.getPackaging();
String originalPackaging = project.getOriginalModel().getPackaging();
Expand All @@ -102,21 +150,21 @@ public Model build(RepositorySystemSession session, MavenProject project, Path s
}

protected Model buildPom(RepositorySystemSession session, MavenProject project, Path src)
throws ModelBuildingException, ComponentLookupException {
throws ModelBuildingException {
ModelBuildingResult result = buildModel(session, project, src);
Model model = result.getRawModel().getDelegate();
return transform(model, project);
}

protected Model buildNonPom(RepositorySystemSession session, MavenProject project, Path src)
throws ModelBuildingException, ComponentLookupException {
throws ModelBuildingException {
ModelBuildingResult result = buildModel(session, project, src);
Model model = result.getEffectiveModel().getDelegate();
return transform(model, project);
}

private ModelBuildingResult buildModel(RepositorySystemSession session, MavenProject project, Path src)
throws ModelBuildingException, ComponentLookupException {
throws ModelBuildingException {
ProfileSelector customSelector = new DefaultProfileSelector() {
@Override
public List<Profile> getActiveProfilesV4(
Expand All @@ -127,21 +175,22 @@ public List<Profile> getActiveProfilesV4(
DefaultModelBuilder modelBuilder = new DefaultModelBuilderFactory()
.setProfileSelector(customSelector)
// apply currently active ModelProcessor etc. to support extensions like jgitver
.setProfileInjector(lookup(ProfileInjector.class))
.setInheritanceAssembler(lookup(InheritanceAssembler.class))
.setDependencyManagementImporter(lookup(DependencyManagementImporter.class))
.setDependencyManagementInjector(lookup(DependencyManagementInjector.class))
.setLifecycleBindingsInjector(lookup(LifecycleBindingsInjector.class))
.setModelInterpolator(lookup(ModelInterpolator.class))
.setModelNormalizer(lookup(ModelNormalizer.class))
.setModelPathTranslator(lookup(ModelPathTranslator.class))
.setModelProcessor(lookup(ModelProcessor.class))
.setModelUrlNormalizer(lookup(ModelUrlNormalizer.class))
.setModelValidator(lookup(ModelValidator.class))
.setPluginConfigurationExpander(lookup(PluginConfigurationExpander.class))
.setPluginManagementInjector(lookup(PluginManagementInjector.class))
.setReportConfigurationExpander(lookup(ReportConfigurationExpander.class))
.setSuperPomProvider(lookup(SuperPomProvider.class))
.setProfileInjector(profileInjector)
.setInheritanceAssembler(inheritanceAssembler)
.setDependencyManagementImporter(dependencyManagementImporter)
.setDependencyManagementInjector(dependencyManagementInjector)
.setLifecycleBindingsInjector(lifecycleBindingsInjector)
.setModelInterpolator(modelInterpolator)
.setModelNormalizer(modelNormalizer)
.setModelPathTranslator(modelPathTranslator)
.setModelProcessor(modelProcessor)
.setModelUrlNormalizer(modelUrlNormalizer)
.setModelValidator(modelValidator)
.setPluginConfigurationExpander(pluginConfigurationExpander)
.setPluginManagementInjector(pluginManagementInjector)
.setReportConfigurationExpander(reportConfigurationExpander)
.setSuperPomProvider(superPomProvider)
.setModelVersionParser(versionParser)
.newInstance();
DefaultModelBuildingRequest request = new DefaultModelBuildingRequest();
try {
Expand All @@ -155,8 +204,8 @@ public List<Profile> getActiveProfilesV4(
request.setModelResolver(new ProjectModelResolver(
session,
new RequestTrace(null),
lookup(RepositorySystem.class),
lookup(RemoteRepositoryManager.class),
repositorySystem.get(),
remoteRepositoryManager,
project.getRemoteProjectRepositories(),
ProjectBuildingRequest.RepositoryMerging.POM_DOMINANT,
null));
Expand All @@ -173,10 +222,6 @@ private Properties toProperties(Map<String, String> map) {
return props;
}

private <T> T lookup(Class<T> clazz) throws ComponentLookupException {
return container.lookup(clazz);
}

static Model transform(Model model, MavenProject project) {
String packaging = model.getPackaging();
if (POM_PACKAGING.equals(packaging)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
import org.apache.maven.project.ExtensionDescriptor;
import org.apache.maven.project.ExtensionDescriptorBuilder;
import org.apache.maven.project.MavenProject;
import org.apache.maven.rtinfo.RuntimeInformation;
import org.apache.maven.session.scope.internal.SessionScopeModule;
import org.codehaus.plexus.DefaultPlexusContainer;
import org.codehaus.plexus.PlexusContainer;
Expand Down Expand Up @@ -134,19 +133,18 @@ public class DefaultMavenPluginManager implements MavenPluginManager {

private final Logger logger = LoggerFactory.getLogger(getClass());

private PlexusContainer container;
private ClassRealmManager classRealmManager;
private PluginDescriptorCache pluginDescriptorCache;
private PluginRealmCache pluginRealmCache;
private DefaultPluginDependenciesResolver pluginDependenciesResolver;
private RuntimeInformation runtimeInformation;
private ExtensionRealmCache extensionRealmCache;
private PluginVersionResolver pluginVersionResolver;
private PluginArtifactsCache pluginArtifactsCache;
private MavenPluginValidator pluginValidator;
private List<MavenPluginConfigurationValidator> configurationValidators;
private PluginValidationManager pluginValidationManager;
private List<MavenPluginPrerequisitesChecker> prerequisitesCheckers;
private final PlexusContainer container;
private final ClassRealmManager classRealmManager;
private final PluginDescriptorCache pluginDescriptorCache;
private final PluginRealmCache pluginRealmCache;
private final DefaultPluginDependenciesResolver pluginDependenciesResolver;
private final ExtensionRealmCache extensionRealmCache;
private final PluginVersionResolver pluginVersionResolver;
private final PluginArtifactsCache pluginArtifactsCache;
private final MavenPluginValidator pluginValidator;
private final List<MavenPluginConfigurationValidator> configurationValidators;
private final PluginValidationManager pluginValidationManager;
private final List<MavenPluginPrerequisitesChecker> prerequisitesCheckers;
private final ExtensionDescriptorBuilder extensionDescriptorBuilder = new ExtensionDescriptorBuilder();
private final PluginDescriptorBuilder builder = new PluginDescriptorBuilder();

Expand All @@ -158,7 +156,6 @@ public DefaultMavenPluginManager(
PluginDescriptorCache pluginDescriptorCache,
PluginRealmCache pluginRealmCache,
DefaultPluginDependenciesResolver pluginDependenciesResolver,
RuntimeInformation runtimeInformation,
ExtensionRealmCache extensionRealmCache,
PluginVersionResolver pluginVersionResolver,
PluginArtifactsCache pluginArtifactsCache,
Expand All @@ -171,7 +168,6 @@ public DefaultMavenPluginManager(
this.pluginDescriptorCache = pluginDescriptorCache;
this.pluginRealmCache = pluginRealmCache;
this.pluginDependenciesResolver = pluginDependenciesResolver;
this.runtimeInformation = runtimeInformation;
this.extensionRealmCache = extensionRealmCache;
this.pluginVersionResolver = pluginVersionResolver;
this.pluginArtifactsCache = pluginArtifactsCache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* Service responsible for validating plugin configuration.
*
*/
interface MavenPluginConfigurationValidator {
public interface MavenPluginConfigurationValidator {
/**
* Checks mojo configuration issues.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@
@Named
@Singleton
public class MavenPluginJavaPrerequisiteChecker implements MavenPluginPrerequisitesChecker {

private final VersionScheme versionScheme;

@Inject
public MavenPluginJavaPrerequisiteChecker(final VersionScheme versionScheme) {
public MavenPluginJavaPrerequisiteChecker(VersionScheme versionScheme) {
this.versionScheme = versionScheme;
}

Expand Down
Loading

0 comments on commit a1fdd89

Please sign in to comment.