From d61677fae0e18dfcd051cd7b67045dd979cd44b9 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Thu, 7 Dec 2023 19:33:32 +0100 Subject: [PATCH 01/21] [MNG-7950][MNG-7951] Use VersionSchemeSelector from Resolver 2.0 (to be in alpha-4) In short, this opens the gate to use different version schemes per Maven session. --- https://issues.apache.org/jira/browse/MNG-7950 https://issues.apache.org/jira/browse/MNG-7951 --- .../java/org/apache/maven/DefaultMaven.java | 10 ++++- .../internal/DefaultMavenPluginManager.java | 28 +++++------- .../MavenPluginConfigurationValidator.java | 2 +- .../MavenPluginJavaPrerequisiteChecker.java | 11 ++--- .../MavenPluginMavenPrerequisiteChecker.java | 38 +++++++++++++--- .../plugin/internal/MavenPluginValidator.java | 2 +- .../DefaultPluginVersionResolver.java | 12 ++--- .../maven/rtinfo/RuntimeInformation.java | 2 + .../internal/DefaultRuntimeInformation.java | 8 ++-- ...avenPluginJavaPrerequisiteCheckerTest.java | 2 +- .../internal/DefaultVersionRangeResolver.java | 18 +++++--- .../DefaultVersionSchemeProvider.java | 44 ------------------- pom.xml | 2 +- 13 files changed, 89 insertions(+), 90 deletions(-) delete mode 100644 maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionSchemeProvider.java diff --git a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java index 10d28bc438a7..107537803ace 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java @@ -76,6 +76,8 @@ import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.RepositorySystemSession.CloseableSession; import org.eclipse.aether.repository.WorkspaceReader; +import org.eclipse.aether.spi.version.VersionSchemeSelector; +import org.eclipse.aether.version.VersionScheme; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.helpers.MessageFormatter; @@ -113,6 +115,8 @@ public class DefaultMaven implements Maven { private final DefaultSessionFactory defaultSessionFactory; + private final VersionSchemeSelector versionSchemeSelector; + private final ProjectSelector projectSelector; @Inject @@ -129,7 +133,8 @@ public DefaultMaven( BuildResumptionAnalyzer buildResumptionAnalyzer, BuildResumptionDataRepository buildResumptionDataRepository, SuperPomProvider superPomProvider, - DefaultSessionFactory defaultSessionFactory) { + DefaultSessionFactory defaultSessionFactory, + VersionSchemeSelector versionSchemeSelector) { this.projectBuilder = projectBuilder; this.lifecycleStarter = lifecycleStarter; this.container = container; @@ -142,6 +147,7 @@ public DefaultMaven( this.buildResumptionDataRepository = buildResumptionDataRepository; this.superPomProvider = superPomProvider; this.defaultSessionFactory = defaultSessionFactory; + this.versionSchemeSelector = versionSchemeSelector; this.projectSelector = new ProjectSelector(); // if necessary switch to DI } @@ -222,6 +228,8 @@ private MavenExecutionResult doExecute(MavenExecutionRequest request) { sessionScope.seed(MavenSession.class, session); sessionScope.seed(Session.class, session.getSession()); sessionScope.seed(InternalSession.class, InternalSession.from(session.getSession())); + sessionScope.seed( + VersionScheme.class, versionSchemeSelector.selectVersionScheme(session.getRepositorySession())); legacySupport.setSession(session); diff --git a/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java b/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java index e5a05d4ea5ed..e1f046742a22 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java @@ -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; @@ -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 configurationValidators; - private PluginValidationManager pluginValidationManager; - private List 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 configurationValidators; + private final PluginValidationManager pluginValidationManager; + private final List prerequisitesCheckers; private final ExtensionDescriptorBuilder extensionDescriptorBuilder = new ExtensionDescriptorBuilder(); private final PluginDescriptorBuilder builder = new PluginDescriptorBuilder(); @@ -158,7 +156,6 @@ public DefaultMavenPluginManager( PluginDescriptorCache pluginDescriptorCache, PluginRealmCache pluginRealmCache, DefaultPluginDependenciesResolver pluginDependenciesResolver, - RuntimeInformation runtimeInformation, ExtensionRealmCache extensionRealmCache, PluginVersionResolver pluginVersionResolver, PluginArtifactsCache pluginArtifactsCache, @@ -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; diff --git a/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginConfigurationValidator.java b/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginConfigurationValidator.java index 9c955747ac41..bda3132cadb9 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginConfigurationValidator.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginConfigurationValidator.java @@ -27,7 +27,7 @@ * Service responsible for validating plugin configuration. * */ -interface MavenPluginConfigurationValidator { +public interface MavenPluginConfigurationValidator { /** * Checks mojo configuration issues. */ diff --git a/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteChecker.java b/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteChecker.java index a100c349a0f8..cb0ecaf4aabb 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteChecker.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteChecker.java @@ -20,6 +20,7 @@ import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Provider; import javax.inject.Singleton; import org.apache.maven.plugin.MavenPluginPrerequisitesChecker; @@ -33,11 +34,11 @@ @Singleton public class MavenPluginJavaPrerequisiteChecker implements MavenPluginPrerequisitesChecker { - private final VersionScheme versionScheme; + private final Provider versionSchemeProvider; @Inject - public MavenPluginJavaPrerequisiteChecker(final VersionScheme versionScheme) { - this.versionScheme = versionScheme; + public MavenPluginJavaPrerequisiteChecker(Provider versionSchemeProvider) { + this.versionSchemeProvider = versionSchemeProvider; } @Override @@ -55,13 +56,13 @@ public void accept(PluginDescriptor pluginDescriptor) { boolean matchesVersion(String requiredVersion, String currentVersion) { VersionConstraint constraint; try { - constraint = versionScheme.parseVersionConstraint(requiredVersion); + constraint = versionSchemeProvider.get().parseVersionConstraint(requiredVersion); } catch (InvalidVersionSpecificationException e) { throw new IllegalArgumentException("Invalid 'requiredJavaVersion' given in plugin descriptor", e); } Version current; try { - current = versionScheme.parseVersion(currentVersion); + current = versionSchemeProvider.get().parseVersion(currentVersion); } catch (InvalidVersionSpecificationException e) { throw new IllegalStateException("Could not parse current Java version", e); } diff --git a/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginMavenPrerequisiteChecker.java b/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginMavenPrerequisiteChecker.java index 55dec64fb5f2..ead151f64e50 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginMavenPrerequisiteChecker.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginMavenPrerequisiteChecker.java @@ -20,11 +20,16 @@ import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Provider; import javax.inject.Singleton; import org.apache.maven.plugin.MavenPluginPrerequisitesChecker; import org.apache.maven.plugin.descriptor.PluginDescriptor; import org.apache.maven.rtinfo.RuntimeInformation; +import org.eclipse.aether.version.InvalidVersionSpecificationException; +import org.eclipse.aether.version.Version; +import org.eclipse.aether.version.VersionConstraint; +import org.eclipse.aether.version.VersionScheme; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,12 +37,16 @@ @Singleton public class MavenPluginMavenPrerequisiteChecker implements MavenPluginPrerequisitesChecker { private final Logger logger = LoggerFactory.getLogger(getClass()); + private final RuntimeInformation runtimeInformation; + private final Provider versionSchemeProvider; + @Inject - public MavenPluginMavenPrerequisiteChecker(RuntimeInformation runtimeInformation) { - super(); + public MavenPluginMavenPrerequisiteChecker( + RuntimeInformation runtimeInformation, Provider versionSchemeProvider) { this.runtimeInformation = runtimeInformation; + this.versionSchemeProvider = versionSchemeProvider; } @Override @@ -48,16 +57,35 @@ public void accept(PluginDescriptor pluginDescriptor) { requiredMavenVersion == null || requiredMavenVersion.trim().isEmpty(); if (!isBlankVersion) { - boolean isRequirementMet = false; + VersionScheme versionScheme = versionSchemeProvider.get(); // this must happen within session + VersionConstraint constraint; try { - isRequirementMet = runtimeInformation.isMavenVersion(requiredMavenVersion); - } catch (IllegalArgumentException e) { + constraint = versionScheme.parseVersionConstraint(requiredMavenVersion); + } catch (InvalidVersionSpecificationException e) { logger.warn( "Could not verify plugin's Maven prerequisite as an invalid version is given in " + requiredMavenVersion, e); return; } + + Version current; + try { + String mavenVersion = runtimeInformation.getMavenVersion(); + if (mavenVersion == null || mavenVersion.isEmpty()) { + throw new IllegalArgumentException("Could not determine current Maven version"); + } + current = versionScheme.parseVersion(mavenVersion); + } catch (InvalidVersionSpecificationException e) { + throw new IllegalStateException("Could not parse current Maven version: " + e.getMessage(), e); + } + + boolean isRequirementMet; + if (constraint.getRange() == null) { + isRequirementMet = constraint.getVersion().compareTo(current) <= 0; + } else { + isRequirementMet = constraint.containsVersion(current); + } if (!isRequirementMet) { throw new IllegalStateException("Required Maven version " + requiredMavenVersion + " is not met by current version " + runtimeInformation.getMavenVersion()); diff --git a/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginValidator.java b/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginValidator.java index 5333c560ae11..09e10a4c3739 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginValidator.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginValidator.java @@ -26,7 +26,7 @@ /** * MavenPluginValidator */ -interface MavenPluginValidator { +public interface MavenPluginValidator { void validate(Artifact pluginArtifact, PluginDescriptor pluginDescriptor, List errors); } diff --git a/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java b/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java index 904606a2a04a..e81c1b7f6e06 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java @@ -57,9 +57,9 @@ import org.eclipse.aether.repository.RemoteRepository; import org.eclipse.aether.resolution.MetadataRequest; import org.eclipse.aether.resolution.MetadataResult; +import org.eclipse.aether.spi.version.VersionSchemeSelector; import org.eclipse.aether.version.InvalidVersionSpecificationException; import org.eclipse.aether.version.Version; -import org.eclipse.aether.version.VersionScheme; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -79,18 +79,18 @@ public class DefaultPluginVersionResolver implements PluginVersionResolver { private final RepositorySystem repositorySystem; private final MetadataReader metadataReader; private final MavenPluginManager pluginManager; - private final VersionScheme versionScheme; + private final VersionSchemeSelector versionSchemeSelector; @Inject public DefaultPluginVersionResolver( RepositorySystem repositorySystem, MetadataReader metadataReader, MavenPluginManager pluginManager, - VersionScheme versionScheme) { + VersionSchemeSelector versionSchemeSelector) { this.repositorySystem = repositorySystem; this.metadataReader = metadataReader; this.pluginManager = pluginManager; - this.versionScheme = versionScheme; + this.versionSchemeSelector = versionSchemeSelector; } @Override @@ -194,7 +194,9 @@ private void selectVersion(DefaultPluginVersionResult result, PluginVersionReque for (String ver : versions.versions.keySet()) { try { - Version v = versionScheme.parseVersion(ver); + Version v = versionSchemeSelector + .selectVersionScheme(request.getRepositorySession()) + .parseVersion(ver); if (ver.endsWith("-SNAPSHOT")) { snapshots.add(v); diff --git a/maven-core/src/main/java/org/apache/maven/rtinfo/RuntimeInformation.java b/maven-core/src/main/java/org/apache/maven/rtinfo/RuntimeInformation.java index 847c8b09f8dc..6bbd8e3b2af2 100644 --- a/maven-core/src/main/java/org/apache/maven/rtinfo/RuntimeInformation.java +++ b/maven-core/src/main/java/org/apache/maven/rtinfo/RuntimeInformation.java @@ -41,6 +41,8 @@ public interface RuntimeInformation { * @return {@code true} if the current Maven runtime matches the specified version range, {@code false} otherwise. * @throws IllegalArgumentException If the specified version range is {@code null}, empty or otherwise not a valid * version specification. + * @deprecated This method is deprecated, use {@link #getMavenVersion()} instead. */ + @Deprecated boolean isMavenVersion(String versionRange); } diff --git a/maven-core/src/main/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformation.java b/maven-core/src/main/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformation.java index 9211c615ff71..9624115f3bd1 100644 --- a/maven-core/src/main/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformation.java +++ b/maven-core/src/main/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformation.java @@ -28,6 +28,7 @@ import java.util.Properties; 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; @@ -43,13 +44,13 @@ public class DefaultRuntimeInformation implements RuntimeInformation { private final Logger logger = LoggerFactory.getLogger(getClass()); - private final VersionScheme versionScheme; + @Deprecated + private final VersionScheme versionScheme = new GenericVersionScheme(); private final String mavenVersion; @Inject - public DefaultRuntimeInformation(final VersionScheme versionScheme) { - this.versionScheme = versionScheme; + public DefaultRuntimeInformation() { this.mavenVersion = loadMavenVersion(); } @@ -87,6 +88,7 @@ private String loadMavenVersion() { } } + @Deprecated @Override public boolean isMavenVersion(String versionRange) { if (Objects.requireNonNull(versionRange, "versionRange cannot be null").isEmpty()) { diff --git a/maven-core/src/test/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteCheckerTest.java b/maven-core/src/test/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteCheckerTest.java index 2c68ebbf2d1b..14a331e992a4 100644 --- a/maven-core/src/test/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteCheckerTest.java +++ b/maven-core/src/test/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteCheckerTest.java @@ -27,7 +27,7 @@ class MavenPluginJavaPrerequisiteCheckerTest { @Test void testMatchesVersion() { - MavenPluginJavaPrerequisiteChecker checker = new MavenPluginJavaPrerequisiteChecker(new GenericVersionScheme()); + MavenPluginJavaPrerequisiteChecker checker = new MavenPluginJavaPrerequisiteChecker(GenericVersionScheme::new); assertTrue(checker.matchesVersion("1.0", "1.8")); assertTrue(checker.matchesVersion("1.8", "9.0.1+11")); assertFalse(checker.matchesVersion("[1.0,2],[3,4]", "2.1")); diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java index c8ca6bd374d6..b253705c80fa 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java @@ -53,11 +53,11 @@ import org.eclipse.aether.resolution.VersionRangeResolutionException; import org.eclipse.aether.resolution.VersionRangeResult; import org.eclipse.aether.spi.synccontext.SyncContextFactory; +import org.eclipse.aether.spi.version.VersionSchemeSelector; import org.eclipse.aether.version.InvalidVersionSpecificationException; import org.eclipse.aether.version.Version; import org.eclipse.aether.version.VersionConstraint; import org.eclipse.aether.version.VersionRange; -import org.eclipse.aether.version.VersionScheme; /** */ @@ -70,19 +70,20 @@ public class DefaultVersionRangeResolver implements VersionRangeResolver { private final MetadataResolver metadataResolver; private final SyncContextFactory syncContextFactory; private final RepositoryEventDispatcher repositoryEventDispatcher; - private final VersionScheme versionScheme; + private final VersionSchemeSelector versionSchemeSelector; @Inject public DefaultVersionRangeResolver( MetadataResolver metadataResolver, SyncContextFactory syncContextFactory, RepositoryEventDispatcher repositoryEventDispatcher, - VersionScheme versionScheme) { + VersionSchemeSelector versionSchemeSelector) { 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"); + this.versionSchemeSelector = + Objects.requireNonNull(versionSchemeSelector, "versionSchemeSelector cannot be null"); } @Override @@ -92,8 +93,9 @@ public VersionRangeResult resolveVersionRange(RepositorySystemSession session, V VersionConstraint versionConstraint; try { - versionConstraint = - versionScheme.parseVersionConstraint(request.getArtifact().getVersion()); + versionConstraint = versionSchemeSelector + .selectVersionScheme(session) + .parseVersionConstraint(request.getArtifact().getVersion()); } catch (InvalidVersionSpecificationException e) { result.addException(e); throw new VersionRangeResolutionException(result); @@ -114,7 +116,9 @@ public VersionRangeResult resolveVersionRange(RepositorySystemSession session, V List versions = new ArrayList<>(); for (Map.Entry v : versionIndex.entrySet()) { try { - Version ver = versionScheme.parseVersion(v.getKey()); + Version ver = versionSchemeSelector + .selectVersionScheme(session) + .parseVersion(v.getKey()); if (versionConstraint.containsVersion(ver)) { versions.add(ver); result.setRepository(ver, v.getValue()); diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionSchemeProvider.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionSchemeProvider.java deleted file mode 100644 index 8bebb05f5796..000000000000 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionSchemeProvider.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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. - */ -package org.apache.maven.repository.internal; - -import javax.inject.Named; -import javax.inject.Provider; -import javax.inject.Singleton; - -import org.eclipse.aether.util.version.GenericVersionScheme; -import org.eclipse.aether.version.VersionScheme; - -/** - * Default version scheme provider: provides singleton {@link GenericVersionScheme} instance. - */ -@Singleton -@Named -public final class DefaultVersionSchemeProvider implements Provider { - private final GenericVersionScheme genericVersionScheme; - - public DefaultVersionSchemeProvider() { - this.genericVersionScheme = new GenericVersionScheme(); - } - - @Override - public VersionScheme get() { - return genericVersionScheme; - } -} diff --git a/pom.xml b/pom.xml index 9d9ee9214330..a8be2310b015 100644 --- a/pom.xml +++ b/pom.xml @@ -176,7 +176,7 @@ under the License. 1.26 1.0.0 4.0.1 - 2.0.0-alpha-3 + 2.0.0-SNAPSHOT 2.0 0.9.0.M2 1.7.36 From 3c34dfc58ab95da392729cb494218456bb2ecc64 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Thu, 14 Dec 2023 19:10:09 +0100 Subject: [PATCH 02/21] Use alpha-5 --- pom.xml | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 447fb7f830d1..78311d328b41 100644 --- a/pom.xml +++ b/pom.xml @@ -176,7 +176,7 @@ under the License. 1.26 1.0.0 4.0.1 - 2.0.0-SNAPSHOT + 2.0.0-alpha-5 2.0 0.9.0.M2 1.7.36 @@ -502,6 +502,20 @@ under the License. + + + + + true + + + false + + maven-2048 + https://repository.apache.org/content/repositories/maven-2048 + + + From da737af2b728b1c81510d32911a45ec3c4a736c4 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Fri, 15 Dec 2023 10:23:36 +0100 Subject: [PATCH 03/21] Sort out new API --- .../internal/impl/DefaultVersionParser.java | 56 ++++++++++++------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java index 28d6ca13c05c..2bf11234807c 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java +++ b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java @@ -18,6 +18,7 @@ */ package org.apache.maven.internal.impl; +import javax.inject.Inject; import javax.inject.Named; import javax.inject.Singleton; @@ -28,11 +29,9 @@ 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 org.eclipse.aether.version.InvalidVersionSpecificationException; +import org.eclipse.aether.version.VersionScheme; -import static org.apache.maven.artifact.versioning.VersionRange.createFromVersionSpec; import static org.apache.maven.internal.impl.Utils.nonNull; @Named @@ -41,18 +40,23 @@ 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 VersionScheme versionScheme; + + @Inject + public DefaultVersionParser(VersionScheme versionScheme) { + this.versionScheme = nonNull(versionScheme, "versionScheme"); + } + @Override public Version parseVersion(String version) { - return new DefaultVersion(new DefaultArtifactVersion(nonNull(version, "version"))); + nonNull(version, "version"); + return new DefaultVersion(versionScheme, 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); - } + nonNull(range, "range"); + return new DefaultVersionRange(versionScheme, range); } @Override @@ -65,10 +69,16 @@ static boolean checkSnapshot(String version) { } static class DefaultVersion implements Version { - private final ArtifactVersion delegate; - - DefaultVersion(ArtifactVersion delegate) { - this.delegate = delegate; + private final VersionScheme versionScheme; + private final org.eclipse.aether.version.Version delegate; + + DefaultVersion(VersionScheme versionScheme, String delegateValue) { + this.versionScheme = versionScheme; + try { + this.delegate = versionScheme.parseVersion(delegateValue); + } catch (InvalidVersionSpecificationException e) { + throw new VersionParserException("Unable to parse version: " + delegateValue, e); + } } @Override @@ -76,7 +86,7 @@ public int compareTo(Version o) { if (o instanceof DefaultVersion) { return delegate.compareTo(((DefaultVersion) o).delegate); } else { - return delegate.compareTo(new DefaultArtifactVersion(o.toString())); + return compareTo(new DefaultVersion(versionScheme, o.toString())); } } @@ -109,10 +119,16 @@ public String toString() { } 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; + private final VersionScheme versionScheme; + private final org.eclipse.aether.version.VersionRange delegate; + + DefaultVersionRange(VersionScheme versionScheme, String delegateValue) { + this.versionScheme = versionScheme; + try { + this.delegate = versionScheme.parseVersionRange(delegateValue); + } catch (InvalidVersionSpecificationException e) { + throw new VersionParserException("Unable to parse version range: " + delegateValue, e); + } } @Override @@ -120,7 +136,7 @@ public boolean contains(Version version) { if (version instanceof DefaultVersion) { return delegate.containsVersion(((DefaultVersion) version).delegate); } else { - return delegate.containsVersion(new DefaultArtifactVersion(version.toString())); + return contains(new DefaultVersion(versionScheme, version.toString())); } } From 6df760db0bfe92aca9af979e4945364b6a39250d Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Fri, 15 Dec 2023 11:10:00 +0100 Subject: [PATCH 04/21] Update --- .../internal/impl/DefaultVersionParser.java | 98 +----------- maven-model-builder/pom.xml | 4 - .../model/building/DefaultModelBuilder.java | 123 ++++++++------- .../building/DefaultModelBuilderFactory.java | 43 +++++- .../model/version/ModelVersionParser.java | 53 +++++++ .../internal/DefaultModelVersionParser.java | 140 ++++++++++++++++++ 6 files changed, 306 insertions(+), 155 deletions(-) create mode 100644 maven-model-builder/src/main/java/org/apache/maven/model/version/ModelVersionParser.java create mode 100644 maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelVersionParser.java diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java index 2bf11234807c..a3d43b31918c 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java +++ b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java @@ -22,15 +22,12 @@ 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.eclipse.aether.version.InvalidVersionSpecificationException; -import org.eclipse.aether.version.VersionScheme; +import org.apache.maven.model.version.ModelVersionParser; import static org.apache.maven.internal.impl.Utils.nonNull; @@ -40,23 +37,23 @@ 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 VersionScheme versionScheme; + private final ModelVersionParser modelVersionParser; @Inject - public DefaultVersionParser(VersionScheme versionScheme) { - this.versionScheme = nonNull(versionScheme, "versionScheme"); + public DefaultVersionParser(ModelVersionParser modelVersionParser) { + this.modelVersionParser = nonNull(modelVersionParser, "modelVersionParser"); } @Override public Version parseVersion(String version) { nonNull(version, "version"); - return new DefaultVersion(versionScheme, version); + return modelVersionParser.parseVersion(version); } @Override public VersionRange parseVersionRange(String range) { nonNull(range, "range"); - return new DefaultVersionRange(versionScheme, range); + return modelVersionParser.parseVersionRange(range); } @Override @@ -67,87 +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 VersionScheme versionScheme; - private final org.eclipse.aether.version.Version delegate; - - DefaultVersion(VersionScheme versionScheme, String delegateValue) { - this.versionScheme = versionScheme; - try { - this.delegate = versionScheme.parseVersion(delegateValue); - } catch (InvalidVersionSpecificationException e) { - throw new VersionParserException("Unable to parse version: " + delegateValue, e); - } - } - - @Override - public int compareTo(Version o) { - if (o instanceof DefaultVersion) { - return delegate.compareTo(((DefaultVersion) o).delegate); - } else { - return compareTo(new DefaultVersion(versionScheme, 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 VersionScheme versionScheme; - private final org.eclipse.aether.version.VersionRange delegate; - - DefaultVersionRange(VersionScheme versionScheme, String delegateValue) { - this.versionScheme = versionScheme; - try { - this.delegate = versionScheme.parseVersionRange(delegateValue); - } catch (InvalidVersionSpecificationException e) { - throw new VersionParserException("Unable to parse version range: " + delegateValue, e); - } - } - - @Override - public boolean contains(Version version) { - if (version instanceof DefaultVersion) { - return delegate.containsVersion(((DefaultVersion) version).delegate); - } else { - return contains(new DefaultVersion(versionScheme, version.toString())); - } - } - - @Override - public String asString() { - return delegate.toString(); - } - - @Override - public String toString() { - return asString(); - } - } } diff --git a/maven-model-builder/pom.xml b/maven-model-builder/pom.xml index 18dc68d25332..b1b5e8c3ad72 100644 --- a/maven-model-builder/pom.xml +++ b/maven-model-builder/pom.xml @@ -48,10 +48,6 @@ under the License. org.apache.maven maven-model - - org.apache.maven - maven-artifact - org.apache.maven maven-builder-support diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java index 033ca11dae6b..f5e9f1eb9e08 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java @@ -32,12 +32,11 @@ import java.util.function.Supplier; import java.util.stream.Collectors; +import org.apache.maven.api.VersionRange; import org.apache.maven.api.feature.Features; import org.apache.maven.api.model.Exclusion; import org.apache.maven.api.model.InputSource; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; -import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; -import org.apache.maven.artifact.versioning.VersionRange; +import org.apache.maven.api.services.VersionParserException; import org.apache.maven.building.Source; import org.apache.maven.model.Activation; import org.apache.maven.model.Build; @@ -50,7 +49,6 @@ import org.apache.maven.model.PluginManagement; import org.apache.maven.model.Profile; import org.apache.maven.model.building.ModelProblem.Severity; -import org.apache.maven.model.building.ModelProblem.Version; import org.apache.maven.model.composition.DependencyManagementImporter; import org.apache.maven.model.inheritance.InheritanceAssembler; import org.apache.maven.model.interpolation.ModelInterpolator; @@ -78,6 +76,7 @@ import org.apache.maven.model.superpom.SuperPomProvider; import org.apache.maven.model.validation.DefaultModelValidator; import org.apache.maven.model.validation.ModelValidator; +import org.apache.maven.model.version.ModelVersionParser; import org.codehaus.plexus.interpolation.InterpolationException; import org.codehaus.plexus.interpolation.MapBasedValueSource; import org.codehaus.plexus.interpolation.StringSearchInterpolator; @@ -111,6 +110,7 @@ public class DefaultModelBuilder implements ModelBuilder { private final ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator; private final ModelVersionProcessor versionProcessor; private final ModelSourceTransformer transformer; + private final ModelVersionParser modelVersionParser; @SuppressWarnings("checkstyle:ParameterNumber") @Inject @@ -133,7 +133,8 @@ public DefaultModelBuilder( ReportConfigurationExpander reportConfigurationExpander, ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator, ModelVersionProcessor versionProcessor, - ModelSourceTransformer transformer) { + ModelSourceTransformer transformer, + ModelVersionParser modelVersionParser) { this.modelProcessor = modelProcessor; this.modelValidator = modelValidator; this.modelNormalizer = modelNormalizer; @@ -153,6 +154,7 @@ public DefaultModelBuilder( this.profileActivationFilePathInterpolator = profileActivationFilePathInterpolator; this.versionProcessor = versionProcessor; this.transformer = transformer; + this.modelVersionParser = modelVersionParser; } /** @@ -180,7 +182,8 @@ public DefaultModelBuilder setModelProcessor(ModelProcessor modelProcessor) { reportConfigurationExpander, profileActivationFilePathInterpolator, versionProcessor, - transformer); + transformer, + modelVersionParser); } /** @@ -208,7 +211,8 @@ public DefaultModelBuilder setModelValidator(ModelValidator modelValidator) { reportConfigurationExpander, profileActivationFilePathInterpolator, versionProcessor, - transformer); + transformer, + modelVersionParser); } /** @@ -236,7 +240,8 @@ public DefaultModelBuilder setModelNormalizer(ModelNormalizer modelNormalizer) { reportConfigurationExpander, profileActivationFilePathInterpolator, versionProcessor, - transformer); + transformer, + modelVersionParser); } /** @@ -264,7 +269,8 @@ public DefaultModelBuilder setModelInterpolator(ModelInterpolator modelInterpola reportConfigurationExpander, profileActivationFilePathInterpolator, versionProcessor, - transformer); + transformer, + modelVersionParser); } /** @@ -292,7 +298,8 @@ public DefaultModelBuilder setModelPathTranslator(ModelPathTranslator modelPathT reportConfigurationExpander, profileActivationFilePathInterpolator, versionProcessor, - transformer); + transformer, + modelVersionParser); } /** @@ -320,7 +327,8 @@ public DefaultModelBuilder setModelUrlNormalizer(ModelUrlNormalizer modelUrlNorm reportConfigurationExpander, profileActivationFilePathInterpolator, versionProcessor, - transformer); + transformer, + modelVersionParser); } /** @@ -348,7 +356,8 @@ public DefaultModelBuilder setSuperPomProvider(SuperPomProvider superPomProvider reportConfigurationExpander, profileActivationFilePathInterpolator, versionProcessor, - transformer); + transformer, + modelVersionParser); } /** @@ -376,7 +385,8 @@ public DefaultModelBuilder setInheritanceAssembler(InheritanceAssembler inherita reportConfigurationExpander, profileActivationFilePathInterpolator, versionProcessor, - transformer); + transformer, + modelVersionParser); } /** @@ -404,7 +414,8 @@ public DefaultModelBuilder setProfileSelector(ProfileSelector profileSelector) { reportConfigurationExpander, profileActivationFilePathInterpolator, versionProcessor, - transformer); + transformer, + modelVersionParser); } /** @@ -432,7 +443,8 @@ public DefaultModelBuilder setProfileInjector(ProfileInjector profileInjector) { reportConfigurationExpander, profileActivationFilePathInterpolator, versionProcessor, - transformer); + transformer, + modelVersionParser); } /** @@ -460,7 +472,8 @@ public DefaultModelBuilder setPluginManagementInjector(PluginManagementInjector reportConfigurationExpander, profileActivationFilePathInterpolator, versionProcessor, - transformer); + transformer, + modelVersionParser); } /** @@ -489,7 +502,8 @@ public DefaultModelBuilder setDependencyManagementInjector( reportConfigurationExpander, profileActivationFilePathInterpolator, versionProcessor, - transformer); + transformer, + modelVersionParser); } /** @@ -518,7 +532,8 @@ public DefaultModelBuilder setDependencyManagementImporter( reportConfigurationExpander, profileActivationFilePathInterpolator, versionProcessor, - transformer); + transformer, + modelVersionParser); } /** @@ -546,7 +561,8 @@ public DefaultModelBuilder setLifecycleBindingsInjector(LifecycleBindingsInjecto reportConfigurationExpander, profileActivationFilePathInterpolator, versionProcessor, - transformer); + transformer, + modelVersionParser); } /** @@ -574,7 +590,8 @@ public DefaultModelBuilder setPluginConfigurationExpander(PluginConfigurationExp reportConfigurationExpander, profileActivationFilePathInterpolator, versionProcessor, - transformer); + transformer, + modelVersionParser); } /** @@ -602,7 +619,8 @@ public DefaultModelBuilder setReportConfigurationExpander(ReportConfigurationExp reportConfigurationExpander, profileActivationFilePathInterpolator, versionProcessor, - transformer); + transformer, + modelVersionParser); } /** @@ -631,7 +649,8 @@ public DefaultModelBuilder setProfileActivationFilePathInterpolator( reportConfigurationExpander, profileActivationFilePathInterpolator, versionProcessor, - transformer); + transformer, + modelVersionParser); } /** @@ -921,7 +940,7 @@ private static void addInterpolationProblem( String path, InterpolationException e, String locationKey) { - problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE) + problems.add(new ModelProblemCollectorRequest(Severity.ERROR, ModelProblem.Version.BASE) .setMessage("Failed to interpolate file location " + path + ": " + e.getMessage()) .setLocation(Optional.ofNullable(file.getLocation(locationKey)) .map(InputLocation::new) @@ -1036,7 +1055,8 @@ public Result buildRawModel( transformer.transform(pomFile.toPath(), context, model); } } catch (TransformerException e) { - problems.add(new ModelProblemCollectorRequest(Severity.FATAL, Version.V40).setException(e)); + problems.add( + new ModelProblemCollectorRequest(Severity.FATAL, ModelProblem.Version.V40).setException(e)); } return newResult(model, problems.getProblems()); @@ -1108,7 +1128,7 @@ private org.apache.maven.api.model.Model doReadFileModel( } Severity severity = (modelSource instanceof FileModelSource) ? Severity.ERROR : Severity.WARNING; - problems.add(new ModelProblemCollectorRequest(severity, Version.V20) + problems.add(new ModelProblemCollectorRequest(severity, ModelProblem.Version.V20) .setMessage("Malformed POM " + modelSource.getLocation() + ": " + e.getMessage()) .setException(e)); } @@ -1128,7 +1148,7 @@ private org.apache.maven.api.model.Model doReadFileModel( } } } catch (ModelParseException e) { - problems.add(new ModelProblemCollectorRequest(Severity.FATAL, Version.BASE) + problems.add(new ModelProblemCollectorRequest(Severity.FATAL, ModelProblem.Version.BASE) .setMessage("Non-parseable POM " + modelSource.getLocation() + ": " + e.getMessage()) .setException(e)); throw problems.newModelBuildingException(); @@ -1142,7 +1162,7 @@ private org.apache.maven.api.model.Model doReadFileModel( msg = e.getClass().getSimpleName(); } } - problems.add(new ModelProblemCollectorRequest(Severity.FATAL, Version.BASE) + problems.add(new ModelProblemCollectorRequest(Severity.FATAL, ModelProblem.Version.BASE) .setMessage("Non-readable POM " + modelSource.getLocation() + ": " + msg) .setException(e)); throw problems.newModelBuildingException(); @@ -1193,7 +1213,8 @@ private ModelData doReadRawModel( transformer.transform(pomFile.toPath(), context, rawModel); } } catch (TransformerException e) { - problems.add(new ModelProblemCollectorRequest(Severity.FATAL, Version.V40).setException(e)); + problems.add( + new ModelProblemCollectorRequest(Severity.FATAL, ModelProblem.Version.V40).setException(e)); } } else if (request.getFileModel() == null) { rawModel = readFileModel(request, problems); @@ -1264,7 +1285,7 @@ private void configureResolver( try { modelResolver.addRepository(repository, replaceRepositories); } catch (InvalidRepositoryException e) { - problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE) + problems.add(new ModelProblemCollectorRequest(Severity.ERROR, ModelProblem.Version.BASE) .setMessage("Invalid repository " + repository.getId() + ": " + e.getMessage()) .setLocation(new InputLocation(repository.getLocation(""))) .setException(e)); @@ -1307,7 +1328,7 @@ private void checkPluginVersions( for (String key : versions.keySet()) { if (versions.get(key) == null && managedVersions.get(key) == null) { InputLocation location = plugins.get(key).getLocation(""); - problems.add(new ModelProblemCollectorRequest(Severity.WARNING, Version.V20) + problems.add(new ModelProblemCollectorRequest(Severity.WARNING, ModelProblem.Version.V20) .setMessage("'build.plugins.plugin.version' for " + key + " is missing.") .setLocation(location)); } @@ -1376,7 +1397,8 @@ private Model interpolateModel(Model model, ModelBuildingRequest request, ModelP ssi.interpolate(interpolatedModel.getParent().getVersion()); interpolatedModel.getParent().setVersion(interpolated); } catch (Exception e) { - ModelProblemCollectorRequest mpcr = new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE) + ModelProblemCollectorRequest mpcr = new ModelProblemCollectorRequest( + Severity.ERROR, ModelProblem.Version.BASE) .setMessage("Failed to interpolate field: " + interpolatedModel.getParent().getVersion() + " on class: ") @@ -1406,7 +1428,7 @@ private ModelData readParent( Model parentModel = parentData.getModel(); if (!"pom".equals(parentModel.getPackaging())) { - problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE) + problems.add(new ModelProblemCollectorRequest(Severity.ERROR, ModelProblem.Version.BASE) .setMessage("Invalid packaging for parent POM " + ModelProblemUtils.toSourceHint(parentModel) + ", must be \"pom\" but is \"" + parentModel.getPackaging() + "\"") .setLocation(parentModel.getLocation("packaging"))); @@ -1439,7 +1461,7 @@ private ModelData readParentLocally( candidateModel = resolver.resolveRawModel(parent.getGroupId(), parent.getArtifactId(), parent.getVersion()); } catch (UnresolvableModelException e) { - problems.add(new ModelProblemCollectorRequest(Severity.FATAL, Version.BASE) // + problems.add(new ModelProblemCollectorRequest(Severity.FATAL, ModelProblem.Version.BASE) // .setMessage(e.getMessage()) .setLocation(parent.getLocation("")) .setException(e)); @@ -1474,7 +1496,7 @@ private ModelData readParentLocally( buffer.append(parent.getArtifactId()).append(", please verify your project structure"); problems.setSource(childModel); - problems.add(new ModelProblemCollectorRequest(Severity.WARNING, Version.BASE) + problems.add(new ModelProblemCollectorRequest(Severity.WARNING, ModelProblem.Version.BASE) .setMessage(buffer.toString()) .setLocation(parent.getLocation(""))); return null; @@ -1483,12 +1505,8 @@ private ModelData readParentLocally( String version = getVersion(candidateModel); if (version != null && parent.getVersion() != null && !version.equals(parent.getVersion())) { try { - VersionRange parentRange = VersionRange.createFromVersionSpec(parent.getVersion()); - if (!parentRange.hasRestrictions()) { - // the parent version is not a range, we have version skew, drop back to resolution from repo - return null; - } - if (!parentRange.containsVersion(new DefaultArtifactVersion(version))) { + VersionRange parentRange = modelVersionParser.parseVersionRange(parent.getVersion()); + if (!parentRange.contains(modelVersionParser.parseVersion(version))) { // version skew drop back to resolution from the repository return null; } @@ -1498,21 +1516,21 @@ private ModelData readParentLocally( if (rawChildModelVersion == null) { // Message below is checked for in the MNG-2199 core IT. - problems.add(new ModelProblemCollectorRequest(Severity.FATAL, Version.V31) + problems.add(new ModelProblemCollectorRequest(Severity.FATAL, ModelProblem.Version.V31) .setMessage("Version must be a constant") .setLocation(childModel.getLocation(""))); } else { if (rawChildVersionReferencesParent(rawChildModelVersion)) { // Message below is checked for in the MNG-2199 core IT. - problems.add(new ModelProblemCollectorRequest(Severity.FATAL, Version.V31) + problems.add(new ModelProblemCollectorRequest(Severity.FATAL, ModelProblem.Version.V31) .setMessage("Version must be a constant") .setLocation(childModel.getLocation("version"))); } } // MNG-2199: What else to check here ? - } catch (InvalidVersionSpecificationException e) { + } catch (VersionParserException e) { // invalid version range, so drop back to resolution from the repository return null; } @@ -1596,7 +1614,7 @@ private ModelData readParentExternally( } } - problems.add(new ModelProblemCollectorRequest(Severity.FATAL, Version.BASE) + problems.add(new ModelProblemCollectorRequest(Severity.FATAL, ModelProblem.Version.BASE) .setMessage(buffer.toString()) .setLocation(parent.getLocation("")) .setException(e)); @@ -1616,14 +1634,14 @@ private ModelData readParentExternally( if (rawChildModelVersion == null) { // Message below is checked for in the MNG-2199 core IT. - problems.add(new ModelProblemCollectorRequest(Severity.FATAL, Version.V31) + problems.add(new ModelProblemCollectorRequest(Severity.FATAL, ModelProblem.Version.V31) .setMessage("Version must be a constant") .setLocation(childModel.getLocation(""))); } else { if (rawChildVersionReferencesParent(rawChildModelVersion)) { // Message below is checked for in the MNG-2199 core IT. - problems.add(new ModelProblemCollectorRequest(Severity.FATAL, Version.V31) + problems.add(new ModelProblemCollectorRequest(Severity.FATAL, ModelProblem.Version.V31) .setMessage("Version must be a constant") .setLocation(childModel.getLocation("version"))); } @@ -1695,21 +1713,21 @@ private DependencyManagement loadDependencyManagement( String version = dependency.getVersion(); if (groupId == null || groupId.length() <= 0) { - problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE) + problems.add(new ModelProblemCollectorRequest(Severity.ERROR, ModelProblem.Version.BASE) .setMessage("'dependencyManagement.dependencies.dependency.groupId' for " + dependency.getManagementKey() + " is missing.") .setLocation(dependency.getLocation(""))); return null; } if (artifactId == null || artifactId.length() <= 0) { - problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE) + problems.add(new ModelProblemCollectorRequest(Severity.ERROR, ModelProblem.Version.BASE) .setMessage("'dependencyManagement.dependencies.dependency.artifactId' for " + dependency.getManagementKey() + " is missing.") .setLocation(dependency.getLocation(""))); return null; } if (version == null || version.length() <= 0) { - problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE) + problems.add(new ModelProblemCollectorRequest(Severity.ERROR, ModelProblem.Version.BASE) .setMessage("'dependencyManagement.dependencies.dependency.version' for " + dependency.getManagementKey() + " is missing.") .setLocation(dependency.getLocation(""))); @@ -1725,7 +1743,8 @@ private DependencyManagement loadDependencyManagement( message.append(modelId).append(" -> "); } message.append(imported); - problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE).setMessage(message.toString())); + problems.add(new ModelProblemCollectorRequest(Severity.ERROR, ModelProblem.Version.BASE) + .setMessage(message.toString())); return null; } @@ -1786,7 +1805,7 @@ private org.apache.maven.api.model.DependencyManagement doLoadDependencyManageme try { importModel = workspaceResolver.resolveEffectiveModel(groupId, artifactId, version); } catch (UnresolvableModelException e) { - problems.add(new ModelProblemCollectorRequest(Severity.FATAL, Version.BASE) + problems.add(new ModelProblemCollectorRequest(Severity.FATAL, ModelProblem.Version.BASE) .setMessage(e.getMessage()) .setException(e)); return null; @@ -1806,7 +1825,7 @@ private org.apache.maven.api.model.DependencyManagement doLoadDependencyManageme } buffer.append(": ").append(e.getMessage()); - problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE) + problems.add(new ModelProblemCollectorRequest(Severity.ERROR, ModelProblem.Version.BASE) .setMessage(buffer.toString()) .setLocation(dependency.getLocation("")) .setException(e)); diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilderFactory.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilderFactory.java index dc474cef22d7..68b022e4c046 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilderFactory.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilderFactory.java @@ -20,6 +20,8 @@ import java.util.Arrays; +import org.apache.maven.api.Version; +import org.apache.maven.api.VersionRange; import org.apache.maven.api.spi.ModelParser; import org.apache.maven.model.Model; import org.apache.maven.model.composition.DefaultDependencyManagementImporter; @@ -71,6 +73,7 @@ import org.apache.maven.model.superpom.SuperPomProvider; import org.apache.maven.model.validation.DefaultModelValidator; import org.apache.maven.model.validation.ModelValidator; +import org.apache.maven.model.version.ModelVersionParser; /** * A factory to create model builder instances when no dependency injection is available. Note: This class is @@ -101,8 +104,7 @@ public class DefaultModelBuilderFactory { private ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator; private ModelVersionProcessor versionProcessor; private ModelSourceTransformer transformer; - - private RootLocator rootLocator; + private ModelVersionParser modelVersionParser; public DefaultModelBuilderFactory setModelProcessor(ModelProcessor modelProcessor) { this.modelProcessor = modelProcessor; @@ -205,13 +207,13 @@ public DefaultModelBuilderFactory setVersionProcessor(ModelVersionProcessor vers return this; } - public DefaultModelBuilderFactory setRootLocator(RootLocator rootLocator) { - this.rootLocator = rootLocator; + public DefaultModelBuilderFactory setTransformer(ModelSourceTransformer transformer) { + this.transformer = transformer; return this; } - public DefaultModelBuilderFactory setTransformer(ModelSourceTransformer transformer) { - this.transformer = transformer; + public DefaultModelBuilderFactory setModelVersionParser(ModelVersionParser modelVersionParser) { + this.modelVersionParser = modelVersionParser; return this; } @@ -333,6 +335,32 @@ private ModelSourceTransformer newModelSourceTransformer() { return new BuildModelSourceTransformer(); } + private ModelVersionParser newModelVersionParser() { + // This is a limited parser that does not support ranges and compares versions as strings + // in real-life this parser should not be used, but replaced with a proper one + return new ModelVersionParser() { + @Override + public Version parseVersion(String version) { + return new Version() { + @Override + public String asString() { + return version; + } + + @Override + public int compareTo(Version o) { + return version.compareTo(o.asString()); + } + }; + } + + @Override + public VersionRange parseVersionRange(String range) { + throw new IllegalArgumentException("ranges not supported by this parser"); + } + }; + } + /** * Creates a new model builder instance. * @@ -360,7 +388,8 @@ public DefaultModelBuilder newInstance() { ? profileActivationFilePathInterpolator : newProfileActivationFilePathInterpolator(), versionProcessor != null ? versionProcessor : newModelVersionPropertiesProcessor(), - transformer != null ? transformer : newModelSourceTransformer()); + transformer != null ? transformer : newModelSourceTransformer(), + modelVersionParser != null ? modelVersionParser : newModelVersionParser()); } private static class StubLifecycleBindingsInjector implements LifecycleBindingsInjector { diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/version/ModelVersionParser.java b/maven-model-builder/src/main/java/org/apache/maven/model/version/ModelVersionParser.java new file mode 100644 index 000000000000..d2003d2dfb4b --- /dev/null +++ b/maven-model-builder/src/main/java/org/apache/maven/model/version/ModelVersionParser.java @@ -0,0 +1,53 @@ +/* + * 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. + */ +package org.apache.maven.model.version; + +import org.apache.maven.api.Version; +import org.apache.maven.api.VersionRange; +import org.apache.maven.api.annotations.Experimental; +import org.apache.maven.api.annotations.Nonnull; + +/** + * Component to parse {@link Version} and {@link VersionRange}. + * + * @since 4.0.0 + */ +@Experimental +public interface ModelVersionParser { + + /** + * Parses the specified version string, for example "1.0". + * + * @param version the version string to parse, must not be {@code null} + * @return the parsed version, never {@code null} + * @throws org.apache.maven.api.services.VersionParserException if the string violates the syntax rules of this scheme + */ + @Nonnull + Version parseVersion(@Nonnull String version); + + /** + * Parses the specified version range specification, for example "[1.0,2.0)". + * + * @param range the range specification to parse, must not be {@code null} + * @return the parsed version range, never {@code null} + * @throws org.apache.maven.api.services.VersionParserException if the range specification violates the syntax rules of this scheme + */ + @Nonnull + VersionRange parseVersionRange(@Nonnull String range); +} diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelVersionParser.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelVersionParser.java new file mode 100644 index 000000000000..9514e36b45d3 --- /dev/null +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelVersionParser.java @@ -0,0 +1,140 @@ +/* + * 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. + */ +package org.apache.maven.repository.internal; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; + +import java.util.Objects; + +import org.apache.maven.api.Version; +import org.apache.maven.api.VersionRange; +import org.apache.maven.api.services.VersionParserException; +import org.apache.maven.model.version.ModelVersionParser; +import org.eclipse.aether.version.InvalidVersionSpecificationException; +import org.eclipse.aether.version.VersionScheme; + +import static java.util.Objects.requireNonNull; + +@Named +@Singleton +public class DefaultModelVersionParser implements ModelVersionParser { + private final VersionScheme versionScheme; + + @Inject + public DefaultModelVersionParser(VersionScheme versionScheme) { + this.versionScheme = requireNonNull(versionScheme, "versionScheme"); + } + + @Override + public Version parseVersion(String version) { + requireNonNull(version, "version"); + return new DefaultVersion(versionScheme, version); + } + + @Override + public VersionRange parseVersionRange(String range) { + requireNonNull(range, "range"); + return new DefaultVersionRange(versionScheme, range); + } + + static class DefaultVersion implements Version { + private final VersionScheme versionScheme; + private final org.eclipse.aether.version.Version delegate; + + DefaultVersion(VersionScheme versionScheme, String delegateValue) { + this.versionScheme = versionScheme; + try { + this.delegate = versionScheme.parseVersion(delegateValue); + } catch (InvalidVersionSpecificationException e) { + throw new VersionParserException("Unable to parse version: " + delegateValue, e); + } + } + + @Override + public int compareTo(Version o) { + if (o instanceof DefaultVersion) { + return delegate.compareTo(((DefaultVersion) o).delegate); + } else { + return compareTo(new DefaultVersion(versionScheme, o.asString())); + } + } + + @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 VersionScheme versionScheme; + private final org.eclipse.aether.version.VersionRange delegate; + + DefaultVersionRange(VersionScheme versionScheme, String delegateValue) { + this.versionScheme = versionScheme; + try { + this.delegate = versionScheme.parseVersionRange(delegateValue); + } catch (InvalidVersionSpecificationException e) { + throw new VersionParserException("Unable to parse version range: " + delegateValue, e); + } + } + + @Override + public boolean contains(Version version) { + if (version instanceof DefaultVersion) { + return delegate.containsVersion(((DefaultVersion) version).delegate); + } else { + return contains(new DefaultVersion(versionScheme, version.asString())); + } + } + + @Override + public String asString() { + return delegate.toString(); + } + + @Override + public String toString() { + return asString(); + } + } +} From cd5f2ab01332f9ba7f81724c001d7d0d19a79b14 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Fri, 15 Dec 2023 11:12:22 +0100 Subject: [PATCH 05/21] Set up proper parser --- .../internal/transformation/impl/DefaultConsumerPomBuilder.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java b/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java index f825cd1c12d8..308651851e38 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java @@ -62,6 +62,7 @@ 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.ModelVersionParser; import org.apache.maven.project.MavenProject; import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.project.ProjectModelResolver; @@ -142,6 +143,7 @@ public List getActiveProfilesV4( .setPluginManagementInjector(lookup(PluginManagementInjector.class)) .setReportConfigurationExpander(lookup(ReportConfigurationExpander.class)) .setSuperPomProvider(lookup(SuperPomProvider.class)) + .setModelVersionParser(lookup(ModelVersionParser.class)) .newInstance(); DefaultModelBuildingRequest request = new DefaultModelBuildingRequest(); try { From affbef2c875735570fb7c4ca982f6f051e468d49 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Fri, 15 Dec 2023 11:23:15 +0100 Subject: [PATCH 06/21] Drop use of plexus container This is legacy way, make it proper --- .../impl/DefaultConsumerPomBuilder.java | 113 ++++++++++++------ 1 file changed, 78 insertions(+), 35 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java b/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java index 308651851e38..01c542e351fc 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java @@ -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; @@ -67,31 +68,77 @@ 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 Provider profileInjector; + + @Inject + private Provider inheritanceAssembler; + + @Inject + private Provider dependencyManagementImporter; + + @Inject + private Provider dependencyManagementInjector; + + @Inject + private Provider lifecycleBindingsInjector; + + @Inject + private Provider modelInterpolator; + + @Inject + private Provider modelNormalizer; + + @Inject + private Provider modelPathTranslator; + + @Inject + private Provider modelProcessor; @Inject - ModelCacheFactory modelCacheFactory; + private Provider modelUrlNormalizer; - public Model build(RepositorySystemSession session, MavenProject project, Path src) - throws ModelBuildingException, ComponentLookupException { + @Inject + private Provider modelValidator; + + @Inject + private Provider pluginConfigurationExpander; + + @Inject + private Provider pluginManagementInjector; + + @Inject + private Provider reportConfigurationExpander; + + @Inject + private Provider superPomProvider; + + @Inject + private Provider modelVersionParser; + + // To break circular dependency + @Inject + private Provider repositorySystem; + + @Inject + private Provider 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(); @@ -103,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 getActiveProfilesV4( @@ -128,22 +175,22 @@ public List 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)) - .setModelVersionParser(lookup(ModelVersionParser.class)) + .setProfileInjector(profileInjector.get()) + .setInheritanceAssembler(inheritanceAssembler.get()) + .setDependencyManagementImporter(dependencyManagementImporter.get()) + .setDependencyManagementInjector(dependencyManagementInjector.get()) + .setLifecycleBindingsInjector(lifecycleBindingsInjector.get()) + .setModelInterpolator(modelInterpolator.get()) + .setModelNormalizer(modelNormalizer.get()) + .setModelPathTranslator(modelPathTranslator.get()) + .setModelProcessor(modelProcessor.get()) + .setModelUrlNormalizer(modelUrlNormalizer.get()) + .setModelValidator(modelValidator.get()) + .setPluginConfigurationExpander(pluginConfigurationExpander.get()) + .setPluginManagementInjector(pluginManagementInjector.get()) + .setReportConfigurationExpander(reportConfigurationExpander.get()) + .setSuperPomProvider(superPomProvider.get()) + .setModelVersionParser(modelVersionParser.get()) .newInstance(); DefaultModelBuildingRequest request = new DefaultModelBuildingRequest(); try { @@ -157,8 +204,8 @@ public List getActiveProfilesV4( request.setModelResolver(new ProjectModelResolver( session, new RequestTrace(null), - lookup(RepositorySystem.class), - lookup(RemoteRepositoryManager.class), + repositorySystem.get(), + remoteRepositoryManager.get(), project.getRemoteProjectRepositories(), ProjectBuildingRequest.RepositoryMerging.POM_DOMINANT, null)); @@ -175,10 +222,6 @@ private Properties toProperties(Map map) { return props; } - private T lookup(Class clazz) throws ComponentLookupException { - return container.lookup(clazz); - } - static Model transform(Model model, MavenProject project) { String packaging = model.getPackaging(); if (POM_PACKAGING.equals(packaging)) { From 4d3f40d9f004261bd5e175688ba9748570f4b3b2 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Fri, 15 Dec 2023 11:39:56 +0100 Subject: [PATCH 07/21] Move nullchecks to their proper place --- .../org/apache/maven/internal/impl/DefaultVersionParser.java | 2 -- .../maven/model/building/DefaultModelBuilderFactory.java | 3 +++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java index a3d43b31918c..8f5b5b00ce85 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java +++ b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java @@ -46,13 +46,11 @@ public DefaultVersionParser(ModelVersionParser modelVersionParser) { @Override public Version parseVersion(String version) { - nonNull(version, "version"); return modelVersionParser.parseVersion(version); } @Override public VersionRange parseVersionRange(String range) { - nonNull(range, "range"); return modelVersionParser.parseVersionRange(range); } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilderFactory.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilderFactory.java index 68b022e4c046..1fac2ca98221 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilderFactory.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilderFactory.java @@ -75,6 +75,8 @@ import org.apache.maven.model.validation.ModelValidator; import org.apache.maven.model.version.ModelVersionParser; +import static java.util.Objects.requireNonNull; + /** * A factory to create model builder instances when no dependency injection is available. Note: This class is * only meant as a utility for developers that want to employ the model builder outside the Maven build system, Maven @@ -341,6 +343,7 @@ private ModelVersionParser newModelVersionParser() { return new ModelVersionParser() { @Override public Version parseVersion(String version) { + requireNonNull(version, "version"); return new Version() { @Override public String asString() { From 2c58486f93c7dbc608c0d3fda3e803618e5e07e4 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Fri, 15 Dec 2023 12:40:27 +0100 Subject: [PATCH 08/21] Use service directly --- .../maven/internal/impl/DefaultArtifact.java | 1 + .../internal/impl/DefaultDependency.java | 1 + .../internal/impl/DefaultVersionParser.java | 65 ------------------- .../impl/DefaultConsumerPomBuilder.java | 6 +- .../model/building/DefaultModelBuilder.java | 46 ++++++------- .../building/DefaultModelBuilderFactory.java | 19 ++++-- .../model/version/ModelVersionParser.java | 53 --------------- ...nParser.java => DefaultVersionParser.java} | 19 +++++- 8 files changed, 56 insertions(+), 154 deletions(-) delete mode 100644 maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java delete mode 100644 maven-model-builder/src/main/java/org/apache/maven/model/version/ModelVersionParser.java rename maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/{DefaultModelVersionParser.java => DefaultVersionParser.java} (87%) diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultArtifact.java b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultArtifact.java index 514d84c34c9b..a2f328c19981 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultArtifact.java +++ b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultArtifact.java @@ -24,6 +24,7 @@ import org.apache.maven.api.ArtifactCoordinate; import org.apache.maven.api.Version; import org.apache.maven.api.annotations.Nonnull; +import org.apache.maven.repository.internal.DefaultVersionParser; import static org.apache.maven.internal.impl.Utils.nonNull; diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultDependency.java b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultDependency.java index e1e17d5848aa..6de762d3873b 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultDependency.java +++ b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultDependency.java @@ -30,6 +30,7 @@ import org.apache.maven.api.annotations.Nonnull; import org.apache.maven.api.annotations.Nullable; import org.apache.maven.api.services.TypeRegistry; +import org.apache.maven.repository.internal.DefaultVersionParser; import org.eclipse.aether.artifact.ArtifactProperties; import static org.apache.maven.internal.impl.Utils.nonNull; diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java deleted file mode 100644 index 8f5b5b00ce85..000000000000 --- a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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. - */ -package org.apache.maven.internal.impl; - -import javax.inject.Inject; -import javax.inject.Named; -import javax.inject.Singleton; - -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.model.version.ModelVersionParser; - -import static org.apache.maven.internal.impl.Utils.nonNull; - -@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 ModelVersionParser modelVersionParser; - - @Inject - public DefaultVersionParser(ModelVersionParser modelVersionParser) { - this.modelVersionParser = nonNull(modelVersionParser, "modelVersionParser"); - } - - @Override - public Version parseVersion(String version) { - return modelVersionParser.parseVersion(version); - } - - @Override - public VersionRange parseVersionRange(String range) { - return modelVersionParser.parseVersionRange(range); - } - - @Override - public boolean isSnapshot(String version) { - return checkSnapshot(version); - } - - static boolean checkSnapshot(String version) { - return version.endsWith(SNAPSHOT) || SNAPSHOT_TIMESTAMP.matcher(version).matches(); - } -} diff --git a/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java b/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java index 01c542e351fc..b64240cc049a 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java @@ -37,6 +37,7 @@ import org.apache.maven.api.model.ModelBase; import org.apache.maven.api.model.Profile; import org.apache.maven.api.model.Repository; +import org.apache.maven.api.services.VersionParser; import org.apache.maven.model.building.DefaultModelBuilder; import org.apache.maven.model.building.DefaultModelBuilderFactory; import org.apache.maven.model.building.DefaultModelBuildingRequest; @@ -63,7 +64,6 @@ 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.ModelVersionParser; import org.apache.maven.project.MavenProject; import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.project.ProjectModelResolver; @@ -128,7 +128,7 @@ class DefaultConsumerPomBuilder implements ConsumerPomBuilder { private Provider superPomProvider; @Inject - private Provider modelVersionParser; + private Provider versionParser; // To break circular dependency @Inject @@ -190,7 +190,7 @@ public List getActiveProfilesV4( .setPluginManagementInjector(pluginManagementInjector.get()) .setReportConfigurationExpander(reportConfigurationExpander.get()) .setSuperPomProvider(superPomProvider.get()) - .setModelVersionParser(modelVersionParser.get()) + .setModelVersionParser(versionParser.get()) .newInstance(); DefaultModelBuildingRequest request = new DefaultModelBuildingRequest(); try { diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java index f5e9f1eb9e08..fbfa8f0c1c12 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java @@ -36,6 +36,7 @@ import org.apache.maven.api.feature.Features; import org.apache.maven.api.model.Exclusion; import org.apache.maven.api.model.InputSource; +import org.apache.maven.api.services.VersionParser; import org.apache.maven.api.services.VersionParserException; import org.apache.maven.building.Source; import org.apache.maven.model.Activation; @@ -76,7 +77,6 @@ import org.apache.maven.model.superpom.SuperPomProvider; import org.apache.maven.model.validation.DefaultModelValidator; import org.apache.maven.model.validation.ModelValidator; -import org.apache.maven.model.version.ModelVersionParser; import org.codehaus.plexus.interpolation.InterpolationException; import org.codehaus.plexus.interpolation.MapBasedValueSource; import org.codehaus.plexus.interpolation.StringSearchInterpolator; @@ -110,7 +110,7 @@ public class DefaultModelBuilder implements ModelBuilder { private final ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator; private final ModelVersionProcessor versionProcessor; private final ModelSourceTransformer transformer; - private final ModelVersionParser modelVersionParser; + private final VersionParser versionParser; @SuppressWarnings("checkstyle:ParameterNumber") @Inject @@ -134,7 +134,7 @@ public DefaultModelBuilder( ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator, ModelVersionProcessor versionProcessor, ModelSourceTransformer transformer, - ModelVersionParser modelVersionParser) { + VersionParser versionParser) { this.modelProcessor = modelProcessor; this.modelValidator = modelValidator; this.modelNormalizer = modelNormalizer; @@ -154,7 +154,7 @@ public DefaultModelBuilder( this.profileActivationFilePathInterpolator = profileActivationFilePathInterpolator; this.versionProcessor = versionProcessor; this.transformer = transformer; - this.modelVersionParser = modelVersionParser; + this.versionParser = versionParser; } /** @@ -183,7 +183,7 @@ public DefaultModelBuilder setModelProcessor(ModelProcessor modelProcessor) { profileActivationFilePathInterpolator, versionProcessor, transformer, - modelVersionParser); + versionParser); } /** @@ -212,7 +212,7 @@ public DefaultModelBuilder setModelValidator(ModelValidator modelValidator) { profileActivationFilePathInterpolator, versionProcessor, transformer, - modelVersionParser); + versionParser); } /** @@ -241,7 +241,7 @@ public DefaultModelBuilder setModelNormalizer(ModelNormalizer modelNormalizer) { profileActivationFilePathInterpolator, versionProcessor, transformer, - modelVersionParser); + versionParser); } /** @@ -270,7 +270,7 @@ public DefaultModelBuilder setModelInterpolator(ModelInterpolator modelInterpola profileActivationFilePathInterpolator, versionProcessor, transformer, - modelVersionParser); + versionParser); } /** @@ -299,7 +299,7 @@ public DefaultModelBuilder setModelPathTranslator(ModelPathTranslator modelPathT profileActivationFilePathInterpolator, versionProcessor, transformer, - modelVersionParser); + versionParser); } /** @@ -328,7 +328,7 @@ public DefaultModelBuilder setModelUrlNormalizer(ModelUrlNormalizer modelUrlNorm profileActivationFilePathInterpolator, versionProcessor, transformer, - modelVersionParser); + versionParser); } /** @@ -357,7 +357,7 @@ public DefaultModelBuilder setSuperPomProvider(SuperPomProvider superPomProvider profileActivationFilePathInterpolator, versionProcessor, transformer, - modelVersionParser); + versionParser); } /** @@ -386,7 +386,7 @@ public DefaultModelBuilder setInheritanceAssembler(InheritanceAssembler inherita profileActivationFilePathInterpolator, versionProcessor, transformer, - modelVersionParser); + versionParser); } /** @@ -415,7 +415,7 @@ public DefaultModelBuilder setProfileSelector(ProfileSelector profileSelector) { profileActivationFilePathInterpolator, versionProcessor, transformer, - modelVersionParser); + versionParser); } /** @@ -444,7 +444,7 @@ public DefaultModelBuilder setProfileInjector(ProfileInjector profileInjector) { profileActivationFilePathInterpolator, versionProcessor, transformer, - modelVersionParser); + versionParser); } /** @@ -473,7 +473,7 @@ public DefaultModelBuilder setPluginManagementInjector(PluginManagementInjector profileActivationFilePathInterpolator, versionProcessor, transformer, - modelVersionParser); + versionParser); } /** @@ -503,7 +503,7 @@ public DefaultModelBuilder setDependencyManagementInjector( profileActivationFilePathInterpolator, versionProcessor, transformer, - modelVersionParser); + versionParser); } /** @@ -533,7 +533,7 @@ public DefaultModelBuilder setDependencyManagementImporter( profileActivationFilePathInterpolator, versionProcessor, transformer, - modelVersionParser); + versionParser); } /** @@ -562,7 +562,7 @@ public DefaultModelBuilder setLifecycleBindingsInjector(LifecycleBindingsInjecto profileActivationFilePathInterpolator, versionProcessor, transformer, - modelVersionParser); + versionParser); } /** @@ -591,7 +591,7 @@ public DefaultModelBuilder setPluginConfigurationExpander(PluginConfigurationExp profileActivationFilePathInterpolator, versionProcessor, transformer, - modelVersionParser); + versionParser); } /** @@ -620,7 +620,7 @@ public DefaultModelBuilder setReportConfigurationExpander(ReportConfigurationExp profileActivationFilePathInterpolator, versionProcessor, transformer, - modelVersionParser); + versionParser); } /** @@ -650,7 +650,7 @@ public DefaultModelBuilder setProfileActivationFilePathInterpolator( profileActivationFilePathInterpolator, versionProcessor, transformer, - modelVersionParser); + versionParser); } /** @@ -1505,8 +1505,8 @@ private ModelData readParentLocally( String version = getVersion(candidateModel); if (version != null && parent.getVersion() != null && !version.equals(parent.getVersion())) { try { - VersionRange parentRange = modelVersionParser.parseVersionRange(parent.getVersion()); - if (!parentRange.contains(modelVersionParser.parseVersion(version))) { + VersionRange parentRange = versionParser.parseVersionRange(parent.getVersion()); + if (!parentRange.contains(versionParser.parseVersion(version))) { // version skew drop back to resolution from the repository return null; } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilderFactory.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilderFactory.java index 1fac2ca98221..f3e4431c345c 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilderFactory.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilderFactory.java @@ -22,6 +22,7 @@ import org.apache.maven.api.Version; import org.apache.maven.api.VersionRange; +import org.apache.maven.api.services.VersionParser; import org.apache.maven.api.spi.ModelParser; import org.apache.maven.model.Model; import org.apache.maven.model.composition.DefaultDependencyManagementImporter; @@ -73,7 +74,6 @@ import org.apache.maven.model.superpom.SuperPomProvider; import org.apache.maven.model.validation.DefaultModelValidator; import org.apache.maven.model.validation.ModelValidator; -import org.apache.maven.model.version.ModelVersionParser; import static java.util.Objects.requireNonNull; @@ -106,7 +106,7 @@ public class DefaultModelBuilderFactory { private ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator; private ModelVersionProcessor versionProcessor; private ModelSourceTransformer transformer; - private ModelVersionParser modelVersionParser; + private VersionParser versionParser; public DefaultModelBuilderFactory setModelProcessor(ModelProcessor modelProcessor) { this.modelProcessor = modelProcessor; @@ -214,8 +214,8 @@ public DefaultModelBuilderFactory setTransformer(ModelSourceTransformer transfor return this; } - public DefaultModelBuilderFactory setModelVersionParser(ModelVersionParser modelVersionParser) { - this.modelVersionParser = modelVersionParser; + public DefaultModelBuilderFactory setModelVersionParser(VersionParser versionParser) { + this.versionParser = versionParser; return this; } @@ -337,10 +337,10 @@ private ModelSourceTransformer newModelSourceTransformer() { return new BuildModelSourceTransformer(); } - private ModelVersionParser newModelVersionParser() { + private VersionParser newModelVersionParser() { // This is a limited parser that does not support ranges and compares versions as strings // in real-life this parser should not be used, but replaced with a proper one - return new ModelVersionParser() { + return new VersionParser() { @Override public Version parseVersion(String version) { requireNonNull(version, "version"); @@ -361,6 +361,11 @@ public int compareTo(Version o) { public VersionRange parseVersionRange(String range) { throw new IllegalArgumentException("ranges not supported by this parser"); } + + @Override + public boolean isSnapshot(String version) { + return version.endsWith("SNAPSHOT"); + } }; } @@ -392,7 +397,7 @@ public DefaultModelBuilder newInstance() { : newProfileActivationFilePathInterpolator(), versionProcessor != null ? versionProcessor : newModelVersionPropertiesProcessor(), transformer != null ? transformer : newModelSourceTransformer(), - modelVersionParser != null ? modelVersionParser : newModelVersionParser()); + versionParser != null ? versionParser : newModelVersionParser()); } private static class StubLifecycleBindingsInjector implements LifecycleBindingsInjector { diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/version/ModelVersionParser.java b/maven-model-builder/src/main/java/org/apache/maven/model/version/ModelVersionParser.java deleted file mode 100644 index d2003d2dfb4b..000000000000 --- a/maven-model-builder/src/main/java/org/apache/maven/model/version/ModelVersionParser.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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. - */ -package org.apache.maven.model.version; - -import org.apache.maven.api.Version; -import org.apache.maven.api.VersionRange; -import org.apache.maven.api.annotations.Experimental; -import org.apache.maven.api.annotations.Nonnull; - -/** - * Component to parse {@link Version} and {@link VersionRange}. - * - * @since 4.0.0 - */ -@Experimental -public interface ModelVersionParser { - - /** - * Parses the specified version string, for example "1.0". - * - * @param version the version string to parse, must not be {@code null} - * @return the parsed version, never {@code null} - * @throws org.apache.maven.api.services.VersionParserException if the string violates the syntax rules of this scheme - */ - @Nonnull - Version parseVersion(@Nonnull String version); - - /** - * Parses the specified version range specification, for example "[1.0,2.0)". - * - * @param range the range specification to parse, must not be {@code null} - * @return the parsed version range, never {@code null} - * @throws org.apache.maven.api.services.VersionParserException if the range specification violates the syntax rules of this scheme - */ - @Nonnull - VersionRange parseVersionRange(@Nonnull String range); -} diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelVersionParser.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionParser.java similarity index 87% rename from maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelVersionParser.java rename to maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionParser.java index 9514e36b45d3..e4dc14b4abe8 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelVersionParser.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionParser.java @@ -23,11 +23,12 @@ 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.model.version.ModelVersionParser; import org.eclipse.aether.version.InvalidVersionSpecificationException; import org.eclipse.aether.version.VersionScheme; @@ -35,11 +36,14 @@ @Named @Singleton -public class DefaultModelVersionParser implements ModelVersionParser { +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 VersionScheme versionScheme; @Inject - public DefaultModelVersionParser(VersionScheme versionScheme) { + public DefaultVersionParser(VersionScheme versionScheme) { this.versionScheme = requireNonNull(versionScheme, "versionScheme"); } @@ -55,6 +59,15 @@ public VersionRange parseVersionRange(String range) { return new DefaultVersionRange(versionScheme, range); } + @Override + public boolean isSnapshot(String version) { + return checkSnapshot(version); + } + + public static boolean checkSnapshot(String version) { + return version.endsWith(SNAPSHOT) || SNAPSHOT_TIMESTAMP.matcher(version).matches(); + } + static class DefaultVersion implements Version { private final VersionScheme versionScheme; private final org.eclipse.aether.version.Version delegate; From a6db4ca179a7b82f6c030943effae3321c00e872 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Fri, 15 Dec 2023 13:55:12 +0100 Subject: [PATCH 09/21] Do not use selector --- .../java/org/apache/maven/DefaultMaven.java | 10 +-- .../maven/internal/impl/DefaultArtifact.java | 1 - .../internal/impl/DefaultDependency.java | 1 - .../internal/impl/DefaultVersionParser.java | 65 +++++++++++++++++++ .../impl/DefaultConsumerPomBuilder.java | 2 +- .../DefaultPluginVersionResolver.java | 13 ++-- .../model/building/DefaultModelBuilder.java | 2 +- .../building/DefaultModelBuilderFactory.java | 7 +- .../maven/model/version/VersionParser.java | 54 +++++++++++++++ .../internal/DefaultVersionParser.java | 26 ++------ .../internal/DefaultVersionRangeResolver.java | 23 +++---- 11 files changed, 145 insertions(+), 59 deletions(-) create mode 100644 maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java create mode 100644 maven-model-builder/src/main/java/org/apache/maven/model/version/VersionParser.java diff --git a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java index 107537803ace..10d28bc438a7 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java @@ -76,8 +76,6 @@ import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.RepositorySystemSession.CloseableSession; import org.eclipse.aether.repository.WorkspaceReader; -import org.eclipse.aether.spi.version.VersionSchemeSelector; -import org.eclipse.aether.version.VersionScheme; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.helpers.MessageFormatter; @@ -115,8 +113,6 @@ public class DefaultMaven implements Maven { private final DefaultSessionFactory defaultSessionFactory; - private final VersionSchemeSelector versionSchemeSelector; - private final ProjectSelector projectSelector; @Inject @@ -133,8 +129,7 @@ public DefaultMaven( BuildResumptionAnalyzer buildResumptionAnalyzer, BuildResumptionDataRepository buildResumptionDataRepository, SuperPomProvider superPomProvider, - DefaultSessionFactory defaultSessionFactory, - VersionSchemeSelector versionSchemeSelector) { + DefaultSessionFactory defaultSessionFactory) { this.projectBuilder = projectBuilder; this.lifecycleStarter = lifecycleStarter; this.container = container; @@ -147,7 +142,6 @@ public DefaultMaven( this.buildResumptionDataRepository = buildResumptionDataRepository; this.superPomProvider = superPomProvider; this.defaultSessionFactory = defaultSessionFactory; - this.versionSchemeSelector = versionSchemeSelector; this.projectSelector = new ProjectSelector(); // if necessary switch to DI } @@ -228,8 +222,6 @@ private MavenExecutionResult doExecute(MavenExecutionRequest request) { sessionScope.seed(MavenSession.class, session); sessionScope.seed(Session.class, session.getSession()); sessionScope.seed(InternalSession.class, InternalSession.from(session.getSession())); - sessionScope.seed( - VersionScheme.class, versionSchemeSelector.selectVersionScheme(session.getRepositorySession())); legacySupport.setSession(session); diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultArtifact.java b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultArtifact.java index a2f328c19981..514d84c34c9b 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultArtifact.java +++ b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultArtifact.java @@ -24,7 +24,6 @@ import org.apache.maven.api.ArtifactCoordinate; import org.apache.maven.api.Version; import org.apache.maven.api.annotations.Nonnull; -import org.apache.maven.repository.internal.DefaultVersionParser; import static org.apache.maven.internal.impl.Utils.nonNull; diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultDependency.java b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultDependency.java index 6de762d3873b..e1e17d5848aa 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultDependency.java +++ b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultDependency.java @@ -30,7 +30,6 @@ import org.apache.maven.api.annotations.Nonnull; import org.apache.maven.api.annotations.Nullable; import org.apache.maven.api.services.TypeRegistry; -import org.apache.maven.repository.internal.DefaultVersionParser; import org.eclipse.aether.artifact.ArtifactProperties; import static org.apache.maven.internal.impl.Utils.nonNull; diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java new file mode 100644 index 000000000000..6c72bab136fb --- /dev/null +++ b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java @@ -0,0 +1,65 @@ +/* + * 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. + */ +package org.apache.maven.internal.impl; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; + +import java.util.regex.Pattern; + +import org.apache.maven.api.Version; +import org.apache.maven.api.VersionRange; +import org.apache.maven.api.services.VersionParser; + +/** + * A wrapper class around a maven resolver artifact. + */ +@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 versionParser; + + @Inject + public DefaultVersionParser(org.apache.maven.model.version.VersionParser versionParser) { + this.versionParser = versionParser; + } + + @Override + public Version parseVersion(String version) { + return versionParser.parseVersion(version); + } + + @Override + public VersionRange parseVersionRange(String range) { + return versionParser.parseVersionRange(range); + } + + @Override + public boolean isSnapshot(String version) { + return checkSnapshot(version); + } + + static boolean checkSnapshot(String version) { + return version.endsWith(SNAPSHOT) || SNAPSHOT_TIMESTAMP.matcher(version).matches(); + } +} diff --git a/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java b/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java index b64240cc049a..6db2cab138eb 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java @@ -37,7 +37,6 @@ import org.apache.maven.api.model.ModelBase; import org.apache.maven.api.model.Profile; import org.apache.maven.api.model.Repository; -import org.apache.maven.api.services.VersionParser; import org.apache.maven.model.building.DefaultModelBuilder; import org.apache.maven.model.building.DefaultModelBuilderFactory; import org.apache.maven.model.building.DefaultModelBuildingRequest; @@ -64,6 +63,7 @@ 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; diff --git a/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java b/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java index e81c1b7f6e06..310e3aa6037b 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java @@ -20,6 +20,7 @@ import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Provider; import javax.inject.Singleton; import java.io.IOException; @@ -57,9 +58,9 @@ import org.eclipse.aether.repository.RemoteRepository; import org.eclipse.aether.resolution.MetadataRequest; import org.eclipse.aether.resolution.MetadataResult; -import org.eclipse.aether.spi.version.VersionSchemeSelector; import org.eclipse.aether.version.InvalidVersionSpecificationException; import org.eclipse.aether.version.Version; +import org.eclipse.aether.version.VersionScheme; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -79,18 +80,18 @@ public class DefaultPluginVersionResolver implements PluginVersionResolver { private final RepositorySystem repositorySystem; private final MetadataReader metadataReader; private final MavenPluginManager pluginManager; - private final VersionSchemeSelector versionSchemeSelector; + private final Provider versionSchemeProvider; @Inject public DefaultPluginVersionResolver( RepositorySystem repositorySystem, MetadataReader metadataReader, MavenPluginManager pluginManager, - VersionSchemeSelector versionSchemeSelector) { + Provider versionSchemeProvider) { this.repositorySystem = repositorySystem; this.metadataReader = metadataReader; this.pluginManager = pluginManager; - this.versionSchemeSelector = versionSchemeSelector; + this.versionSchemeProvider = versionSchemeProvider; } @Override @@ -194,9 +195,7 @@ private void selectVersion(DefaultPluginVersionResult result, PluginVersionReque for (String ver : versions.versions.keySet()) { try { - Version v = versionSchemeSelector - .selectVersionScheme(request.getRepositorySession()) - .parseVersion(ver); + Version v = versionSchemeProvider.get().parseVersion(ver); if (ver.endsWith("-SNAPSHOT")) { snapshots.add(v); diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java index fbfa8f0c1c12..b4c51537181d 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java @@ -36,7 +36,6 @@ import org.apache.maven.api.feature.Features; import org.apache.maven.api.model.Exclusion; import org.apache.maven.api.model.InputSource; -import org.apache.maven.api.services.VersionParser; import org.apache.maven.api.services.VersionParserException; import org.apache.maven.building.Source; import org.apache.maven.model.Activation; @@ -77,6 +76,7 @@ import org.apache.maven.model.superpom.SuperPomProvider; import org.apache.maven.model.validation.DefaultModelValidator; import org.apache.maven.model.validation.ModelValidator; +import org.apache.maven.model.version.VersionParser; import org.codehaus.plexus.interpolation.InterpolationException; import org.codehaus.plexus.interpolation.MapBasedValueSource; import org.codehaus.plexus.interpolation.StringSearchInterpolator; diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilderFactory.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilderFactory.java index f3e4431c345c..de7b99eb6c2f 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilderFactory.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilderFactory.java @@ -22,7 +22,6 @@ import org.apache.maven.api.Version; import org.apache.maven.api.VersionRange; -import org.apache.maven.api.services.VersionParser; import org.apache.maven.api.spi.ModelParser; import org.apache.maven.model.Model; import org.apache.maven.model.composition.DefaultDependencyManagementImporter; @@ -74,6 +73,7 @@ import org.apache.maven.model.superpom.SuperPomProvider; import org.apache.maven.model.validation.DefaultModelValidator; import org.apache.maven.model.validation.ModelValidator; +import org.apache.maven.model.version.VersionParser; import static java.util.Objects.requireNonNull; @@ -361,11 +361,6 @@ public int compareTo(Version o) { public VersionRange parseVersionRange(String range) { throw new IllegalArgumentException("ranges not supported by this parser"); } - - @Override - public boolean isSnapshot(String version) { - return version.endsWith("SNAPSHOT"); - } }; } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/version/VersionParser.java b/maven-model-builder/src/main/java/org/apache/maven/model/version/VersionParser.java new file mode 100644 index 000000000000..f6eaebefe8d7 --- /dev/null +++ b/maven-model-builder/src/main/java/org/apache/maven/model/version/VersionParser.java @@ -0,0 +1,54 @@ +/* + * 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. + */ +package org.apache.maven.model.version; + +import org.apache.maven.api.Version; +import org.apache.maven.api.VersionRange; +import org.apache.maven.api.annotations.Nonnull; +import org.apache.maven.api.services.VersionParserException; + +/** + * Model builder specific version parser. It is intentionally not + * {@link org.apache.maven.api.services.VersionParser} as this is not a service, + * but at Maven runtime it MAY actually use that service. + * + * @since 4.0.0 + */ +public interface VersionParser { + + /** + * Parses the specified version string, for example "1.0". + * + * @param version the version string to parse, must not be {@code null} + * @return the parsed version, never {@code null} + * @throws VersionParserException if the string violates the syntax rules of this scheme + */ + @Nonnull + Version parseVersion(@Nonnull String version); + + /** + * Parses the specified version range specification, for example "[1.0,2.0)". + * + * @param range the range specification to parse, must not be {@code null} + * @return the parsed version range, never {@code null} + * @throws VersionParserException if the range specification violates the syntax rules of this scheme + */ + @Nonnull + VersionRange parseVersionRange(@Nonnull String range); +} diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionParser.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionParser.java index e4dc14b4abe8..b2cae9d42ef6 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionParser.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionParser.java @@ -20,15 +20,15 @@ import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Provider; 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.model.version.VersionParser; import org.eclipse.aether.version.InvalidVersionSpecificationException; import org.eclipse.aether.version.VersionScheme; @@ -37,35 +37,23 @@ @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 VersionScheme versionScheme; + private final Provider versionSchemeProvider; @Inject - public DefaultVersionParser(VersionScheme versionScheme) { - this.versionScheme = requireNonNull(versionScheme, "versionScheme"); + public DefaultVersionParser(Provider versionSchemeProvider) { + this.versionSchemeProvider = requireNonNull(versionSchemeProvider, "versionSchemeProvider"); } @Override public Version parseVersion(String version) { requireNonNull(version, "version"); - return new DefaultVersion(versionScheme, version); + return new DefaultVersion(versionSchemeProvider.get(), version); } @Override public VersionRange parseVersionRange(String range) { requireNonNull(range, "range"); - return new DefaultVersionRange(versionScheme, range); - } - - @Override - public boolean isSnapshot(String version) { - return checkSnapshot(version); - } - - public static boolean checkSnapshot(String version) { - return version.endsWith(SNAPSHOT) || SNAPSHOT_TIMESTAMP.matcher(version).matches(); + return new DefaultVersionRange(versionSchemeProvider.get(), range); } static class DefaultVersion implements Version { diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java index b253705c80fa..1635bda1b266 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java @@ -20,6 +20,7 @@ import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Provider; import javax.inject.Singleton; import java.io.InputStream; @@ -53,11 +54,7 @@ import org.eclipse.aether.resolution.VersionRangeResolutionException; import org.eclipse.aether.resolution.VersionRangeResult; import org.eclipse.aether.spi.synccontext.SyncContextFactory; -import org.eclipse.aether.spi.version.VersionSchemeSelector; -import org.eclipse.aether.version.InvalidVersionSpecificationException; -import org.eclipse.aether.version.Version; -import org.eclipse.aether.version.VersionConstraint; -import org.eclipse.aether.version.VersionRange; +import org.eclipse.aether.version.*; /** */ @@ -70,20 +67,20 @@ public class DefaultVersionRangeResolver implements VersionRangeResolver { private final MetadataResolver metadataResolver; private final SyncContextFactory syncContextFactory; private final RepositoryEventDispatcher repositoryEventDispatcher; - private final VersionSchemeSelector versionSchemeSelector; + private final Provider versionSchemeProvider; @Inject public DefaultVersionRangeResolver( MetadataResolver metadataResolver, SyncContextFactory syncContextFactory, RepositoryEventDispatcher repositoryEventDispatcher, - VersionSchemeSelector versionSchemeSelector) { + Provider versionSchemeProvider) { 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.versionSchemeSelector = - Objects.requireNonNull(versionSchemeSelector, "versionSchemeSelector cannot be null"); + this.versionSchemeProvider = + Objects.requireNonNull(versionSchemeProvider, "versionSchemeProvider cannot be null"); } @Override @@ -93,8 +90,8 @@ public VersionRangeResult resolveVersionRange(RepositorySystemSession session, V VersionConstraint versionConstraint; try { - versionConstraint = versionSchemeSelector - .selectVersionScheme(session) + versionConstraint = versionSchemeProvider + .get() .parseVersionConstraint(request.getArtifact().getVersion()); } catch (InvalidVersionSpecificationException e) { result.addException(e); @@ -116,9 +113,7 @@ public VersionRangeResult resolveVersionRange(RepositorySystemSession session, V List versions = new ArrayList<>(); for (Map.Entry v : versionIndex.entrySet()) { try { - Version ver = versionSchemeSelector - .selectVersionScheme(session) - .parseVersion(v.getKey()); + Version ver = versionSchemeProvider.get().parseVersion(v.getKey()); if (versionConstraint.containsVersion(ver)) { versions.add(ver); result.setRepository(ver, v.getValue()); From 38d5b9e5250acaef72b4b1ae048d3772f4085da4 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Fri, 15 Dec 2023 15:41:49 +0100 Subject: [PATCH 10/21] Make it per session --- .../internal/impl/DefaultVersionParser.java | 114 +++++++++++++++++- .../MavenPluginJavaPrerequisiteChecker.java | 17 ++- .../MavenPluginMavenPrerequisiteChecker.java | 16 ++- .../DefaultPluginVersionResolver.java | 16 ++- ...ginParameterExpressionEvaluatorV4Test.java | 23 +++- ...avenPluginJavaPrerequisiteCheckerTest.java | 16 ++- .../DefaultMavenProjectBuilderTest.java | 19 +++ .../internal/DefaultVersionRangeResolver.java | 24 ++-- .../internal/util/TestVersionParser.java} | 6 +- 9 files changed, 212 insertions(+), 39 deletions(-) rename maven-resolver-provider/src/{main/java/org/apache/maven/repository/internal/DefaultVersionParser.java => test/java/org/apache/maven/repository/internal/util/TestVersionParser.java} (96%) diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java index 6c72bab136fb..c060f7d706cc 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java +++ b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java @@ -20,38 +20,57 @@ import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Provider; 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.eclipse.aether.spi.version.VersionSchemeSelector; +import org.eclipse.aether.version.InvalidVersionSpecificationException; +import org.eclipse.aether.version.VersionScheme; + +import static java.util.Objects.requireNonNull; /** * A wrapper class around a maven resolver artifact. */ @Named @Singleton -public class DefaultVersionParser implements VersionParser { +public class DefaultVersionParser implements VersionParser, org.apache.maven.model.version.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 versionParser; + private final Provider internalSessionProvider; + private final VersionSchemeSelector versionSchemeSelector; @Inject - public DefaultVersionParser(org.apache.maven.model.version.VersionParser versionParser) { - this.versionParser = versionParser; + public DefaultVersionParser( + Provider internalSessionProvider, VersionSchemeSelector versionSchemeSelector) { + this.internalSessionProvider = internalSessionProvider; + this.versionSchemeSelector = versionSchemeSelector; } @Override public Version parseVersion(String version) { - return versionParser.parseVersion(version); + requireNonNull(version, "version"); + return new DefaultVersion( + versionSchemeSelector.selectVersionScheme( + internalSessionProvider.get().getSession()), + version); } @Override public VersionRange parseVersionRange(String range) { - return versionParser.parseVersionRange(range); + requireNonNull(range, "range"); + return new DefaultVersionRange( + versionSchemeSelector.selectVersionScheme( + internalSessionProvider.get().getSession()), + range); } @Override @@ -62,4 +81,87 @@ 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 VersionScheme versionScheme; + private final org.eclipse.aether.version.Version delegate; + + DefaultVersion(VersionScheme versionScheme, String delegateValue) { + this.versionScheme = versionScheme; + try { + this.delegate = versionScheme.parseVersion(delegateValue); + } catch (InvalidVersionSpecificationException e) { + throw new VersionParserException("Unable to parse version: " + delegateValue, e); + } + } + + @Override + public int compareTo(Version o) { + if (o instanceof DefaultVersion) { + return delegate.compareTo(((DefaultVersion) o).delegate); + } else { + return compareTo(new DefaultVersion(versionScheme, o.asString())); + } + } + + @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 VersionScheme versionScheme; + private final org.eclipse.aether.version.VersionRange delegate; + + DefaultVersionRange(VersionScheme versionScheme, String delegateValue) { + this.versionScheme = versionScheme; + try { + this.delegate = versionScheme.parseVersionRange(delegateValue); + } catch (InvalidVersionSpecificationException e) { + throw new VersionParserException("Unable to parse version range: " + delegateValue, e); + } + } + + @Override + public boolean contains(Version version) { + if (version instanceof DefaultVersion) { + return delegate.containsVersion(((DefaultVersion) version).delegate); + } else { + return contains(new DefaultVersion(versionScheme, version.asString())); + } + } + + @Override + public String asString() { + return delegate.toString(); + } + + @Override + public String toString() { + return asString(); + } + } } diff --git a/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteChecker.java b/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteChecker.java index cb0ecaf4aabb..cbf9e6024fe3 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteChecker.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteChecker.java @@ -23,8 +23,10 @@ import javax.inject.Provider; import javax.inject.Singleton; +import org.apache.maven.internal.impl.InternalSession; import org.apache.maven.plugin.MavenPluginPrerequisitesChecker; import org.apache.maven.plugin.descriptor.PluginDescriptor; +import org.eclipse.aether.spi.version.VersionSchemeSelector; import org.eclipse.aether.version.InvalidVersionSpecificationException; import org.eclipse.aether.version.Version; import org.eclipse.aether.version.VersionConstraint; @@ -34,11 +36,14 @@ @Singleton public class MavenPluginJavaPrerequisiteChecker implements MavenPluginPrerequisitesChecker { - private final Provider versionSchemeProvider; + private final Provider internalSessionProvider; + private final VersionSchemeSelector versionSchemeSelector; @Inject - public MavenPluginJavaPrerequisiteChecker(Provider versionSchemeProvider) { - this.versionSchemeProvider = versionSchemeProvider; + public MavenPluginJavaPrerequisiteChecker( + Provider internalSessionProvider, VersionSchemeSelector versionSchemeSelector) { + this.internalSessionProvider = internalSessionProvider; + this.versionSchemeSelector = versionSchemeSelector; } @Override @@ -54,15 +59,17 @@ public void accept(PluginDescriptor pluginDescriptor) { } boolean matchesVersion(String requiredVersion, String currentVersion) { + VersionScheme versionScheme = versionSchemeSelector.selectVersionScheme( + internalSessionProvider.get().getSession()); VersionConstraint constraint; try { - constraint = versionSchemeProvider.get().parseVersionConstraint(requiredVersion); + constraint = versionScheme.parseVersionConstraint(requiredVersion); } catch (InvalidVersionSpecificationException e) { throw new IllegalArgumentException("Invalid 'requiredJavaVersion' given in plugin descriptor", e); } Version current; try { - current = versionSchemeProvider.get().parseVersion(currentVersion); + current = versionScheme.parseVersion(currentVersion); } catch (InvalidVersionSpecificationException e) { throw new IllegalStateException("Could not parse current Java version", e); } diff --git a/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginMavenPrerequisiteChecker.java b/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginMavenPrerequisiteChecker.java index ead151f64e50..3b71560d3cb2 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginMavenPrerequisiteChecker.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginMavenPrerequisiteChecker.java @@ -23,9 +23,11 @@ import javax.inject.Provider; import javax.inject.Singleton; +import org.apache.maven.internal.impl.InternalSession; import org.apache.maven.plugin.MavenPluginPrerequisitesChecker; import org.apache.maven.plugin.descriptor.PluginDescriptor; import org.apache.maven.rtinfo.RuntimeInformation; +import org.eclipse.aether.spi.version.VersionSchemeSelector; import org.eclipse.aether.version.InvalidVersionSpecificationException; import org.eclipse.aether.version.Version; import org.eclipse.aether.version.VersionConstraint; @@ -39,14 +41,17 @@ public class MavenPluginMavenPrerequisiteChecker implements MavenPluginPrerequis private final Logger logger = LoggerFactory.getLogger(getClass()); private final RuntimeInformation runtimeInformation; - - private final Provider versionSchemeProvider; + private final Provider internalSessionProvider; + private final VersionSchemeSelector versionSchemeSelector; @Inject public MavenPluginMavenPrerequisiteChecker( - RuntimeInformation runtimeInformation, Provider versionSchemeProvider) { + RuntimeInformation runtimeInformation, + Provider internalSessionProvider, + VersionSchemeSelector versionSchemeSelector) { this.runtimeInformation = runtimeInformation; - this.versionSchemeProvider = versionSchemeProvider; + this.internalSessionProvider = internalSessionProvider; + this.versionSchemeSelector = versionSchemeSelector; } @Override @@ -57,7 +62,8 @@ public void accept(PluginDescriptor pluginDescriptor) { requiredMavenVersion == null || requiredMavenVersion.trim().isEmpty(); if (!isBlankVersion) { - VersionScheme versionScheme = versionSchemeProvider.get(); // this must happen within session + VersionScheme versionScheme = versionSchemeSelector.selectVersionScheme( + internalSessionProvider.get().getSession()); // this must happen within session VersionConstraint constraint; try { constraint = versionScheme.parseVersionConstraint(requiredMavenVersion); diff --git a/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java b/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java index 310e3aa6037b..ba1c51440605 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java @@ -37,6 +37,7 @@ import org.apache.maven.artifact.repository.metadata.Metadata; import org.apache.maven.artifact.repository.metadata.Versioning; import org.apache.maven.artifact.repository.metadata.io.MetadataReader; +import org.apache.maven.internal.impl.InternalSession; import org.apache.maven.model.Build; import org.apache.maven.model.Plugin; import org.apache.maven.plugin.MavenPluginManager; @@ -58,9 +59,9 @@ import org.eclipse.aether.repository.RemoteRepository; import org.eclipse.aether.resolution.MetadataRequest; import org.eclipse.aether.resolution.MetadataResult; +import org.eclipse.aether.spi.version.VersionSchemeSelector; import org.eclipse.aether.version.InvalidVersionSpecificationException; import org.eclipse.aether.version.Version; -import org.eclipse.aether.version.VersionScheme; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -80,18 +81,21 @@ public class DefaultPluginVersionResolver implements PluginVersionResolver { private final RepositorySystem repositorySystem; private final MetadataReader metadataReader; private final MavenPluginManager pluginManager; - private final Provider versionSchemeProvider; + private final Provider internalSessionProvider; + private final VersionSchemeSelector versionSchemeSelector; @Inject public DefaultPluginVersionResolver( RepositorySystem repositorySystem, MetadataReader metadataReader, MavenPluginManager pluginManager, - Provider versionSchemeProvider) { + Provider internalSessionProvider, + VersionSchemeSelector versionSchemeSelector) { this.repositorySystem = repositorySystem; this.metadataReader = metadataReader; this.pluginManager = pluginManager; - this.versionSchemeProvider = versionSchemeProvider; + this.internalSessionProvider = internalSessionProvider; + this.versionSchemeSelector = versionSchemeSelector; } @Override @@ -195,7 +199,9 @@ private void selectVersion(DefaultPluginVersionResult result, PluginVersionReque for (String ver : versions.versions.keySet()) { try { - Version v = versionSchemeProvider.get().parseVersion(ver); + Version v = versionSchemeSelector + .selectVersionScheme(internalSessionProvider.get().getSession()) + .parseVersion(ver); if (ver.endsWith("-SNAPSHOT")) { snapshots.add(v); diff --git a/maven-core/src/test/java/org/apache/maven/plugin/PluginParameterExpressionEvaluatorV4Test.java b/maven-core/src/test/java/org/apache/maven/plugin/PluginParameterExpressionEvaluatorV4Test.java index eb676599130a..e2a556db2a2e 100644 --- a/maven-core/src/test/java/org/apache/maven/plugin/PluginParameterExpressionEvaluatorV4Test.java +++ b/maven-core/src/test/java/org/apache/maven/plugin/PluginParameterExpressionEvaluatorV4Test.java @@ -44,10 +44,7 @@ import org.apache.maven.execution.DefaultMavenExecutionResult; import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.execution.MavenSession; -import org.apache.maven.internal.impl.AbstractSession; -import org.apache.maven.internal.impl.DefaultMojoExecution; -import org.apache.maven.internal.impl.DefaultProject; -import org.apache.maven.internal.impl.DefaultSession; +import org.apache.maven.internal.impl.*; import org.apache.maven.model.Build; import org.apache.maven.model.Model; import org.apache.maven.model.building.DefaultModelBuildingRequest; @@ -58,6 +55,7 @@ import org.apache.maven.project.CycleDetectedException; import org.apache.maven.project.DuplicateProjectException; import org.apache.maven.project.MavenProject; +import org.apache.maven.session.scope.internal.SessionScope; import org.codehaus.plexus.MutablePlexusContainer; import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; @@ -65,10 +63,12 @@ import org.codehaus.plexus.util.Os; import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.graph.DefaultDependencyNode; import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; import org.eclipse.aether.repository.LocalRepository; import org.eclipse.aether.repository.NoLocalRepositoryManagerException; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.codehaus.plexus.testing.PlexusExtension.getTestFile; @@ -80,6 +80,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** */ @@ -89,11 +90,25 @@ public class PluginParameterExpressionEvaluatorV4Test extends AbstractCoreMavenC @Inject PlexusContainer container; + @Inject + SessionScope sessionScope; + @Inject private MavenRepositorySystem factory; private Path rootDirectory; + @BeforeEach + public void setUp() { + RepositorySystemSession repositorySystemSession = mock(RepositorySystemSession.class); + when(repositorySystemSession.getConfigProperties()).thenReturn(Collections.emptyMap()); + InternalSession internalSession = mock(InternalSession.class); + when(internalSession.getSession()).thenReturn(repositorySystemSession); + + sessionScope.enter(); + sessionScope.seed(InternalSession.class, internalSession); + } + @Test public void testPluginDescriptorExpressionReference() throws Exception { Session session = newSession(); diff --git a/maven-core/src/test/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteCheckerTest.java b/maven-core/src/test/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteCheckerTest.java index 14a331e992a4..31483b54370c 100644 --- a/maven-core/src/test/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteCheckerTest.java +++ b/maven-core/src/test/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteCheckerTest.java @@ -18,16 +18,30 @@ */ package org.apache.maven.plugin.internal; +import org.apache.maven.internal.impl.InternalSession; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.spi.version.VersionSchemeSelector; import org.eclipse.aether.util.version.GenericVersionScheme; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class MavenPluginJavaPrerequisiteCheckerTest { @Test void testMatchesVersion() { - MavenPluginJavaPrerequisiteChecker checker = new MavenPluginJavaPrerequisiteChecker(GenericVersionScheme::new); + RepositorySystemSession repositorySystemSession = mock(RepositorySystemSession.class); + InternalSession internalSession = mock(InternalSession.class); + when(internalSession.getSession()).thenReturn(repositorySystemSession); + VersionSchemeSelector schemeSelector = mock(VersionSchemeSelector.class); + when(schemeSelector.selectVersionScheme(any(RepositorySystemSession.class))) + .thenReturn(new GenericVersionScheme()); + + MavenPluginJavaPrerequisiteChecker checker = + new MavenPluginJavaPrerequisiteChecker(() -> internalSession, schemeSelector); assertTrue(checker.matchesVersion("1.0", "1.8")); assertTrue(checker.matchesVersion("1.8", "9.0.1+11")); assertFalse(checker.matchesVersion("[1.0,2],[3,4]", "2.1")); diff --git a/maven-core/src/test/java/org/apache/maven/project/DefaultMavenProjectBuilderTest.java b/maven-core/src/test/java/org/apache/maven/project/DefaultMavenProjectBuilderTest.java index 97152f5e4e35..fa82f4c68f14 100644 --- a/maven-core/src/test/java/org/apache/maven/project/DefaultMavenProjectBuilderTest.java +++ b/maven-core/src/test/java/org/apache/maven/project/DefaultMavenProjectBuilderTest.java @@ -18,15 +18,21 @@ */ package org.apache.maven.project; +import javax.inject.Inject; + import java.io.File; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; +import java.util.Collections; import java.util.List; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.internal.impl.InternalSession; +import org.apache.maven.session.scope.internal.SessionScope; +import org.eclipse.aether.RepositorySystemSession; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -45,6 +51,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class DefaultMavenProjectBuilderTest extends AbstractMavenProjectTestCase { @TempDir @@ -54,10 +62,21 @@ class DefaultMavenProjectBuilderTest extends AbstractMavenProjectTestCase { @TempDir Path projectRoot; + @Inject + SessionScope sessionScope; + @Override @BeforeEach public void setUp() throws Exception { projectBuilder = getContainer().lookup(ProjectBuilder.class); + + RepositorySystemSession repositorySystemSession = mock(RepositorySystemSession.class); + when(repositorySystemSession.getConfigProperties()).thenReturn(Collections.emptyMap()); + InternalSession internalSession = mock(InternalSession.class); + when(internalSession.getSession()).thenReturn(repositorySystemSession); + + sessionScope.enter(); + sessionScope.seed(InternalSession.class, internalSession); } protected MavenProject getProject(Artifact pom, boolean allowStub) throws Exception { diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java index 1635bda1b266..8024fd774402 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java @@ -20,7 +20,6 @@ import javax.inject.Inject; import javax.inject.Named; -import javax.inject.Provider; import javax.inject.Singleton; import java.io.InputStream; @@ -54,7 +53,12 @@ import org.eclipse.aether.resolution.VersionRangeResolutionException; import org.eclipse.aether.resolution.VersionRangeResult; import org.eclipse.aether.spi.synccontext.SyncContextFactory; -import org.eclipse.aether.version.*; +import org.eclipse.aether.spi.version.VersionSchemeSelector; +import org.eclipse.aether.version.InvalidVersionSpecificationException; +import org.eclipse.aether.version.Version; +import org.eclipse.aether.version.VersionConstraint; +import org.eclipse.aether.version.VersionRange; +import org.eclipse.aether.version.VersionScheme; /** */ @@ -67,20 +71,20 @@ public class DefaultVersionRangeResolver implements VersionRangeResolver { private final MetadataResolver metadataResolver; private final SyncContextFactory syncContextFactory; private final RepositoryEventDispatcher repositoryEventDispatcher; - private final Provider versionSchemeProvider; + private final VersionSchemeSelector versionSchemeSelector; @Inject public DefaultVersionRangeResolver( MetadataResolver metadataResolver, SyncContextFactory syncContextFactory, RepositoryEventDispatcher repositoryEventDispatcher, - Provider versionSchemeProvider) { + VersionSchemeSelector versionSchemeSelector) { 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.versionSchemeProvider = - Objects.requireNonNull(versionSchemeProvider, "versionSchemeProvider cannot be null"); + this.versionSchemeSelector = + Objects.requireNonNull(versionSchemeSelector, "versionSchemeSelector cannot be null"); } @Override @@ -88,11 +92,11 @@ public VersionRangeResult resolveVersionRange(RepositorySystemSession session, V throws VersionRangeResolutionException { VersionRangeResult result = new VersionRangeResult(request); + VersionScheme versionScheme = versionSchemeSelector.selectVersionScheme(session); VersionConstraint versionConstraint; try { - versionConstraint = versionSchemeProvider - .get() - .parseVersionConstraint(request.getArtifact().getVersion()); + versionConstraint = + versionScheme.parseVersionConstraint(request.getArtifact().getVersion()); } catch (InvalidVersionSpecificationException e) { result.addException(e); throw new VersionRangeResolutionException(result); @@ -113,7 +117,7 @@ public VersionRangeResult resolveVersionRange(RepositorySystemSession session, V List versions = new ArrayList<>(); for (Map.Entry v : versionIndex.entrySet()) { try { - Version ver = versionSchemeProvider.get().parseVersion(v.getKey()); + Version ver = versionScheme.parseVersion(v.getKey()); if (versionConstraint.containsVersion(ver)) { versions.add(ver); result.setRepository(ver, v.getValue()); diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionParser.java b/maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/util/TestVersionParser.java similarity index 96% rename from maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionParser.java rename to maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/util/TestVersionParser.java index b2cae9d42ef6..d9a31ebde953 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionParser.java +++ b/maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/util/TestVersionParser.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.maven.repository.internal; +package org.apache.maven.repository.internal.util; import javax.inject.Inject; import javax.inject.Named; @@ -36,11 +36,11 @@ @Named @Singleton -public class DefaultVersionParser implements VersionParser { +public class TestVersionParser implements VersionParser { private final Provider versionSchemeProvider; @Inject - public DefaultVersionParser(Provider versionSchemeProvider) { + public TestVersionParser(Provider versionSchemeProvider) { this.versionSchemeProvider = requireNonNull(versionSchemeProvider, "versionSchemeProvider"); } From 9fe8fe0339e8cfba00993aa03f451dd9ab5a2a91 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Fri, 15 Dec 2023 16:01:02 +0100 Subject: [PATCH 11/21] Use proper nonnull --- .../maven/internal/impl/DefaultVersionParser.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java index c060f7d706cc..eba9d35e9a08 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java +++ b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java @@ -34,7 +34,7 @@ import org.eclipse.aether.version.InvalidVersionSpecificationException; import org.eclipse.aether.version.VersionScheme; -import static java.util.Objects.requireNonNull; +import static org.apache.maven.internal.impl.Utils.nonNull; /** * A wrapper class around a maven resolver artifact. @@ -51,13 +51,13 @@ public class DefaultVersionParser implements VersionParser, org.apache.maven.mod @Inject public DefaultVersionParser( Provider internalSessionProvider, VersionSchemeSelector versionSchemeSelector) { - this.internalSessionProvider = internalSessionProvider; - this.versionSchemeSelector = versionSchemeSelector; + this.internalSessionProvider = nonNull(internalSessionProvider, "internalSessionProvider"); + this.versionSchemeSelector = nonNull(versionSchemeSelector, "versionSchemeSelector"); } @Override public Version parseVersion(String version) { - requireNonNull(version, "version"); + nonNull(version, "version"); return new DefaultVersion( versionSchemeSelector.selectVersionScheme( internalSessionProvider.get().getSession()), @@ -66,7 +66,7 @@ public Version parseVersion(String version) { @Override public VersionRange parseVersionRange(String range) { - requireNonNull(range, "range"); + nonNull(range, "range"); return new DefaultVersionRange( versionSchemeSelector.selectVersionScheme( internalSessionProvider.get().getSession()), From 2a51421cc1395c29fc1d1113c0c540ecddf7b977 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Sat, 16 Dec 2023 19:20:38 +0100 Subject: [PATCH 12/21] Move off selector --- .../AbstractProjectInheritanceTestCase.java | 24 ++++++ .../t00/ProjectInheritanceTest.java | 1 + .../internal/impl/DefaultVersionParser.java | 22 ++--- .../impl/SessionVersionSchemeProvider.java | 83 +++++++++++++++++++ .../MavenPluginJavaPrerequisiteChecker.java | 15 +--- .../MavenPluginMavenPrerequisiteChecker.java | 15 +--- .../DefaultPluginVersionResolver.java | 16 ++-- .../lifecycle/LifecycleExecutorTest.java | 20 +++++ .../LifecycleDependencyResolverTest.java | 20 +++++ .../maven/plugin/PluginManagerTest.java | 22 +++++ ...avenPluginJavaPrerequisiteCheckerTest.java | 16 +--- .../maven/project/ProjectBuilderTest.java | 22 +++++ .../project/ProjectModelResolverTest.java | 25 ++++-- .../internal/DefaultVersionRangeResolver.java | 12 +-- 14 files changed, 238 insertions(+), 75 deletions(-) create mode 100644 maven-core/src/main/java/org/apache/maven/internal/impl/SessionVersionSchemeProvider.java diff --git a/maven-compat/src/test/java/org/apache/maven/project/inheritance/AbstractProjectInheritanceTestCase.java b/maven-compat/src/test/java/org/apache/maven/project/inheritance/AbstractProjectInheritanceTestCase.java index dbdd51e7a31c..6205ad5553b3 100644 --- a/maven-compat/src/test/java/org/apache/maven/project/inheritance/AbstractProjectInheritanceTestCase.java +++ b/maven-compat/src/test/java/org/apache/maven/project/inheritance/AbstractProjectInheritanceTestCase.java @@ -18,15 +18,39 @@ */ package org.apache.maven.project.inheritance; +import javax.inject.Inject; + import java.io.File; +import java.util.Collections; +import org.apache.maven.internal.impl.InternalSession; import org.apache.maven.project.AbstractMavenProjectTestCase; +import org.apache.maven.session.scope.internal.SessionScope; +import org.eclipse.aether.RepositorySystemSession; +import org.junit.jupiter.api.BeforeEach; import static org.codehaus.plexus.testing.PlexusExtension.getTestFile; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** */ public abstract class AbstractProjectInheritanceTestCase extends AbstractMavenProjectTestCase { + + @Inject + SessionScope sessionScope; + + @BeforeEach + public void setUpSession() { + RepositorySystemSession repositorySystemSession = mock(RepositorySystemSession.class); + when(repositorySystemSession.getConfigProperties()).thenReturn(Collections.emptyMap()); + InternalSession internalSession = mock(InternalSession.class); + when(internalSession.getSession()).thenReturn(repositorySystemSession); + + sessionScope.enter(); + sessionScope.seed(InternalSession.class, internalSession); + } + protected String getTestSeries() { String className = getClass().getPackage().getName(); diff --git a/maven-compat/src/test/java/org/apache/maven/project/inheritance/t00/ProjectInheritanceTest.java b/maven-compat/src/test/java/org/apache/maven/project/inheritance/t00/ProjectInheritanceTest.java index 45043ed22f40..18d3d6ebf0dc 100644 --- a/maven-compat/src/test/java/org/apache/maven/project/inheritance/t00/ProjectInheritanceTest.java +++ b/maven-compat/src/test/java/org/apache/maven/project/inheritance/t00/ProjectInheritanceTest.java @@ -35,6 +35,7 @@ * */ class ProjectInheritanceTest extends AbstractProjectInheritanceTestCase { + // ---------------------------------------------------------------------- // // p4 inherits from p3 diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java index eba9d35e9a08..631a3cd280fd 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java +++ b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java @@ -30,14 +30,13 @@ import org.apache.maven.api.VersionRange; import org.apache.maven.api.services.VersionParser; import org.apache.maven.api.services.VersionParserException; -import org.eclipse.aether.spi.version.VersionSchemeSelector; import org.eclipse.aether.version.InvalidVersionSpecificationException; import org.eclipse.aether.version.VersionScheme; import static org.apache.maven.internal.impl.Utils.nonNull; /** - * A wrapper class around a maven resolver artifact. + * A wrapper class around a resolver version that works as model version parser as well. */ @Named @Singleton @@ -45,32 +44,23 @@ public class DefaultVersionParser implements VersionParser, org.apache.maven.mod private static final String SNAPSHOT = "SNAPSHOT"; private static final Pattern SNAPSHOT_TIMESTAMP = Pattern.compile("^(.*-)?([0-9]{8}\\.[0-9]{6}-[0-9]+)$"); - private final Provider internalSessionProvider; - private final VersionSchemeSelector versionSchemeSelector; + private final Provider versionSchemeProvider; @Inject - public DefaultVersionParser( - Provider internalSessionProvider, VersionSchemeSelector versionSchemeSelector) { - this.internalSessionProvider = nonNull(internalSessionProvider, "internalSessionProvider"); - this.versionSchemeSelector = nonNull(versionSchemeSelector, "versionSchemeSelector"); + public DefaultVersionParser(Provider versionSchemeProvider) { + this.versionSchemeProvider = nonNull(versionSchemeProvider, "versionSchemeProvider"); } @Override public Version parseVersion(String version) { nonNull(version, "version"); - return new DefaultVersion( - versionSchemeSelector.selectVersionScheme( - internalSessionProvider.get().getSession()), - version); + return new DefaultVersion(versionSchemeProvider.get(), version); } @Override public VersionRange parseVersionRange(String range) { nonNull(range, "range"); - return new DefaultVersionRange( - versionSchemeSelector.selectVersionScheme( - internalSessionProvider.get().getSession()), - range); + return new DefaultVersionRange(versionSchemeProvider.get(), range); } @Override diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/SessionVersionSchemeProvider.java b/maven-core/src/main/java/org/apache/maven/internal/impl/SessionVersionSchemeProvider.java new file mode 100644 index 000000000000..1ced1bb164d3 --- /dev/null +++ b/maven-core/src/main/java/org/apache/maven/internal/impl/SessionVersionSchemeProvider.java @@ -0,0 +1,83 @@ +/* + * 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. + */ +package org.apache.maven.internal.impl; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Provider; +import javax.inject.Singleton; + +import java.util.Map; + +import org.eclipse.aether.internal.impl.version.GenericVersionSchemeProvider; +import org.eclipse.aether.util.ConfigUtils; +import org.eclipse.aether.version.*; +import org.eclipse.sisu.Priority; + +import static org.apache.maven.internal.impl.Utils.nonNull; + +/** + * A Maven core component that provides {@link VersionScheme} instances based on session configuration. + */ +@Named +@Singleton +@Priority(10) +public class SessionVersionSchemeProvider implements Provider { + private final Provider internalSessionProvider; + private final Map versionSchemes; + + @Inject + public SessionVersionSchemeProvider( + Provider internalSessionProvider, Map versionSchemes) { + this.internalSessionProvider = nonNull(internalSessionProvider, "internalSessionProvider"); + this.versionSchemes = nonNull(versionSchemes, "versionSchemes"); + } + + @Override + public VersionScheme get() { + InternalSession session = internalSessionProvider.get(); + String schemeName = ConfigUtils.getString( + session.getSession(), GenericVersionSchemeProvider.NAME, "maven.versionScheme.name"); + VersionScheme result = versionSchemes.get(schemeName); + if (result == null) { + // A "small hack" here: if we'd throw IAEx here, it would be caught by Guice and user would end up + // with cryptic error how object graph cannot be constructed. Hence, what we do instead is + // returning "fake" VersionScheme instance, that just throws, thus postponing failure to the point + // where scheme is really needed, and allowing Guice to construct Maven components. This would allow + // us to catch exception and present (more) meaningful message to end user. + result = new VersionScheme() { + @Override + public Version parseVersion(String s) { + throw new IllegalArgumentException("Unsupported version scheme: " + schemeName); + } + + @Override + public VersionRange parseVersionRange(String s) { + throw new IllegalArgumentException("Unsupported version scheme: " + schemeName); + } + + @Override + public VersionConstraint parseVersionConstraint(String s) { + throw new IllegalArgumentException("Unsupported version scheme: " + schemeName); + } + }; + } + return result; + } +} diff --git a/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteChecker.java b/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteChecker.java index cbf9e6024fe3..75ceafe6bf26 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteChecker.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteChecker.java @@ -23,10 +23,8 @@ import javax.inject.Provider; import javax.inject.Singleton; -import org.apache.maven.internal.impl.InternalSession; import org.apache.maven.plugin.MavenPluginPrerequisitesChecker; import org.apache.maven.plugin.descriptor.PluginDescriptor; -import org.eclipse.aether.spi.version.VersionSchemeSelector; import org.eclipse.aether.version.InvalidVersionSpecificationException; import org.eclipse.aether.version.Version; import org.eclipse.aether.version.VersionConstraint; @@ -35,15 +33,11 @@ @Named @Singleton public class MavenPluginJavaPrerequisiteChecker implements MavenPluginPrerequisitesChecker { - - private final Provider internalSessionProvider; - private final VersionSchemeSelector versionSchemeSelector; + private final Provider versionSchemeProvider; @Inject - public MavenPluginJavaPrerequisiteChecker( - Provider internalSessionProvider, VersionSchemeSelector versionSchemeSelector) { - this.internalSessionProvider = internalSessionProvider; - this.versionSchemeSelector = versionSchemeSelector; + public MavenPluginJavaPrerequisiteChecker(Provider versionSchemeProvider) { + this.versionSchemeProvider = versionSchemeProvider; } @Override @@ -59,8 +53,7 @@ public void accept(PluginDescriptor pluginDescriptor) { } boolean matchesVersion(String requiredVersion, String currentVersion) { - VersionScheme versionScheme = versionSchemeSelector.selectVersionScheme( - internalSessionProvider.get().getSession()); + VersionScheme versionScheme = versionSchemeProvider.get(); VersionConstraint constraint; try { constraint = versionScheme.parseVersionConstraint(requiredVersion); diff --git a/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginMavenPrerequisiteChecker.java b/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginMavenPrerequisiteChecker.java index 3b71560d3cb2..fb921efa93b1 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginMavenPrerequisiteChecker.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginMavenPrerequisiteChecker.java @@ -23,11 +23,9 @@ import javax.inject.Provider; import javax.inject.Singleton; -import org.apache.maven.internal.impl.InternalSession; import org.apache.maven.plugin.MavenPluginPrerequisitesChecker; import org.apache.maven.plugin.descriptor.PluginDescriptor; import org.apache.maven.rtinfo.RuntimeInformation; -import org.eclipse.aether.spi.version.VersionSchemeSelector; import org.eclipse.aether.version.InvalidVersionSpecificationException; import org.eclipse.aether.version.Version; import org.eclipse.aether.version.VersionConstraint; @@ -41,17 +39,13 @@ public class MavenPluginMavenPrerequisiteChecker implements MavenPluginPrerequis private final Logger logger = LoggerFactory.getLogger(getClass()); private final RuntimeInformation runtimeInformation; - private final Provider internalSessionProvider; - private final VersionSchemeSelector versionSchemeSelector; + private final Provider versionSchemeProvider; @Inject public MavenPluginMavenPrerequisiteChecker( - RuntimeInformation runtimeInformation, - Provider internalSessionProvider, - VersionSchemeSelector versionSchemeSelector) { + RuntimeInformation runtimeInformation, Provider versionSchemeProvider) { this.runtimeInformation = runtimeInformation; - this.internalSessionProvider = internalSessionProvider; - this.versionSchemeSelector = versionSchemeSelector; + this.versionSchemeProvider = versionSchemeProvider; } @Override @@ -62,8 +56,7 @@ public void accept(PluginDescriptor pluginDescriptor) { requiredMavenVersion == null || requiredMavenVersion.trim().isEmpty(); if (!isBlankVersion) { - VersionScheme versionScheme = versionSchemeSelector.selectVersionScheme( - internalSessionProvider.get().getSession()); // this must happen within session + VersionScheme versionScheme = versionSchemeProvider.get(); VersionConstraint constraint; try { constraint = versionScheme.parseVersionConstraint(requiredMavenVersion); diff --git a/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java b/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java index ba1c51440605..310e3aa6037b 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java @@ -37,7 +37,6 @@ import org.apache.maven.artifact.repository.metadata.Metadata; import org.apache.maven.artifact.repository.metadata.Versioning; import org.apache.maven.artifact.repository.metadata.io.MetadataReader; -import org.apache.maven.internal.impl.InternalSession; import org.apache.maven.model.Build; import org.apache.maven.model.Plugin; import org.apache.maven.plugin.MavenPluginManager; @@ -59,9 +58,9 @@ import org.eclipse.aether.repository.RemoteRepository; import org.eclipse.aether.resolution.MetadataRequest; import org.eclipse.aether.resolution.MetadataResult; -import org.eclipse.aether.spi.version.VersionSchemeSelector; import org.eclipse.aether.version.InvalidVersionSpecificationException; import org.eclipse.aether.version.Version; +import org.eclipse.aether.version.VersionScheme; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -81,21 +80,18 @@ public class DefaultPluginVersionResolver implements PluginVersionResolver { private final RepositorySystem repositorySystem; private final MetadataReader metadataReader; private final MavenPluginManager pluginManager; - private final Provider internalSessionProvider; - private final VersionSchemeSelector versionSchemeSelector; + private final Provider versionSchemeProvider; @Inject public DefaultPluginVersionResolver( RepositorySystem repositorySystem, MetadataReader metadataReader, MavenPluginManager pluginManager, - Provider internalSessionProvider, - VersionSchemeSelector versionSchemeSelector) { + Provider versionSchemeProvider) { this.repositorySystem = repositorySystem; this.metadataReader = metadataReader; this.pluginManager = pluginManager; - this.internalSessionProvider = internalSessionProvider; - this.versionSchemeSelector = versionSchemeSelector; + this.versionSchemeProvider = versionSchemeProvider; } @Override @@ -199,9 +195,7 @@ private void selectVersion(DefaultPluginVersionResult result, PluginVersionReque for (String ver : versions.versions.keySet()) { try { - Version v = versionSchemeSelector - .selectVersionScheme(internalSessionProvider.get().getSession()) - .parseVersion(ver); + Version v = versionSchemeProvider.get().parseVersion(ver); if (ver.endsWith("-SNAPSHOT")) { snapshots.add(v); diff --git a/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java b/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java index 2c742b4c8730..b084f1986072 100644 --- a/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java +++ b/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java @@ -35,6 +35,7 @@ import org.apache.maven.execution.ProjectDependencyGraph; import org.apache.maven.execution.ProjectExecutionEvent; import org.apache.maven.execution.ProjectExecutionListener; +import org.apache.maven.internal.impl.InternalSession; import org.apache.maven.lifecycle.internal.DefaultLifecycleTaskSegmentCalculator; import org.apache.maven.lifecycle.internal.ExecutionPlanItem; import org.apache.maven.lifecycle.internal.LifecycleExecutionPlanCalculator; @@ -47,6 +48,9 @@ import org.apache.maven.plugin.MojoNotFoundException; import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.project.MavenProject; +import org.apache.maven.session.scope.internal.SessionScope; +import org.eclipse.aether.RepositorySystemSession; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.hamcrest.MatcherAssert.assertThat; @@ -55,6 +59,8 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class LifecycleExecutorTest extends AbstractCoreMavenComponentTestCase { @Inject @@ -69,6 +75,20 @@ class LifecycleExecutorTest extends AbstractCoreMavenComponentTestCase { @Inject private MojoDescriptorCreator mojoDescriptorCreator; + @Inject + SessionScope sessionScope; + + @BeforeEach + public void setUp() { + RepositorySystemSession repositorySystemSession = mock(RepositorySystemSession.class); + when(repositorySystemSession.getConfigProperties()).thenReturn(Collections.emptyMap()); + InternalSession internalSession = mock(InternalSession.class); + when(internalSession.getSession()).thenReturn(repositorySystemSession); + + sessionScope.enter(); + sessionScope.seed(InternalSession.class, internalSession); + } + protected String getProjectsDirectory() { return "src/test/projects/lifecycle-executor"; } diff --git a/maven-core/src/test/java/org/apache/maven/lifecycle/internal/LifecycleDependencyResolverTest.java b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/LifecycleDependencyResolverTest.java index 3ff43bc2d2e6..722c737aee84 100644 --- a/maven-core/src/test/java/org/apache/maven/lifecycle/internal/LifecycleDependencyResolverTest.java +++ b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/LifecycleDependencyResolverTest.java @@ -30,16 +30,36 @@ import org.apache.maven.AbstractCoreMavenComponentTestCase; import org.apache.maven.artifact.Artifact; import org.apache.maven.execution.MavenSession; +import org.apache.maven.internal.impl.InternalSession; import org.apache.maven.project.MavenProject; +import org.apache.maven.session.scope.internal.SessionScope; +import org.eclipse.aether.RepositorySystemSession; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class LifecycleDependencyResolverTest extends AbstractCoreMavenComponentTestCase { @Inject private LifecycleDependencyResolver resolver; + @Inject + SessionScope sessionScope; + + @BeforeEach + public void setUp() { + RepositorySystemSession repositorySystemSession = mock(RepositorySystemSession.class); + when(repositorySystemSession.getConfigProperties()).thenReturn(Collections.emptyMap()); + InternalSession internalSession = mock(InternalSession.class); + when(internalSession.getSession()).thenReturn(repositorySystemSession); + + sessionScope.enter(); + sessionScope.seed(InternalSession.class, internalSession); + } + @Override protected String getProjectsDirectory() { return null; diff --git a/maven-core/src/test/java/org/apache/maven/plugin/PluginManagerTest.java b/maven-core/src/test/java/org/apache/maven/plugin/PluginManagerTest.java index 7020e40d25b6..b2b0b36ea2ed 100644 --- a/maven-core/src/test/java/org/apache/maven/plugin/PluginManagerTest.java +++ b/maven-core/src/test/java/org/apache/maven/plugin/PluginManagerTest.java @@ -20,6 +20,7 @@ import javax.inject.Inject; +import java.util.Collections; import java.util.List; import org.apache.maven.AbstractCoreMavenComponentTestCase; @@ -27,22 +28,43 @@ import org.apache.maven.artifact.repository.DefaultRepositoryRequest; import org.apache.maven.artifact.repository.RepositoryRequest; import org.apache.maven.execution.MavenSession; +import org.apache.maven.internal.impl.InternalSession; import org.apache.maven.model.Plugin; import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.plugin.descriptor.PluginDescriptor; import org.apache.maven.project.MavenProject; +import org.apache.maven.session.scope.internal.SessionScope; import org.codehaus.plexus.classworlds.realm.ClassRealm; import org.codehaus.plexus.component.repository.ComponentDescriptor; +import org.eclipse.aether.RepositorySystemSession; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class PluginManagerTest extends AbstractCoreMavenComponentTestCase { @Inject private DefaultBuildPluginManager pluginManager; + @Inject + SessionScope sessionScope; + + @BeforeEach + public void setUp() { + RepositorySystemSession repositorySystemSession = mock(RepositorySystemSession.class); + when(repositorySystemSession.getConfigProperties()).thenReturn(Collections.emptyMap()); + InternalSession internalSession = mock(InternalSession.class); + when(internalSession.getSession()).thenReturn(repositorySystemSession); + + sessionScope.enter(); + sessionScope.seed(InternalSession.class, internalSession); + } + + @Override protected String getProjectsDirectory() { return "src/test/projects/plugin-manager"; } diff --git a/maven-core/src/test/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteCheckerTest.java b/maven-core/src/test/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteCheckerTest.java index 31483b54370c..14a331e992a4 100644 --- a/maven-core/src/test/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteCheckerTest.java +++ b/maven-core/src/test/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteCheckerTest.java @@ -18,30 +18,16 @@ */ package org.apache.maven.plugin.internal; -import org.apache.maven.internal.impl.InternalSession; -import org.eclipse.aether.RepositorySystemSession; -import org.eclipse.aether.spi.version.VersionSchemeSelector; import org.eclipse.aether.util.version.GenericVersionScheme; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; class MavenPluginJavaPrerequisiteCheckerTest { @Test void testMatchesVersion() { - RepositorySystemSession repositorySystemSession = mock(RepositorySystemSession.class); - InternalSession internalSession = mock(InternalSession.class); - when(internalSession.getSession()).thenReturn(repositorySystemSession); - VersionSchemeSelector schemeSelector = mock(VersionSchemeSelector.class); - when(schemeSelector.selectVersionScheme(any(RepositorySystemSession.class))) - .thenReturn(new GenericVersionScheme()); - - MavenPluginJavaPrerequisiteChecker checker = - new MavenPluginJavaPrerequisiteChecker(() -> internalSession, schemeSelector); + MavenPluginJavaPrerequisiteChecker checker = new MavenPluginJavaPrerequisiteChecker(GenericVersionScheme::new); assertTrue(checker.matchesVersion("1.0", "1.8")); assertTrue(checker.matchesVersion("1.8", "9.0.1+11")); assertFalse(checker.matchesVersion("[1.0,2],[3,4]", "2.1")); diff --git a/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java b/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java index 637d05badda8..d6cfedba3b5a 100644 --- a/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java +++ b/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java @@ -18,6 +18,8 @@ */ package org.apache.maven.project; +import javax.inject.Inject; + import java.io.File; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -31,6 +33,7 @@ import org.apache.commons.io.FileUtils; import org.apache.maven.AbstractCoreMavenComponentTestCase; import org.apache.maven.execution.MavenSession; +import org.apache.maven.internal.impl.InternalSession; import org.apache.maven.model.Dependency; import org.apache.maven.model.InputLocation; import org.apache.maven.model.Plugin; @@ -38,6 +41,9 @@ import org.apache.maven.model.building.ModelBuildingRequest; import org.apache.maven.model.building.ModelProblem; import org.apache.maven.model.building.ModelSource; +import org.apache.maven.session.scope.internal.SessionScope; +import org.eclipse.aether.RepositorySystemSession; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -54,8 +60,24 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class ProjectBuilderTest extends AbstractCoreMavenComponentTestCase { + @Inject + SessionScope sessionScope; + + @BeforeEach + public void setUp() { + RepositorySystemSession repositorySystemSession = mock(RepositorySystemSession.class); + when(repositorySystemSession.getConfigProperties()).thenReturn(Collections.emptyMap()); + InternalSession internalSession = mock(InternalSession.class); + when(internalSession.getSession()).thenReturn(repositorySystemSession); + + sessionScope.enter(); + sessionScope.seed(InternalSession.class, internalSession); + } + @Override protected String getProjectsDirectory() { return "src/test/projects/project-builder"; diff --git a/maven-core/src/test/java/org/apache/maven/project/ProjectModelResolverTest.java b/maven-core/src/test/java/org/apache/maven/project/ProjectModelResolverTest.java index ec8cb759b8f3..a43f1d688134 100644 --- a/maven-core/src/test/java/org/apache/maven/project/ProjectModelResolverTest.java +++ b/maven-core/src/test/java/org/apache/maven/project/ProjectModelResolverTest.java @@ -18,6 +18,8 @@ */ package org.apache.maven.project; +import javax.inject.Inject; + import java.io.File; import java.util.Collections; import java.util.List; @@ -25,15 +27,19 @@ import org.apache.maven.artifact.InvalidRepositoryException; import org.apache.maven.bridge.MavenRepositorySystem; +import org.apache.maven.internal.impl.InternalSession; import org.apache.maven.model.Dependency; import org.apache.maven.model.Parent; import org.apache.maven.model.resolution.ModelResolver; import org.apache.maven.model.resolution.UnresolvableModelException; import org.apache.maven.repository.internal.MavenRepositorySystemUtils; +import org.apache.maven.session.scope.internal.SessionScope; import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.impl.RemoteRepositoryManager; import org.eclipse.aether.repository.RemoteRepository; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.codehaus.plexus.testing.PlexusExtension.getBasedir; @@ -42,6 +48,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * Test cases for the project {@code ModelResolver} implementation. @@ -50,11 +58,18 @@ */ class ProjectModelResolverTest extends AbstractMavenProjectTestCase { - /** - * Creates a new {@code ProjectModelResolverTest} instance. - */ - public ProjectModelResolverTest() { - super(); + @Inject + SessionScope sessionScope; + + @BeforeEach + public void setUp() { + RepositorySystemSession repositorySystemSession = mock(RepositorySystemSession.class); + when(repositorySystemSession.getConfigProperties()).thenReturn(Collections.emptyMap()); + InternalSession internalSession = mock(InternalSession.class); + when(internalSession.getSession()).thenReturn(repositorySystemSession); + + sessionScope.enter(); + sessionScope.seed(InternalSession.class, internalSession); } @Test diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java index 8024fd774402..8df25a7c2d1e 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java @@ -20,6 +20,7 @@ import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Provider; import javax.inject.Singleton; import java.io.InputStream; @@ -53,7 +54,6 @@ import org.eclipse.aether.resolution.VersionRangeResolutionException; import org.eclipse.aether.resolution.VersionRangeResult; import org.eclipse.aether.spi.synccontext.SyncContextFactory; -import org.eclipse.aether.spi.version.VersionSchemeSelector; import org.eclipse.aether.version.InvalidVersionSpecificationException; import org.eclipse.aether.version.Version; import org.eclipse.aether.version.VersionConstraint; @@ -71,20 +71,20 @@ public class DefaultVersionRangeResolver implements VersionRangeResolver { private final MetadataResolver metadataResolver; private final SyncContextFactory syncContextFactory; private final RepositoryEventDispatcher repositoryEventDispatcher; - private final VersionSchemeSelector versionSchemeSelector; + private final Provider versionSchemeProvider; @Inject public DefaultVersionRangeResolver( MetadataResolver metadataResolver, SyncContextFactory syncContextFactory, RepositoryEventDispatcher repositoryEventDispatcher, - VersionSchemeSelector versionSchemeSelector) { + Provider versionSchemeProvider) { 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.versionSchemeSelector = - Objects.requireNonNull(versionSchemeSelector, "versionSchemeSelector cannot be null"); + this.versionSchemeProvider = + Objects.requireNonNull(versionSchemeProvider, "versionSchemeProvider cannot be null"); } @Override @@ -92,7 +92,7 @@ public VersionRangeResult resolveVersionRange(RepositorySystemSession session, V throws VersionRangeResolutionException { VersionRangeResult result = new VersionRangeResult(request); - VersionScheme versionScheme = versionSchemeSelector.selectVersionScheme(session); + VersionScheme versionScheme = versionSchemeProvider.get(); VersionConstraint versionConstraint; try { versionConstraint = From b0a9e5672426ae42f49414c229a1582c28565c7a Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Sat, 16 Dec 2023 19:38:02 +0100 Subject: [PATCH 13/21] Move off session as well --- .../AbstractProjectInheritanceTestCase.java | 24 ------ .../t00/ProjectInheritanceTest.java | 1 - .../impl/SessionVersionSchemeProvider.java | 83 ------------------- .../lifecycle/LifecycleExecutorTest.java | 20 ----- .../LifecycleDependencyResolverTest.java | 20 ----- .../maven/plugin/PluginManagerTest.java | 22 ----- .../maven/project/ProjectBuilderTest.java | 22 ----- .../project/ProjectModelResolverTest.java | 25 ++---- .../DefaultVersionSchemeProvider.java | 44 ++++++++++ 9 files changed, 49 insertions(+), 212 deletions(-) delete mode 100644 maven-core/src/main/java/org/apache/maven/internal/impl/SessionVersionSchemeProvider.java create mode 100644 maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionSchemeProvider.java diff --git a/maven-compat/src/test/java/org/apache/maven/project/inheritance/AbstractProjectInheritanceTestCase.java b/maven-compat/src/test/java/org/apache/maven/project/inheritance/AbstractProjectInheritanceTestCase.java index 6205ad5553b3..dbdd51e7a31c 100644 --- a/maven-compat/src/test/java/org/apache/maven/project/inheritance/AbstractProjectInheritanceTestCase.java +++ b/maven-compat/src/test/java/org/apache/maven/project/inheritance/AbstractProjectInheritanceTestCase.java @@ -18,39 +18,15 @@ */ package org.apache.maven.project.inheritance; -import javax.inject.Inject; - import java.io.File; -import java.util.Collections; -import org.apache.maven.internal.impl.InternalSession; import org.apache.maven.project.AbstractMavenProjectTestCase; -import org.apache.maven.session.scope.internal.SessionScope; -import org.eclipse.aether.RepositorySystemSession; -import org.junit.jupiter.api.BeforeEach; import static org.codehaus.plexus.testing.PlexusExtension.getTestFile; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; /** */ public abstract class AbstractProjectInheritanceTestCase extends AbstractMavenProjectTestCase { - - @Inject - SessionScope sessionScope; - - @BeforeEach - public void setUpSession() { - RepositorySystemSession repositorySystemSession = mock(RepositorySystemSession.class); - when(repositorySystemSession.getConfigProperties()).thenReturn(Collections.emptyMap()); - InternalSession internalSession = mock(InternalSession.class); - when(internalSession.getSession()).thenReturn(repositorySystemSession); - - sessionScope.enter(); - sessionScope.seed(InternalSession.class, internalSession); - } - protected String getTestSeries() { String className = getClass().getPackage().getName(); diff --git a/maven-compat/src/test/java/org/apache/maven/project/inheritance/t00/ProjectInheritanceTest.java b/maven-compat/src/test/java/org/apache/maven/project/inheritance/t00/ProjectInheritanceTest.java index 18d3d6ebf0dc..45043ed22f40 100644 --- a/maven-compat/src/test/java/org/apache/maven/project/inheritance/t00/ProjectInheritanceTest.java +++ b/maven-compat/src/test/java/org/apache/maven/project/inheritance/t00/ProjectInheritanceTest.java @@ -35,7 +35,6 @@ * */ class ProjectInheritanceTest extends AbstractProjectInheritanceTestCase { - // ---------------------------------------------------------------------- // // p4 inherits from p3 diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/SessionVersionSchemeProvider.java b/maven-core/src/main/java/org/apache/maven/internal/impl/SessionVersionSchemeProvider.java deleted file mode 100644 index 1ced1bb164d3..000000000000 --- a/maven-core/src/main/java/org/apache/maven/internal/impl/SessionVersionSchemeProvider.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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. - */ -package org.apache.maven.internal.impl; - -import javax.inject.Inject; -import javax.inject.Named; -import javax.inject.Provider; -import javax.inject.Singleton; - -import java.util.Map; - -import org.eclipse.aether.internal.impl.version.GenericVersionSchemeProvider; -import org.eclipse.aether.util.ConfigUtils; -import org.eclipse.aether.version.*; -import org.eclipse.sisu.Priority; - -import static org.apache.maven.internal.impl.Utils.nonNull; - -/** - * A Maven core component that provides {@link VersionScheme} instances based on session configuration. - */ -@Named -@Singleton -@Priority(10) -public class SessionVersionSchemeProvider implements Provider { - private final Provider internalSessionProvider; - private final Map versionSchemes; - - @Inject - public SessionVersionSchemeProvider( - Provider internalSessionProvider, Map versionSchemes) { - this.internalSessionProvider = nonNull(internalSessionProvider, "internalSessionProvider"); - this.versionSchemes = nonNull(versionSchemes, "versionSchemes"); - } - - @Override - public VersionScheme get() { - InternalSession session = internalSessionProvider.get(); - String schemeName = ConfigUtils.getString( - session.getSession(), GenericVersionSchemeProvider.NAME, "maven.versionScheme.name"); - VersionScheme result = versionSchemes.get(schemeName); - if (result == null) { - // A "small hack" here: if we'd throw IAEx here, it would be caught by Guice and user would end up - // with cryptic error how object graph cannot be constructed. Hence, what we do instead is - // returning "fake" VersionScheme instance, that just throws, thus postponing failure to the point - // where scheme is really needed, and allowing Guice to construct Maven components. This would allow - // us to catch exception and present (more) meaningful message to end user. - result = new VersionScheme() { - @Override - public Version parseVersion(String s) { - throw new IllegalArgumentException("Unsupported version scheme: " + schemeName); - } - - @Override - public VersionRange parseVersionRange(String s) { - throw new IllegalArgumentException("Unsupported version scheme: " + schemeName); - } - - @Override - public VersionConstraint parseVersionConstraint(String s) { - throw new IllegalArgumentException("Unsupported version scheme: " + schemeName); - } - }; - } - return result; - } -} diff --git a/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java b/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java index b084f1986072..2c742b4c8730 100644 --- a/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java +++ b/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java @@ -35,7 +35,6 @@ import org.apache.maven.execution.ProjectDependencyGraph; import org.apache.maven.execution.ProjectExecutionEvent; import org.apache.maven.execution.ProjectExecutionListener; -import org.apache.maven.internal.impl.InternalSession; import org.apache.maven.lifecycle.internal.DefaultLifecycleTaskSegmentCalculator; import org.apache.maven.lifecycle.internal.ExecutionPlanItem; import org.apache.maven.lifecycle.internal.LifecycleExecutionPlanCalculator; @@ -48,9 +47,6 @@ import org.apache.maven.plugin.MojoNotFoundException; import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.project.MavenProject; -import org.apache.maven.session.scope.internal.SessionScope; -import org.eclipse.aether.RepositorySystemSession; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.hamcrest.MatcherAssert.assertThat; @@ -59,8 +55,6 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; class LifecycleExecutorTest extends AbstractCoreMavenComponentTestCase { @Inject @@ -75,20 +69,6 @@ class LifecycleExecutorTest extends AbstractCoreMavenComponentTestCase { @Inject private MojoDescriptorCreator mojoDescriptorCreator; - @Inject - SessionScope sessionScope; - - @BeforeEach - public void setUp() { - RepositorySystemSession repositorySystemSession = mock(RepositorySystemSession.class); - when(repositorySystemSession.getConfigProperties()).thenReturn(Collections.emptyMap()); - InternalSession internalSession = mock(InternalSession.class); - when(internalSession.getSession()).thenReturn(repositorySystemSession); - - sessionScope.enter(); - sessionScope.seed(InternalSession.class, internalSession); - } - protected String getProjectsDirectory() { return "src/test/projects/lifecycle-executor"; } diff --git a/maven-core/src/test/java/org/apache/maven/lifecycle/internal/LifecycleDependencyResolverTest.java b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/LifecycleDependencyResolverTest.java index 722c737aee84..3ff43bc2d2e6 100644 --- a/maven-core/src/test/java/org/apache/maven/lifecycle/internal/LifecycleDependencyResolverTest.java +++ b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/LifecycleDependencyResolverTest.java @@ -30,36 +30,16 @@ import org.apache.maven.AbstractCoreMavenComponentTestCase; import org.apache.maven.artifact.Artifact; import org.apache.maven.execution.MavenSession; -import org.apache.maven.internal.impl.InternalSession; import org.apache.maven.project.MavenProject; -import org.apache.maven.session.scope.internal.SessionScope; -import org.eclipse.aether.RepositorySystemSession; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; class LifecycleDependencyResolverTest extends AbstractCoreMavenComponentTestCase { @Inject private LifecycleDependencyResolver resolver; - @Inject - SessionScope sessionScope; - - @BeforeEach - public void setUp() { - RepositorySystemSession repositorySystemSession = mock(RepositorySystemSession.class); - when(repositorySystemSession.getConfigProperties()).thenReturn(Collections.emptyMap()); - InternalSession internalSession = mock(InternalSession.class); - when(internalSession.getSession()).thenReturn(repositorySystemSession); - - sessionScope.enter(); - sessionScope.seed(InternalSession.class, internalSession); - } - @Override protected String getProjectsDirectory() { return null; diff --git a/maven-core/src/test/java/org/apache/maven/plugin/PluginManagerTest.java b/maven-core/src/test/java/org/apache/maven/plugin/PluginManagerTest.java index b2b0b36ea2ed..7020e40d25b6 100644 --- a/maven-core/src/test/java/org/apache/maven/plugin/PluginManagerTest.java +++ b/maven-core/src/test/java/org/apache/maven/plugin/PluginManagerTest.java @@ -20,7 +20,6 @@ import javax.inject.Inject; -import java.util.Collections; import java.util.List; import org.apache.maven.AbstractCoreMavenComponentTestCase; @@ -28,43 +27,22 @@ import org.apache.maven.artifact.repository.DefaultRepositoryRequest; import org.apache.maven.artifact.repository.RepositoryRequest; import org.apache.maven.execution.MavenSession; -import org.apache.maven.internal.impl.InternalSession; import org.apache.maven.model.Plugin; import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.plugin.descriptor.PluginDescriptor; import org.apache.maven.project.MavenProject; -import org.apache.maven.session.scope.internal.SessionScope; import org.codehaus.plexus.classworlds.realm.ClassRealm; import org.codehaus.plexus.component.repository.ComponentDescriptor; -import org.eclipse.aether.RepositorySystemSession; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.fail; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; class PluginManagerTest extends AbstractCoreMavenComponentTestCase { @Inject private DefaultBuildPluginManager pluginManager; - @Inject - SessionScope sessionScope; - - @BeforeEach - public void setUp() { - RepositorySystemSession repositorySystemSession = mock(RepositorySystemSession.class); - when(repositorySystemSession.getConfigProperties()).thenReturn(Collections.emptyMap()); - InternalSession internalSession = mock(InternalSession.class); - when(internalSession.getSession()).thenReturn(repositorySystemSession); - - sessionScope.enter(); - sessionScope.seed(InternalSession.class, internalSession); - } - - @Override protected String getProjectsDirectory() { return "src/test/projects/plugin-manager"; } diff --git a/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java b/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java index d6cfedba3b5a..637d05badda8 100644 --- a/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java +++ b/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java @@ -18,8 +18,6 @@ */ package org.apache.maven.project; -import javax.inject.Inject; - import java.io.File; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -33,7 +31,6 @@ import org.apache.commons.io.FileUtils; import org.apache.maven.AbstractCoreMavenComponentTestCase; import org.apache.maven.execution.MavenSession; -import org.apache.maven.internal.impl.InternalSession; import org.apache.maven.model.Dependency; import org.apache.maven.model.InputLocation; import org.apache.maven.model.Plugin; @@ -41,9 +38,6 @@ import org.apache.maven.model.building.ModelBuildingRequest; import org.apache.maven.model.building.ModelProblem; import org.apache.maven.model.building.ModelSource; -import org.apache.maven.session.scope.internal.SessionScope; -import org.eclipse.aether.RepositorySystemSession; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -60,24 +54,8 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; class ProjectBuilderTest extends AbstractCoreMavenComponentTestCase { - @Inject - SessionScope sessionScope; - - @BeforeEach - public void setUp() { - RepositorySystemSession repositorySystemSession = mock(RepositorySystemSession.class); - when(repositorySystemSession.getConfigProperties()).thenReturn(Collections.emptyMap()); - InternalSession internalSession = mock(InternalSession.class); - when(internalSession.getSession()).thenReturn(repositorySystemSession); - - sessionScope.enter(); - sessionScope.seed(InternalSession.class, internalSession); - } - @Override protected String getProjectsDirectory() { return "src/test/projects/project-builder"; diff --git a/maven-core/src/test/java/org/apache/maven/project/ProjectModelResolverTest.java b/maven-core/src/test/java/org/apache/maven/project/ProjectModelResolverTest.java index a43f1d688134..ec8cb759b8f3 100644 --- a/maven-core/src/test/java/org/apache/maven/project/ProjectModelResolverTest.java +++ b/maven-core/src/test/java/org/apache/maven/project/ProjectModelResolverTest.java @@ -18,8 +18,6 @@ */ package org.apache.maven.project; -import javax.inject.Inject; - import java.io.File; import java.util.Collections; import java.util.List; @@ -27,19 +25,15 @@ import org.apache.maven.artifact.InvalidRepositoryException; import org.apache.maven.bridge.MavenRepositorySystem; -import org.apache.maven.internal.impl.InternalSession; import org.apache.maven.model.Dependency; import org.apache.maven.model.Parent; import org.apache.maven.model.resolution.ModelResolver; import org.apache.maven.model.resolution.UnresolvableModelException; import org.apache.maven.repository.internal.MavenRepositorySystemUtils; -import org.apache.maven.session.scope.internal.SessionScope; import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.RepositorySystem; -import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.impl.RemoteRepositoryManager; import org.eclipse.aether.repository.RemoteRepository; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.codehaus.plexus.testing.PlexusExtension.getBasedir; @@ -48,8 +42,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; /** * Test cases for the project {@code ModelResolver} implementation. @@ -58,18 +50,11 @@ */ class ProjectModelResolverTest extends AbstractMavenProjectTestCase { - @Inject - SessionScope sessionScope; - - @BeforeEach - public void setUp() { - RepositorySystemSession repositorySystemSession = mock(RepositorySystemSession.class); - when(repositorySystemSession.getConfigProperties()).thenReturn(Collections.emptyMap()); - InternalSession internalSession = mock(InternalSession.class); - when(internalSession.getSession()).thenReturn(repositorySystemSession); - - sessionScope.enter(); - sessionScope.seed(InternalSession.class, internalSession); + /** + * Creates a new {@code ProjectModelResolverTest} instance. + */ + public ProjectModelResolverTest() { + super(); } @Test diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionSchemeProvider.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionSchemeProvider.java new file mode 100644 index 000000000000..8bebb05f5796 --- /dev/null +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionSchemeProvider.java @@ -0,0 +1,44 @@ +/* + * 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. + */ +package org.apache.maven.repository.internal; + +import javax.inject.Named; +import javax.inject.Provider; +import javax.inject.Singleton; + +import org.eclipse.aether.util.version.GenericVersionScheme; +import org.eclipse.aether.version.VersionScheme; + +/** + * Default version scheme provider: provides singleton {@link GenericVersionScheme} instance. + */ +@Singleton +@Named +public final class DefaultVersionSchemeProvider implements Provider { + private final GenericVersionScheme genericVersionScheme; + + public DefaultVersionSchemeProvider() { + this.genericVersionScheme = new GenericVersionScheme(); + } + + @Override + public VersionScheme get() { + return genericVersionScheme; + } +} From eed8263347164a46e9336a842b62f8bd5822723a Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Sat, 16 Dec 2023 20:07:51 +0100 Subject: [PATCH 14/21] Simplify even more --- .../internal/impl/DefaultVersionParser.java | 102 ++---------------- .../internal/DefaultModelVersionParser.java} | 6 +- 2 files changed, 9 insertions(+), 99 deletions(-) rename maven-resolver-provider/src/{test/java/org/apache/maven/repository/internal/util/TestVersionParser.java => main/java/org/apache/maven/repository/internal/DefaultModelVersionParser.java} (95%) diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java index 631a3cd280fd..5cc2b106a497 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java +++ b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultVersionParser.java @@ -20,18 +20,13 @@ import javax.inject.Inject; import javax.inject.Named; -import javax.inject.Provider; 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.eclipse.aether.version.InvalidVersionSpecificationException; -import org.eclipse.aether.version.VersionScheme; import static org.apache.maven.internal.impl.Utils.nonNull; @@ -40,27 +35,25 @@ */ @Named @Singleton -public class DefaultVersionParser implements VersionParser, org.apache.maven.model.version.VersionParser { +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 Provider versionSchemeProvider; + private final org.apache.maven.model.version.VersionParser modelVersionParser; @Inject - public DefaultVersionParser(Provider versionSchemeProvider) { - this.versionSchemeProvider = nonNull(versionSchemeProvider, "versionSchemeProvider"); + public DefaultVersionParser(org.apache.maven.model.version.VersionParser modelVersionParser) { + this.modelVersionParser = nonNull(modelVersionParser, "modelVersionParser"); } @Override public Version parseVersion(String version) { - nonNull(version, "version"); - return new DefaultVersion(versionSchemeProvider.get(), version); + return modelVersionParser.parseVersion(version); } @Override public VersionRange parseVersionRange(String range) { - nonNull(range, "range"); - return new DefaultVersionRange(versionSchemeProvider.get(), range); + return modelVersionParser.parseVersionRange(range); } @Override @@ -71,87 +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 VersionScheme versionScheme; - private final org.eclipse.aether.version.Version delegate; - - DefaultVersion(VersionScheme versionScheme, String delegateValue) { - this.versionScheme = versionScheme; - try { - this.delegate = versionScheme.parseVersion(delegateValue); - } catch (InvalidVersionSpecificationException e) { - throw new VersionParserException("Unable to parse version: " + delegateValue, e); - } - } - - @Override - public int compareTo(Version o) { - if (o instanceof DefaultVersion) { - return delegate.compareTo(((DefaultVersion) o).delegate); - } else { - return compareTo(new DefaultVersion(versionScheme, o.asString())); - } - } - - @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 VersionScheme versionScheme; - private final org.eclipse.aether.version.VersionRange delegate; - - DefaultVersionRange(VersionScheme versionScheme, String delegateValue) { - this.versionScheme = versionScheme; - try { - this.delegate = versionScheme.parseVersionRange(delegateValue); - } catch (InvalidVersionSpecificationException e) { - throw new VersionParserException("Unable to parse version range: " + delegateValue, e); - } - } - - @Override - public boolean contains(Version version) { - if (version instanceof DefaultVersion) { - return delegate.containsVersion(((DefaultVersion) version).delegate); - } else { - return contains(new DefaultVersion(versionScheme, version.asString())); - } - } - - @Override - public String asString() { - return delegate.toString(); - } - - @Override - public String toString() { - return asString(); - } - } } diff --git a/maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/util/TestVersionParser.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelVersionParser.java similarity index 95% rename from maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/util/TestVersionParser.java rename to maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelVersionParser.java index d9a31ebde953..6bba42369d9b 100644 --- a/maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/util/TestVersionParser.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelVersionParser.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.maven.repository.internal.util; +package org.apache.maven.repository.internal; import javax.inject.Inject; import javax.inject.Named; @@ -36,11 +36,11 @@ @Named @Singleton -public class TestVersionParser implements VersionParser { +public class DefaultModelVersionParser implements VersionParser { private final Provider versionSchemeProvider; @Inject - public TestVersionParser(Provider versionSchemeProvider) { + public DefaultModelVersionParser(Provider versionSchemeProvider) { this.versionSchemeProvider = requireNonNull(versionSchemeProvider, "versionSchemeProvider"); } From 701c247eb2cd5e67af43c1f51692ad127d09d0bc Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Sat, 16 Dec 2023 22:06:24 +0100 Subject: [PATCH 15/21] Undo unneeded changes --- .../MavenPluginMavenPrerequisiteChecker.java | 37 +++---------------- .../maven/rtinfo/RuntimeInformation.java | 2 - .../internal/DefaultRuntimeInformation.java | 10 ++--- ...ginParameterExpressionEvaluatorV4Test.java | 23 ++---------- .../DefaultMavenProjectBuilderTest.java | 19 ---------- 5 files changed, 14 insertions(+), 77 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginMavenPrerequisiteChecker.java b/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginMavenPrerequisiteChecker.java index fb921efa93b1..55dec64fb5f2 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginMavenPrerequisiteChecker.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginMavenPrerequisiteChecker.java @@ -20,16 +20,11 @@ import javax.inject.Inject; import javax.inject.Named; -import javax.inject.Provider; import javax.inject.Singleton; import org.apache.maven.plugin.MavenPluginPrerequisitesChecker; import org.apache.maven.plugin.descriptor.PluginDescriptor; import org.apache.maven.rtinfo.RuntimeInformation; -import org.eclipse.aether.version.InvalidVersionSpecificationException; -import org.eclipse.aether.version.Version; -import org.eclipse.aether.version.VersionConstraint; -import org.eclipse.aether.version.VersionScheme; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,15 +32,12 @@ @Singleton public class MavenPluginMavenPrerequisiteChecker implements MavenPluginPrerequisitesChecker { private final Logger logger = LoggerFactory.getLogger(getClass()); - private final RuntimeInformation runtimeInformation; - private final Provider versionSchemeProvider; @Inject - public MavenPluginMavenPrerequisiteChecker( - RuntimeInformation runtimeInformation, Provider versionSchemeProvider) { + public MavenPluginMavenPrerequisiteChecker(RuntimeInformation runtimeInformation) { + super(); this.runtimeInformation = runtimeInformation; - this.versionSchemeProvider = versionSchemeProvider; } @Override @@ -56,35 +48,16 @@ public void accept(PluginDescriptor pluginDescriptor) { requiredMavenVersion == null || requiredMavenVersion.trim().isEmpty(); if (!isBlankVersion) { - VersionScheme versionScheme = versionSchemeProvider.get(); - VersionConstraint constraint; + boolean isRequirementMet = false; try { - constraint = versionScheme.parseVersionConstraint(requiredMavenVersion); - } catch (InvalidVersionSpecificationException e) { + isRequirementMet = runtimeInformation.isMavenVersion(requiredMavenVersion); + } catch (IllegalArgumentException e) { logger.warn( "Could not verify plugin's Maven prerequisite as an invalid version is given in " + requiredMavenVersion, e); return; } - - Version current; - try { - String mavenVersion = runtimeInformation.getMavenVersion(); - if (mavenVersion == null || mavenVersion.isEmpty()) { - throw new IllegalArgumentException("Could not determine current Maven version"); - } - current = versionScheme.parseVersion(mavenVersion); - } catch (InvalidVersionSpecificationException e) { - throw new IllegalStateException("Could not parse current Maven version: " + e.getMessage(), e); - } - - boolean isRequirementMet; - if (constraint.getRange() == null) { - isRequirementMet = constraint.getVersion().compareTo(current) <= 0; - } else { - isRequirementMet = constraint.containsVersion(current); - } if (!isRequirementMet) { throw new IllegalStateException("Required Maven version " + requiredMavenVersion + " is not met by current version " + runtimeInformation.getMavenVersion()); diff --git a/maven-core/src/main/java/org/apache/maven/rtinfo/RuntimeInformation.java b/maven-core/src/main/java/org/apache/maven/rtinfo/RuntimeInformation.java index 6bbd8e3b2af2..847c8b09f8dc 100644 --- a/maven-core/src/main/java/org/apache/maven/rtinfo/RuntimeInformation.java +++ b/maven-core/src/main/java/org/apache/maven/rtinfo/RuntimeInformation.java @@ -41,8 +41,6 @@ public interface RuntimeInformation { * @return {@code true} if the current Maven runtime matches the specified version range, {@code false} otherwise. * @throws IllegalArgumentException If the specified version range is {@code null}, empty or otherwise not a valid * version specification. - * @deprecated This method is deprecated, use {@link #getMavenVersion()} instead. */ - @Deprecated boolean isMavenVersion(String versionRange); } diff --git a/maven-core/src/main/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformation.java b/maven-core/src/main/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformation.java index 9624115f3bd1..aa35239a4110 100644 --- a/maven-core/src/main/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformation.java +++ b/maven-core/src/main/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformation.java @@ -20,6 +20,7 @@ import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Provider; import javax.inject.Singleton; import java.io.IOException; @@ -28,7 +29,6 @@ import java.util.Properties; 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; @@ -44,13 +44,13 @@ public class DefaultRuntimeInformation implements RuntimeInformation { private final Logger logger = LoggerFactory.getLogger(getClass()); - @Deprecated - private final VersionScheme versionScheme = new GenericVersionScheme(); + private final Provider versionSchemeProvider; private final String mavenVersion; @Inject - public DefaultRuntimeInformation() { + public DefaultRuntimeInformation(Provider versionSchemeProvider) { + this.versionSchemeProvider = versionSchemeProvider; this.mavenVersion = loadMavenVersion(); } @@ -88,13 +88,13 @@ private String loadMavenVersion() { } } - @Deprecated @Override public boolean isMavenVersion(String versionRange) { if (Objects.requireNonNull(versionRange, "versionRange cannot be null").isEmpty()) { throw new IllegalArgumentException("versionRange cannot be empty"); } + VersionScheme versionScheme = versionSchemeProvider.get(); VersionConstraint constraint; try { constraint = versionScheme.parseVersionConstraint(versionRange); diff --git a/maven-core/src/test/java/org/apache/maven/plugin/PluginParameterExpressionEvaluatorV4Test.java b/maven-core/src/test/java/org/apache/maven/plugin/PluginParameterExpressionEvaluatorV4Test.java index e2a556db2a2e..eb676599130a 100644 --- a/maven-core/src/test/java/org/apache/maven/plugin/PluginParameterExpressionEvaluatorV4Test.java +++ b/maven-core/src/test/java/org/apache/maven/plugin/PluginParameterExpressionEvaluatorV4Test.java @@ -44,7 +44,10 @@ import org.apache.maven.execution.DefaultMavenExecutionResult; import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.execution.MavenSession; -import org.apache.maven.internal.impl.*; +import org.apache.maven.internal.impl.AbstractSession; +import org.apache.maven.internal.impl.DefaultMojoExecution; +import org.apache.maven.internal.impl.DefaultProject; +import org.apache.maven.internal.impl.DefaultSession; import org.apache.maven.model.Build; import org.apache.maven.model.Model; import org.apache.maven.model.building.DefaultModelBuildingRequest; @@ -55,7 +58,6 @@ import org.apache.maven.project.CycleDetectedException; import org.apache.maven.project.DuplicateProjectException; import org.apache.maven.project.MavenProject; -import org.apache.maven.session.scope.internal.SessionScope; import org.codehaus.plexus.MutablePlexusContainer; import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; @@ -63,12 +65,10 @@ import org.codehaus.plexus.util.Os; import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.RepositorySystem; -import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.graph.DefaultDependencyNode; import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; import org.eclipse.aether.repository.LocalRepository; import org.eclipse.aether.repository.NoLocalRepositoryManagerException; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.codehaus.plexus.testing.PlexusExtension.getTestFile; @@ -80,7 +80,6 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; /** */ @@ -90,25 +89,11 @@ public class PluginParameterExpressionEvaluatorV4Test extends AbstractCoreMavenC @Inject PlexusContainer container; - @Inject - SessionScope sessionScope; - @Inject private MavenRepositorySystem factory; private Path rootDirectory; - @BeforeEach - public void setUp() { - RepositorySystemSession repositorySystemSession = mock(RepositorySystemSession.class); - when(repositorySystemSession.getConfigProperties()).thenReturn(Collections.emptyMap()); - InternalSession internalSession = mock(InternalSession.class); - when(internalSession.getSession()).thenReturn(repositorySystemSession); - - sessionScope.enter(); - sessionScope.seed(InternalSession.class, internalSession); - } - @Test public void testPluginDescriptorExpressionReference() throws Exception { Session session = newSession(); diff --git a/maven-core/src/test/java/org/apache/maven/project/DefaultMavenProjectBuilderTest.java b/maven-core/src/test/java/org/apache/maven/project/DefaultMavenProjectBuilderTest.java index fa82f4c68f14..97152f5e4e35 100644 --- a/maven-core/src/test/java/org/apache/maven/project/DefaultMavenProjectBuilderTest.java +++ b/maven-core/src/test/java/org/apache/maven/project/DefaultMavenProjectBuilderTest.java @@ -18,21 +18,15 @@ */ package org.apache.maven.project; -import javax.inject.Inject; - import java.io.File; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; -import java.util.Collections; import java.util.List; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.internal.impl.InternalSession; -import org.apache.maven.session.scope.internal.SessionScope; -import org.eclipse.aether.RepositorySystemSession; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -51,8 +45,6 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; class DefaultMavenProjectBuilderTest extends AbstractMavenProjectTestCase { @TempDir @@ -62,21 +54,10 @@ class DefaultMavenProjectBuilderTest extends AbstractMavenProjectTestCase { @TempDir Path projectRoot; - @Inject - SessionScope sessionScope; - @Override @BeforeEach public void setUp() throws Exception { projectBuilder = getContainer().lookup(ProjectBuilder.class); - - RepositorySystemSession repositorySystemSession = mock(RepositorySystemSession.class); - when(repositorySystemSession.getConfigProperties()).thenReturn(Collections.emptyMap()); - InternalSession internalSession = mock(InternalSession.class); - when(internalSession.getSession()).thenReturn(repositorySystemSession); - - sessionScope.enter(); - sessionScope.seed(InternalSession.class, internalSession); } protected MavenProject getProject(Artifact pom, boolean allowStub) throws Exception { From 530cfeb6ce6b496e7a063f38a47e2ed65e072048 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Sun, 17 Dec 2023 13:41:37 +0100 Subject: [PATCH 16/21] Resolver 2.0.0-alpha-5 is released. --- pom.xml | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/pom.xml b/pom.xml index 78311d328b41..e05c9fe57f7c 100644 --- a/pom.xml +++ b/pom.xml @@ -502,20 +502,6 @@ under the License. - - - - - true - - - false - - maven-2048 - https://repository.apache.org/content/repositories/maven-2048 - - - From 49cec292edce1b8aeb5d8f54a7e729ad70f00a5b Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Mon, 18 Dec 2023 08:43:30 +0100 Subject: [PATCH 17/21] Do not use Provider on the injection side when not really required --- .../impl/DefaultConsumerPomBuilder.java | 77 ++++++++++--------- .../impl/ConsumerPomBuilderTest.java | 9 +++ 2 files changed, 49 insertions(+), 37 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java b/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java index 6db2cab138eb..5784c5c21df1 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java @@ -20,7 +20,6 @@ import javax.inject.Inject; import javax.inject.Named; -import javax.inject.Provider; import java.nio.file.Path; import java.util.ArrayList; @@ -30,6 +29,7 @@ import java.util.Properties; import java.util.stream.Collectors; +import org.apache.maven.SessionScoped; import org.apache.maven.api.model.Dependency; import org.apache.maven.api.model.DependencyManagement; import org.apache.maven.api.model.DistributionManagement; @@ -72,8 +72,11 @@ import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.RequestTrace; import org.eclipse.aether.impl.RemoteRepositoryManager; +import org.eclipse.sisu.Typed; @Named +@SessionScoped +@Typed class DefaultConsumerPomBuilder implements ConsumerPomBuilder { private static final String BOM_PACKAGING = "bom"; @@ -83,59 +86,59 @@ class DefaultConsumerPomBuilder implements ConsumerPomBuilder { private ModelCacheFactory modelCacheFactory; @Inject - private Provider profileInjector; + private ProfileInjector profileInjector; @Inject - private Provider inheritanceAssembler; + private InheritanceAssembler inheritanceAssembler; @Inject - private Provider dependencyManagementImporter; + private DependencyManagementImporter dependencyManagementImporter; @Inject - private Provider dependencyManagementInjector; + private DependencyManagementInjector dependencyManagementInjector; @Inject - private Provider lifecycleBindingsInjector; + private LifecycleBindingsInjector lifecycleBindingsInjector; @Inject - private Provider modelInterpolator; + private ModelInterpolator modelInterpolator; @Inject - private Provider modelNormalizer; + private ModelNormalizer modelNormalizer; @Inject - private Provider modelPathTranslator; + private ModelPathTranslator modelPathTranslator; @Inject - private Provider modelProcessor; + private ModelProcessor modelProcessor; @Inject - private Provider modelUrlNormalizer; + private ModelUrlNormalizer modelUrlNormalizer; @Inject - private Provider modelValidator; + private ModelValidator modelValidator; @Inject - private Provider pluginConfigurationExpander; + private PluginConfigurationExpander pluginConfigurationExpander; @Inject - private Provider pluginManagementInjector; + private PluginManagementInjector pluginManagementInjector; @Inject - private Provider reportConfigurationExpander; + private ReportConfigurationExpander reportConfigurationExpander; @Inject - private Provider superPomProvider; + private SuperPomProvider superPomProvider; @Inject - private Provider versionParser; + private VersionParser versionParser; // To break circular dependency @Inject - private Provider repositorySystem; + private RepositorySystem repositorySystem; @Inject - private Provider remoteRepositoryManager; + private RemoteRepositoryManager remoteRepositoryManager; @Override public Model build(RepositorySystemSession session, MavenProject project, Path src) throws ModelBuildingException { @@ -175,22 +178,22 @@ public List getActiveProfilesV4( DefaultModelBuilder modelBuilder = new DefaultModelBuilderFactory() .setProfileSelector(customSelector) // apply currently active ModelProcessor etc. to support extensions like jgitver - .setProfileInjector(profileInjector.get()) - .setInheritanceAssembler(inheritanceAssembler.get()) - .setDependencyManagementImporter(dependencyManagementImporter.get()) - .setDependencyManagementInjector(dependencyManagementInjector.get()) - .setLifecycleBindingsInjector(lifecycleBindingsInjector.get()) - .setModelInterpolator(modelInterpolator.get()) - .setModelNormalizer(modelNormalizer.get()) - .setModelPathTranslator(modelPathTranslator.get()) - .setModelProcessor(modelProcessor.get()) - .setModelUrlNormalizer(modelUrlNormalizer.get()) - .setModelValidator(modelValidator.get()) - .setPluginConfigurationExpander(pluginConfigurationExpander.get()) - .setPluginManagementInjector(pluginManagementInjector.get()) - .setReportConfigurationExpander(reportConfigurationExpander.get()) - .setSuperPomProvider(superPomProvider.get()) - .setModelVersionParser(versionParser.get()) + .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 { @@ -204,8 +207,8 @@ public List getActiveProfilesV4( request.setModelResolver(new ProjectModelResolver( session, new RequestTrace(null), - repositorySystem.get(), - remoteRepositoryManager.get(), + repositorySystem, + remoteRepositoryManager, project.getRemoteProjectRepositories(), ProjectBuildingRequest.RepositoryMerging.POM_DOMINANT, null)); diff --git a/maven-core/src/test/java/org/apache/maven/internal/transformation/impl/ConsumerPomBuilderTest.java b/maven-core/src/test/java/org/apache/maven/internal/transformation/impl/ConsumerPomBuilderTest.java index 9cf8c9312603..6221663ce2a9 100644 --- a/maven-core/src/test/java/org/apache/maven/internal/transformation/impl/ConsumerPomBuilderTest.java +++ b/maven-core/src/test/java/org/apache/maven/internal/transformation/impl/ConsumerPomBuilderTest.java @@ -32,7 +32,9 @@ import org.apache.maven.internal.transformation.AbstractRepositoryTestCase; import org.apache.maven.model.v4.MavenStaxReader; import org.apache.maven.project.MavenProject; +import org.apache.maven.session.scope.internal.SessionScope; import org.eclipse.aether.DefaultRepositorySystemSession; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -43,6 +45,13 @@ public class ConsumerPomBuilderTest extends AbstractRepositoryTestCase { @Inject ConsumerPomBuilder builder; + @BeforeEach + public void setUp() throws Exception { + super.setUp(); + SessionScope scope = container.lookup(SessionScope.class); + scope.enter(); + } + @Test void testTrivialConsumer() throws Exception { MavenProject project; From 5ce52798bea3c990bd407ef3b6cf2e5b6000e7fd Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Mon, 18 Dec 2023 08:58:06 +0100 Subject: [PATCH 18/21] Remove usage of Provider --- .../internal/MavenPluginJavaPrerequisiteChecker.java | 8 +++----- .../version/internal/DefaultPluginVersionResolver.java | 9 ++++----- .../rtinfo/internal/DefaultRuntimeInformation.java | 8 +++----- .../MavenPluginJavaPrerequisiteCheckerTest.java | 2 +- .../repository/internal/DefaultModelVersionParser.java | 10 +++++----- .../internal/DefaultVersionRangeResolver.java | 9 +++------ 6 files changed, 19 insertions(+), 27 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteChecker.java b/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteChecker.java index 75ceafe6bf26..45b5b9896dcc 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteChecker.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteChecker.java @@ -20,7 +20,6 @@ import javax.inject.Inject; import javax.inject.Named; -import javax.inject.Provider; import javax.inject.Singleton; import org.apache.maven.plugin.MavenPluginPrerequisitesChecker; @@ -33,11 +32,11 @@ @Named @Singleton public class MavenPluginJavaPrerequisiteChecker implements MavenPluginPrerequisitesChecker { - private final Provider versionSchemeProvider; + private final VersionScheme versionScheme; @Inject - public MavenPluginJavaPrerequisiteChecker(Provider versionSchemeProvider) { - this.versionSchemeProvider = versionSchemeProvider; + public MavenPluginJavaPrerequisiteChecker(VersionScheme versionScheme) { + this.versionScheme = versionScheme; } @Override @@ -53,7 +52,6 @@ public void accept(PluginDescriptor pluginDescriptor) { } boolean matchesVersion(String requiredVersion, String currentVersion) { - VersionScheme versionScheme = versionSchemeProvider.get(); VersionConstraint constraint; try { constraint = versionScheme.parseVersionConstraint(requiredVersion); diff --git a/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java b/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java index 310e3aa6037b..904606a2a04a 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java @@ -20,7 +20,6 @@ import javax.inject.Inject; import javax.inject.Named; -import javax.inject.Provider; import javax.inject.Singleton; import java.io.IOException; @@ -80,18 +79,18 @@ public class DefaultPluginVersionResolver implements PluginVersionResolver { private final RepositorySystem repositorySystem; private final MetadataReader metadataReader; private final MavenPluginManager pluginManager; - private final Provider versionSchemeProvider; + private final VersionScheme versionScheme; @Inject public DefaultPluginVersionResolver( RepositorySystem repositorySystem, MetadataReader metadataReader, MavenPluginManager pluginManager, - Provider versionSchemeProvider) { + VersionScheme versionScheme) { this.repositorySystem = repositorySystem; this.metadataReader = metadataReader; this.pluginManager = pluginManager; - this.versionSchemeProvider = versionSchemeProvider; + this.versionScheme = versionScheme; } @Override @@ -195,7 +194,7 @@ private void selectVersion(DefaultPluginVersionResult result, PluginVersionReque for (String ver : versions.versions.keySet()) { try { - Version v = versionSchemeProvider.get().parseVersion(ver); + Version v = versionScheme.parseVersion(ver); if (ver.endsWith("-SNAPSHOT")) { snapshots.add(v); diff --git a/maven-core/src/main/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformation.java b/maven-core/src/main/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformation.java index aa35239a4110..5b940b8ab30c 100644 --- a/maven-core/src/main/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformation.java +++ b/maven-core/src/main/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformation.java @@ -20,7 +20,6 @@ import javax.inject.Inject; import javax.inject.Named; -import javax.inject.Provider; import javax.inject.Singleton; import java.io.IOException; @@ -44,13 +43,13 @@ public class DefaultRuntimeInformation implements RuntimeInformation { private final Logger logger = LoggerFactory.getLogger(getClass()); - private final Provider versionSchemeProvider; + private final VersionScheme versionScheme; private final String mavenVersion; @Inject - public DefaultRuntimeInformation(Provider versionSchemeProvider) { - this.versionSchemeProvider = versionSchemeProvider; + public DefaultRuntimeInformation(VersionScheme versionScheme) { + this.versionScheme = versionScheme; this.mavenVersion = loadMavenVersion(); } @@ -94,7 +93,6 @@ public boolean isMavenVersion(String versionRange) { throw new IllegalArgumentException("versionRange cannot be empty"); } - VersionScheme versionScheme = versionSchemeProvider.get(); VersionConstraint constraint; try { constraint = versionScheme.parseVersionConstraint(versionRange); diff --git a/maven-core/src/test/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteCheckerTest.java b/maven-core/src/test/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteCheckerTest.java index 14a331e992a4..2c68ebbf2d1b 100644 --- a/maven-core/src/test/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteCheckerTest.java +++ b/maven-core/src/test/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteCheckerTest.java @@ -27,7 +27,7 @@ class MavenPluginJavaPrerequisiteCheckerTest { @Test void testMatchesVersion() { - MavenPluginJavaPrerequisiteChecker checker = new MavenPluginJavaPrerequisiteChecker(GenericVersionScheme::new); + MavenPluginJavaPrerequisiteChecker checker = new MavenPluginJavaPrerequisiteChecker(new GenericVersionScheme()); assertTrue(checker.matchesVersion("1.0", "1.8")); assertTrue(checker.matchesVersion("1.8", "9.0.1+11")); assertFalse(checker.matchesVersion("[1.0,2],[3,4]", "2.1")); diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelVersionParser.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelVersionParser.java index 6bba42369d9b..390866b22484 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelVersionParser.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelVersionParser.java @@ -37,23 +37,23 @@ @Named @Singleton public class DefaultModelVersionParser implements VersionParser { - private final Provider versionSchemeProvider; + private final VersionScheme versionScheme; @Inject - public DefaultModelVersionParser(Provider versionSchemeProvider) { - this.versionSchemeProvider = requireNonNull(versionSchemeProvider, "versionSchemeProvider"); + public DefaultModelVersionParser(VersionScheme versionScheme) { + this.versionScheme = requireNonNull(versionScheme, "versionScheme"); } @Override public Version parseVersion(String version) { requireNonNull(version, "version"); - return new DefaultVersion(versionSchemeProvider.get(), version); + return new DefaultVersion(versionScheme, version); } @Override public VersionRange parseVersionRange(String range) { requireNonNull(range, "range"); - return new DefaultVersionRange(versionSchemeProvider.get(), range); + return new DefaultVersionRange(versionScheme, range); } static class DefaultVersion implements Version { diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java index 8df25a7c2d1e..c8ca6bd374d6 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java @@ -20,7 +20,6 @@ import javax.inject.Inject; import javax.inject.Named; -import javax.inject.Provider; import javax.inject.Singleton; import java.io.InputStream; @@ -71,20 +70,19 @@ public class DefaultVersionRangeResolver implements VersionRangeResolver { private final MetadataResolver metadataResolver; private final SyncContextFactory syncContextFactory; private final RepositoryEventDispatcher repositoryEventDispatcher; - private final Provider versionSchemeProvider; + private final VersionScheme versionScheme; @Inject public DefaultVersionRangeResolver( MetadataResolver metadataResolver, SyncContextFactory syncContextFactory, RepositoryEventDispatcher repositoryEventDispatcher, - Provider versionSchemeProvider) { + 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.versionSchemeProvider = - Objects.requireNonNull(versionSchemeProvider, "versionSchemeProvider cannot be null"); + this.versionScheme = Objects.requireNonNull(versionScheme, "versionScheme cannot be null"); } @Override @@ -92,7 +90,6 @@ public VersionRangeResult resolveVersionRange(RepositorySystemSession session, V throws VersionRangeResolutionException { VersionRangeResult result = new VersionRangeResult(request); - VersionScheme versionScheme = versionSchemeProvider.get(); VersionConstraint versionConstraint; try { versionConstraint = From 88bc6f29685a0961583789602a3d59d8cb8e8ceb Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Mon, 18 Dec 2023 09:10:41 +0100 Subject: [PATCH 19/21] Fix code --- .../maven/repository/internal/DefaultModelVersionParser.java | 1 - 1 file changed, 1 deletion(-) diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelVersionParser.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelVersionParser.java index 390866b22484..02338cb728bf 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelVersionParser.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelVersionParser.java @@ -20,7 +20,6 @@ import javax.inject.Inject; import javax.inject.Named; -import javax.inject.Provider; import javax.inject.Singleton; import java.util.Objects; From 6ca4861952d1144668a57d2e53e516a19bdccaff Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Mon, 18 Dec 2023 10:29:27 +0100 Subject: [PATCH 20/21] DefaultConsumerPomBuilder should not be in session scope --- .../transformation/impl/DefaultConsumerPomBuilder.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java b/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java index 5784c5c21df1..5878edf7a27c 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java @@ -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; @@ -29,7 +30,6 @@ import java.util.Properties; import java.util.stream.Collectors; -import org.apache.maven.SessionScoped; import org.apache.maven.api.model.Dependency; import org.apache.maven.api.model.DependencyManagement; import org.apache.maven.api.model.DistributionManagement; @@ -72,11 +72,8 @@ import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.RequestTrace; import org.eclipse.aether.impl.RemoteRepositoryManager; -import org.eclipse.sisu.Typed; @Named -@SessionScoped -@Typed class DefaultConsumerPomBuilder implements ConsumerPomBuilder { private static final String BOM_PACKAGING = "bom"; @@ -135,7 +132,7 @@ class DefaultConsumerPomBuilder implements ConsumerPomBuilder { // To break circular dependency @Inject - private RepositorySystem repositorySystem; + private Provider repositorySystem; @Inject private RemoteRepositoryManager remoteRepositoryManager; @@ -207,7 +204,7 @@ public List getActiveProfilesV4( request.setModelResolver(new ProjectModelResolver( session, new RequestTrace(null), - repositorySystem, + repositorySystem.get(), remoteRepositoryManager, project.getRemoteProjectRepositories(), ProjectBuildingRequest.RepositoryMerging.POM_DOMINANT, From 9199b937e01962b6b123774209a42701a087ea19 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Mon, 18 Dec 2023 11:48:03 +0100 Subject: [PATCH 21/21] Undo this change Not needed anymore --- .../transformation/impl/ConsumerPomBuilderTest.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/maven-core/src/test/java/org/apache/maven/internal/transformation/impl/ConsumerPomBuilderTest.java b/maven-core/src/test/java/org/apache/maven/internal/transformation/impl/ConsumerPomBuilderTest.java index 6221663ce2a9..9cf8c9312603 100644 --- a/maven-core/src/test/java/org/apache/maven/internal/transformation/impl/ConsumerPomBuilderTest.java +++ b/maven-core/src/test/java/org/apache/maven/internal/transformation/impl/ConsumerPomBuilderTest.java @@ -32,9 +32,7 @@ import org.apache.maven.internal.transformation.AbstractRepositoryTestCase; import org.apache.maven.model.v4.MavenStaxReader; import org.apache.maven.project.MavenProject; -import org.apache.maven.session.scope.internal.SessionScope; import org.eclipse.aether.DefaultRepositorySystemSession; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -45,13 +43,6 @@ public class ConsumerPomBuilderTest extends AbstractRepositoryTestCase { @Inject ConsumerPomBuilder builder; - @BeforeEach - public void setUp() throws Exception { - super.setUp(); - SessionScope scope = container.lookup(SessionScope.class); - scope.enter(); - } - @Test void testTrivialConsumer() throws Exception { MavenProject project;