Skip to content

Commit

Permalink
[MNG-7969][MNG-7981] Add missing information on Maven 4 version api t…
Browse files Browse the repository at this point in the history
…o fix an exception (#1355)

Co-authored-by: Guillaume Nodet <gnodet@gmail.com>
  • Loading branch information
cstamas and gnodet authored Dec 19, 2023
1 parent fe71f7d commit a381970
Show file tree
Hide file tree
Showing 35 changed files with 1,729 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public interface ArtifactCoordinate {
* @return the version
*/
@Nonnull
VersionRange getVersion();
VersionConstraint getVersion();

/**
* The extension of the artifact.
Expand Down
11 changes: 11 additions & 0 deletions api/maven-api-core/src/main/java/org/apache/maven/api/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -463,4 +463,15 @@ Artifact createArtifact(
*/
@Nonnull
VersionRange parseVersionRange(@Nonnull String versionRange);

/**
* Parses the specified version constraint specification, for example "1.0" or "[1.0,2.0)".
* <p>
* Shortcut for {@code getService(VersionParser.class).parseVersionConstraint(...)}.
*
* @see org.apache.maven.api.services.VersionParser#parseVersionConstraint(String)
* @throws org.apache.maven.api.services.VersionParserException if the parsing failed
*/
@Nonnull
VersionConstraint parseVersionConstraint(@Nonnull String versionConstraint);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
*/
@Experimental
public interface Version extends Comparable<Version> {

// TODO: add access to the version information

/**
* Returns a string representation of this version.
* @return the string representation of this version
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.api;

import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.Nullable;

/**
* Version constraint for dependency. Constraint is either a range ("[1,2)") or recommended version ("1.0").
*
* @since 4.0.0
*/
@Experimental
public interface VersionConstraint {
/**
* Returns the range of this constraint, or {@code null} if none.
* <p>
* Note: only one, this method or {@link #getRecommendedVersion()} method must return non-{@code null} value.
*/
@Nullable
VersionRange getVersionRange();

/**
* Returns the recommended version of this constraint, or {@code null} if none.
* <p>
* Note: only one, this method or {@link #getVersionRange()} method must return non-{@code null} value.
*/
@Nullable
Version getRecommendedVersion();

/**
* Determines whether the specified version is contained within this constraint.
*
* @param version the version to test, must not be {@code null}
* @return {@code true} if this range contains the specified version, {@code false} otherwise
*/
boolean contains(@Nonnull Version version);

/**
* Returns a string representation of this version constraint
* @return the string representation of this version constraint
*/
@Nonnull
String asString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.Nullable;

/**
* A range of versions.
Expand All @@ -28,9 +29,6 @@
*/
@Experimental
public interface VersionRange {

// TODO: v4: add access to the version information

/**
* Determines whether the specified version is contained within this range.
*
Expand All @@ -39,10 +37,37 @@ public interface VersionRange {
*/
boolean contains(@Nonnull Version version);

/**
* Returns the upper boundary of this range, or {@code null} if none.
*/
@Nullable
Boundary getUpperBoundary();

/**
* Returns the lower boundary of this range, or {@code null} if none.
*/
@Nullable
Boundary getLowerBoundary();

/**
* Returns a string representation of this version range
* @return the string representation of this version range
*/
@Nonnull
String asString();

/**
* Represents range boundary.
*/
interface Boundary {
/**
* The bounding version.
*/
Version getVersion();

/**
* Returns {@code true} if version is included of the range.
*/
boolean isInclusive();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.apache.maven.api.Service;
import org.apache.maven.api.Version;
import org.apache.maven.api.VersionConstraint;
import org.apache.maven.api.VersionRange;
import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull;
Expand Down Expand Up @@ -53,6 +54,16 @@ public interface VersionParser extends Service {
@Nonnull
VersionRange parseVersionRange(@Nonnull String range);

/**
* Parses the specified version constraint specification, for example "1.0" or "[1.0,2.0)".
*
* @param constraint the constraint specification to parse, must not be {@code null}
* @return the parsed version constraint, never {@code null}
* @throws VersionParserException if the range specification violates the syntax rules of this scheme
*/
@Nonnull
VersionConstraint parseVersionConstraint(@Nonnull String constraint);

/**
* Checks whether a given artifact version is considered a {@code SNAPSHOT} or not.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,11 @@ public VersionRange parseVersionRange(String versionRange) {
return getService(VersionParser.class).parseVersionRange(versionRange);
}

@Override
public VersionConstraint parseVersionConstraint(String versionConstraint) {
return getService(VersionParser.class).parseVersionConstraint(versionConstraint);
}

@Override
public Version resolveVersion(ArtifactCoordinate artifact) {
return getService(VersionResolver.class).resolve(this, artifact).getVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.Objects;

import org.apache.maven.api.ArtifactCoordinate;
import org.apache.maven.api.VersionRange;
import org.apache.maven.api.VersionConstraint;
import org.apache.maven.api.annotations.Nonnull;

import static org.apache.maven.internal.impl.Utils.nonNull;
Expand Down Expand Up @@ -57,8 +57,8 @@ public String getArtifactId() {

@Nonnull
@Override
public VersionRange getVersion() {
return session.parseVersionRange(coordinate.getVersion());
public VersionConstraint getVersion() {
return session.parseVersionConstraint(coordinate.getVersion());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@

import java.util.Collection;

import org.apache.maven.api.DependencyCoordinate;
import org.apache.maven.api.Exclusion;
import org.apache.maven.api.Scope;
import org.apache.maven.api.Type;
import org.apache.maven.api.VersionRange;
import org.apache.maven.api.*;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.Nullable;
import org.apache.maven.api.services.TypeRegistry;
Expand Down Expand Up @@ -63,8 +59,8 @@ public String getClassifier() {
}

@Override
public VersionRange getVersion() {
return session.parseVersionRange(dependency.getArtifact().getVersion());
public VersionConstraint getVersion() {
return session.parseVersionConstraint(dependency.getArtifact().getVersion());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,15 @@ public DependencyResolverResult resolve(DependencyResolverRequest request)
List<ArtifactCoordinate> coordinates =
deps.stream().map(Artifact::toCoordinate).collect(Collectors.toList());
Map<Artifact, Path> artifacts = session.resolveArtifacts(coordinates);
Map<Dependency, Path> dependencies = deps.stream().collect(Collectors.toMap(d -> d, artifacts::get));
List<Path> paths = new ArrayList<>(dependencies.values());
Map<Dependency, Path> dependencies = new LinkedHashMap<>();
List<Path> paths = new ArrayList<>();
for (Dependency d : deps) {
Path path = artifacts.get(d);
if (dependencies.put(d, path) != null) {
throw new IllegalStateException("Duplicate key");
}
paths.add(path);
}

return new DefaultDependencyResolverResult(
collectorResult.getExceptions(), collectorResult.getRoot(), nodes, paths, dependencies);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,7 @@
import java.util.Optional;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.api.Artifact;
import org.apache.maven.api.DependencyCoordinate;
import org.apache.maven.api.Exclusion;
import org.apache.maven.api.Project;
import org.apache.maven.api.RemoteRepository;
import org.apache.maven.api.Scope;
import org.apache.maven.api.Type;
import org.apache.maven.api.VersionRange;
import org.apache.maven.api.*;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.Nullable;
import org.apache.maven.api.model.DependencyManagement;
Expand Down Expand Up @@ -180,8 +173,8 @@ public String getClassifier() {
}

@Override
public VersionRange getVersion() {
return session.parseVersionRange(dependency.getVersion());
public VersionConstraint getVersion() {
return session.parseVersionConstraint(dependency.getVersion());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import java.util.regex.Pattern;

import org.apache.maven.api.Version;
import org.apache.maven.api.VersionConstraint;
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;

Expand All @@ -39,10 +41,10 @@ public class DefaultVersionParser implements VersionParser {
private static final String SNAPSHOT = "SNAPSHOT";
private static final Pattern SNAPSHOT_TIMESTAMP = Pattern.compile("^(.*-)?([0-9]{8}\\.[0-9]{6}-[0-9]+)$");

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

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

Expand All @@ -56,6 +58,11 @@ public VersionRange parseVersionRange(String range) {
return modelVersionParser.parseVersionRange(range);
}

@Override
public VersionConstraint parseVersionConstraint(String constraint) {
return modelVersionParser.parseVersionConstraint(constraint);
}

@Override
public boolean isSnapshot(String version) {
return checkSnapshot(version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +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.model.version.ModelVersionParser;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.project.ProjectModelResolver;
Expand Down Expand Up @@ -128,7 +128,7 @@ class DefaultConsumerPomBuilder implements ConsumerPomBuilder {
private SuperPomProvider superPomProvider;

@Inject
private VersionParser versionParser;
private ModelVersionParser versionParser;

// To break circular dependency
@Inject
Expand Down
Loading

0 comments on commit a381970

Please sign in to comment.