[MNG-7972] Artifact coordinate out of "standard string" (#1347)

Expose ArtifactCoordinate creation out of standard string.

---

https://issues.apache.org/jira/browse/MNG-7972
This commit is contained in:
Tamas Cservenak 2023-12-18 11:30:20 +01:00 committed by GitHub
parent 84a48a8bf2
commit 6e192fbb54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 140 additions and 42 deletions

View File

@ -194,14 +194,26 @@ public interface Session {
/**
* Shortcut for {@code getService(ArtifactFactory.class).create(...)}.
*
* @see org.apache.maven.api.services.ArtifactFactory#create(Session, String, String, String, String)
* @see org.apache.maven.api.services.ArtifactCoordinateFactory#create(Session, String, String, String, String)
*/
ArtifactCoordinate createArtifactCoordinate(String groupId, String artifactId, String version, String extension);
/**
* Creates a coordinate out of string that is formatted like:
* {@code <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>}
* <p>
* Shortcut for {@code getService(ArtifactFactory.class).create(...)}.
*
* @see org.apache.maven.api.services.ArtifactFactory#create(Session, String, String, String, String, String, String)
* @param coordString the string having "standard" coordinate.
* @return an {@code ArtifactCoordinate}, never {@code null}
* @see org.apache.maven.api.services.ArtifactCoordinateFactory#create(Session, String)
*/
ArtifactCoordinate createArtifactCoordinate(String coordString);
/**
* Shortcut for {@code getService(ArtifactFactory.class).create(...)}.
*
* @see org.apache.maven.api.services.ArtifactCoordinateFactory#create(Session, String, String, String, String, String, String)
*/
ArtifactCoordinate createArtifactCoordinate(
String groupId, String artifactId, String version, String classifier, String extension, String type);
@ -209,7 +221,7 @@ public interface Session {
/**
* Shortcut for {@code getService(ArtifactFactory.class).create(...)}.
*
* @see org.apache.maven.api.services.ArtifactFactory#create(Session, String, String, String, String, String, String)
* @see org.apache.maven.api.services.ArtifactCoordinateFactory#create(Session, String, String, String, String, String, String)
*/
ArtifactCoordinate createArtifactCoordinate(Artifact artifact);

View File

@ -37,12 +37,26 @@ public interface ArtifactCoordinateFactory extends Service {
* Creates a coordinate.
*
* @param request the request holding coordinate creation parameters
* @return an {@code Artifact}, never {@code null}
* @return an {@code ArtifactCoordinate}, never {@code null}
* @throws IllegalArgumentException if {@code request} is null or {@code request.session} is null or invalid
*/
@Nonnull
ArtifactCoordinate create(@Nonnull ArtifactCoordinateFactoryRequest request);
/**
* Creates a coordinate out of string that is formatted like:
* {@code <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>}
*
* @param session the session.
* @param coordinateString the string having "standard" coordinate.
* @return an {@code ArtifactCoordinate}, never {@code null}
* @throws IllegalArgumentException if {@code session} is null or invalid
*/
@Nonnull
default ArtifactCoordinate create(@Nonnull Session session, @Nonnull String coordinateString) {
return create(ArtifactCoordinateFactoryRequest.build(session, coordinateString));
}
@Nonnull
default ArtifactCoordinate create(
@Nonnull Session session, String groupId, String artifactId, String version, String extension) {

View File

@ -51,11 +51,13 @@ public interface ArtifactCoordinateFactoryRequest {
String getType();
String getCoordinateString();
@Nonnull
static ArtifactCoordinateFactoryRequest build(
@Nonnull Session session, String groupId, String artifactId, String version, String extension) {
return ArtifactCoordinateFactoryRequest.builder()
.session(nonNull(session, "session cannot be null"))
.session(nonNull(session, "session"))
.groupId(groupId)
.artifactId(artifactId)
.version(version)
@ -73,7 +75,7 @@ public interface ArtifactCoordinateFactoryRequest {
String extension,
String type) {
return ArtifactCoordinateFactoryRequest.builder()
.session(nonNull(session, "session cannot be null"))
.session(nonNull(session, "session"))
.groupId(groupId)
.artifactId(artifactId)
.version(version)
@ -83,11 +85,19 @@ public interface ArtifactCoordinateFactoryRequest {
.build();
}
@Nonnull
static ArtifactCoordinateFactoryRequest build(@Nonnull Session session, @Nonnull String coordinateString) {
return ArtifactCoordinateFactoryRequest.builder()
.session(nonNull(session, "session"))
.coordinateString(nonNull(coordinateString, "coordinateString"))
.build();
}
@Nonnull
static ArtifactCoordinateFactoryRequest build(@Nonnull Session session, @Nonnull ArtifactCoordinate coordinate) {
return ArtifactCoordinateFactoryRequest.builder()
.session(nonNull(session, "session cannot be null"))
.groupId(nonNull(coordinate, "coordinate cannot be null").getGroupId())
.session(nonNull(session, "session"))
.groupId(nonNull(coordinate, "coordinate").getGroupId())
.artifactId(coordinate.getArtifactId())
.classifier(coordinate.getClassifier())
.version(coordinate.getVersion().asString())
@ -108,6 +118,7 @@ public interface ArtifactCoordinateFactoryRequest {
private String classifier;
private String extension;
private String type;
private String coordinateString;
ArtifactFactoryRequestBuilder() {}
@ -146,9 +157,14 @@ public interface ArtifactCoordinateFactoryRequest {
return this;
}
public ArtifactFactoryRequestBuilder coordinateString(String coordinateString) {
this.coordinateString = coordinateString;
return this;
}
public ArtifactCoordinateFactoryRequest build() {
return new DefaultArtifactFactoryRequestArtifact(
session, groupId, artifactId, version, classifier, extension, type);
session, groupId, artifactId, version, classifier, extension, type, coordinateString);
}
private static class DefaultArtifactFactoryRequestArtifact extends BaseRequest
@ -159,6 +175,7 @@ public interface ArtifactCoordinateFactoryRequest {
private final String classifier;
private final String extension;
private final String type;
private final String coordinateString;
DefaultArtifactFactoryRequestArtifact(
@Nonnull Session session,
@ -167,7 +184,8 @@ public interface ArtifactCoordinateFactoryRequest {
String version,
String classifier,
String extension,
String type) {
String type,
String coordinateString) {
super(session);
this.groupId = groupId;
this.artifactId = artifactId;
@ -175,6 +193,7 @@ public interface ArtifactCoordinateFactoryRequest {
this.classifier = classifier;
this.extension = extension;
this.type = type;
this.coordinateString = coordinateString;
}
@Override
@ -206,6 +225,10 @@ public interface ArtifactCoordinateFactoryRequest {
public String getType() {
return type;
}
public String getCoordinateString() {
return coordinateString;
}
}
}
}

View File

@ -109,6 +109,7 @@ public interface DependencyCoordinateFactoryRequest extends ArtifactCoordinateFa
private String classifier;
private String extension;
private String type;
private String coordinateString;
private String scope;
private boolean optional;
private Collection<Exclusion> exclusions = Collections.emptyList();
@ -150,6 +151,11 @@ public interface DependencyCoordinateFactoryRequest extends ArtifactCoordinateFa
return this;
}
public DependencyCoordinateFactoryRequestBuilder coordinateString(String coordinateString) {
this.coordinateString = coordinateString;
return this;
}
public DependencyCoordinateFactoryRequestBuilder scope(String scope) {
this.scope = scope;
return this;
@ -182,7 +188,17 @@ public interface DependencyCoordinateFactoryRequest extends ArtifactCoordinateFa
public DependencyCoordinateFactoryRequest build() {
return new DefaultDependencyCoordinateFactoryRequest(
session, groupId, artifactId, version, classifier, extension, type, scope, optional, exclusions);
session,
groupId,
artifactId,
version,
classifier,
extension,
type,
coordinateString,
scope,
optional,
exclusions);
}
private static class DefaultDependencyCoordinateFactoryRequest extends BaseRequest
@ -193,6 +209,7 @@ public interface DependencyCoordinateFactoryRequest extends ArtifactCoordinateFa
private final String classifier;
private final String extension;
private final String type;
private final String coordinateString;
private final String scope;
private final boolean optional;
private final Collection<Exclusion> exclusions;
@ -206,6 +223,7 @@ public interface DependencyCoordinateFactoryRequest extends ArtifactCoordinateFa
String classifier,
String extension,
String type,
String coordinateString,
String scope,
boolean optional,
Collection<Exclusion> exclusions) {
@ -216,6 +234,7 @@ public interface DependencyCoordinateFactoryRequest extends ArtifactCoordinateFa
this.classifier = classifier;
this.extension = extension;
this.type = type;
this.coordinateString = coordinateString;
this.scope = scope;
this.optional = optional;
this.exclusions = exclusions;
@ -251,6 +270,10 @@ public interface DependencyCoordinateFactoryRequest extends ArtifactCoordinateFa
return type;
}
public String getCoordinateString() {
return coordinateString;
}
@Override
public String getScope() {
return scope;

View File

@ -219,6 +219,16 @@ public abstract class AbstractSession implements InternalSession {
return getService(ArtifactCoordinateFactory.class).create(this, groupId, artifactId, version, extension);
}
/**
* Shortcut for <code>getService(CoordinateFactory.class).create(...)</code>
*
* @see ArtifactCoordinateFactory#create(Session, String)
*/
@Override
public ArtifactCoordinate createArtifactCoordinate(String coordString) {
return getService(ArtifactCoordinateFactory.class).create(this, coordString);
}
/**
* Shortcut for <code>getService(CoordinateFactory.class).create(...)</code>
*

View File

@ -36,24 +36,30 @@ public class DefaultArtifactCoordinateFactory implements ArtifactCoordinateFacto
public ArtifactCoordinate create(@Nonnull ArtifactCoordinateFactoryRequest request) {
nonNull(request, "request");
InternalSession session = InternalSession.from(request.getSession());
ArtifactType type = null;
if (request.getType() != null) {
type = session.getSession().getArtifactTypeRegistry().get(request.getType());
if (request.getCoordinateString() != null) {
return new DefaultArtifactCoordinate(
session, new org.eclipse.aether.artifact.DefaultArtifact(request.getCoordinateString()));
} else {
ArtifactType type = null;
if (request.getType() != null) {
type = session.getSession().getArtifactTypeRegistry().get(request.getType());
}
String str1 = request.getClassifier();
String classifier = str1 != null && !str1.isEmpty()
? request.getClassifier()
: type != null ? type.getClassifier() : "";
String str = request.getExtension();
String extension =
str != null && !str.isEmpty() ? request.getExtension() : type != null ? type.getExtension() : "";
return new DefaultArtifactCoordinate(
session,
new org.eclipse.aether.artifact.DefaultArtifact(
request.getGroupId(),
request.getArtifactId(),
classifier,
extension,
request.getVersion(),
type));
}
String str1 = request.getClassifier();
String classifier =
str1 != null && !str1.isEmpty() ? request.getClassifier() : type != null ? type.getClassifier() : "";
String str = request.getExtension();
String extension =
str != null && !str.isEmpty() ? request.getExtension() : type != null ? type.getExtension() : "";
return new DefaultArtifactCoordinate(
session,
new org.eclipse.aether.artifact.DefaultArtifact(
request.getGroupId(),
request.getArtifactId(),
classifier,
extension,
request.getVersion(),
type));
}
}

View File

@ -45,19 +45,29 @@ public class DefaultDependencyCoordinateFactory implements DependencyCoordinateF
if (request.getType() != null) {
type = session.getSession().getArtifactTypeRegistry().get(request.getType());
}
return new DefaultDependencyCoordinate(
session,
new org.eclipse.aether.graph.Dependency(
new org.eclipse.aether.artifact.DefaultArtifact(
request.getGroupId(),
request.getArtifactId(),
request.getClassifier(),
request.getExtension(),
request.getVersion(),
type),
request.getScope(),
request.isOptional(),
map(request.getExclusions(), this::toExclusion)));
if (request.getCoordinateString() != null) {
return new DefaultDependencyCoordinate(
session,
new org.eclipse.aether.graph.Dependency(
new org.eclipse.aether.artifact.DefaultArtifact(request.getCoordinateString()),
request.getScope(),
request.isOptional(),
map(request.getExclusions(), this::toExclusion)));
} else {
return new DefaultDependencyCoordinate(
session,
new org.eclipse.aether.graph.Dependency(
new org.eclipse.aether.artifact.DefaultArtifact(
request.getGroupId(),
request.getArtifactId(),
request.getClassifier(),
request.getExtension(),
request.getVersion(),
type),
request.getScope(),
request.isOptional(),
map(request.getExclusions(), this::toExclusion)));
}
}
private org.eclipse.aether.graph.Exclusion toExclusion(Exclusion exclusion) {