Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare Maven Connector by introducing Maveniverse MIMA (MIni MAven) #1065

Merged
merged 9 commits into from
Feb 8, 2025
10 changes: 10 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ enola_maven.install(
"org.jsoup:jsoup:1.18.1",
"org.apache.tika:tika-core:3.0.0-BETA2",
"org.apache.tika:tika-parsers-standard-package:3.0.0-BETA2",

# Maven Connector
"eu.maveniverse.maven.mima.runtime:standalone-static:2.4.21",
"eu.maveniverse.maven.mima:context:2.4.21",
"eu.maveniverse.maven.mima.extensions:mmr:2.4.21",
"org.apache.maven.resolver:maven-resolver-api:1.9.22",
"org.apache.maven.resolver:maven-resolver-util:1.9.22",
"org.apache.maven:maven-model:3.9.9",

# IPFS & IPLD etc.
"com.github.ipld:java-cid:1.3.8",

# TODO Replace with "com.github.multiformats:java-multibase:..."
Expand Down
50 changes: 50 additions & 0 deletions java/dev/enola/model/enola/maven/connect/mima/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# SPDX-License-Identifier: Apache-2.0
#
# Copyright 2023 The Enola <https://enola.dev> Authors
#
# Licensed 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
#
# https://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.

load("@rules_java//java:defs.bzl", "java_library")
load("//tools/bazel:junit.bzl", "junit_tests")

java_library(
name = "mima",
srcs = glob(
["*.java"],
exclude = ["*Test.java"],
),
plugins = ["//tools/bazel/java_plugin:autoservice"],
visibility = ["//:__subpackages__"],
deps = [
"@enola_maven//:com_google_auto_service_auto_service_annotations",
"@enola_maven//:com_google_errorprone_error_prone_annotations",
"@enola_maven//:com_google_guava_guava",
"@enola_maven//:eu_maveniverse_maven_mima_context",
"@enola_maven//:eu_maveniverse_maven_mima_extensions_mmr",
"@enola_maven//:eu_maveniverse_maven_mima_runtime_standalone_static",
"@enola_maven//:org_apache_maven_maven_model",
"@enola_maven//:org_apache_maven_resolver_maven_resolver_api",
"@enola_maven//:org_apache_maven_resolver_maven_resolver_util",
"@enola_maven//:org_jspecify_jspecify",
"@enola_maven//:org_slf4j_slf4j_api",
],
)

junit_tests(
name = "tests",
srcs = glob(["*Test.java"]),
deps = [
":mima",
"@enola_maven//:org_apache_maven_resolver_maven_resolver_api",
],
)
183 changes: 183 additions & 0 deletions java/dev/enola/model/enola/maven/connect/mima/GAVR.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2025 The Enola <https://enola.dev> Authors
*
* Licensed 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
*
* https://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 dev.enola.model.enola.maven.connect.mima;

import static com.google.common.base.Strings.isNullOrEmpty;

import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;

/**
* GAVR is a Maven GroupID, ArtifactID, Version, Extension (AKA Type), Classifier + Repository.
*
* <p>The GroupID, ArtifactID & Version are mandatory and cannot be empty. The Extension, Classifier
* & Repository can be empty, but never null.
*
* <p>This class itself does NOT imply any "defaults" for Extension, Classifier & Repository; but
* it's users may well.
*/
public record GAVR(
String groupId,
String artifactId,
String extension,
String classifier,
String version,
String repo) {

// TODO Consider #performance - make this a class to cache Gradle & PkgURL representations?

/**
* Parse a "short Gradle-style" GAV in the
* <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version> format, like e.g.
* "ch.vorburger.mariaDB4j:mariaDB4j-core:3.1.0" to a GAVR. NB: This syntax does not allow
* specifying a repository!
*/
public static GAVR parseGradle(String gav) {
var artifact = new DefaultArtifact(gav);
return new GAVR(
artifact.getGroupId(),
artifact.getArtifactId(),
artifact.getExtension(),
artifact.getClassifier(),
artifact.getVersion(),
"");
}

// TODO public GAVR parsePkgURL(String purl), with
// https://github.com/package-url/packageurl-java

public static class Builder {
private String groupId;
private String artifactId;
private String extension;
private String classifier;
private String version;
private String repo;

public Builder groupId(String groupId) {
this.groupId = groupId;
return this;
}

public Builder artifactId(String artifactId) {
this.artifactId = artifactId;
return this;
}

public Builder extension(String extension) {
this.extension = extension;
return this;
}

public Builder classifier(String classifier) {
this.classifier = classifier;
return this;
}

public Builder version(String version) {
this.version = version;
return this;
}

public Builder repo(String repo) {
this.repo = repo;
return this;
}

public GAVR build() {
return new GAVR(
groupId,
artifactId,
nullToEmpty(extension),
nullToEmpty(classifier),
version,
nullToEmpty(repo));
}

private String nullToEmpty(String string) {
if (string == null) return "";
else return string;
}
}

public GAVR {
requireNonEmpty(groupId, "groupId");
requireNonEmpty(artifactId, "artifactId");
requireNonNull(extension, "extension");
requireNonNull(classifier, "classifier");
requireNonEmpty(version, "version");
requireNonNull(repo, "repo");
}

private void requireNonNull(Object object, String field) {
if (object == null) throw new IllegalStateException(field + " cannot be null");
}

private void requireNonEmpty(String string, String field) {
if (isNullOrEmpty(string))
throw new IllegalStateException(field + " cannot be null or empty");
}

/** Return a String in the same format that {@link #parseGradle(String)} uses. */
@SuppressWarnings("StringBufferReplaceableByString") // pre-sizing is more efficient (?)
public String toGradle() {
var sb = // w.o. repo.length()
new StringBuilder(
4 // max. 4x ':'
+ groupId.length()
+ artifactId.length()
+ extension.length()
+ classifier.length()
+ version.length());

sb.append(groupId);
sb.append(':');
sb.append(artifactId);

if (!extension.isEmpty()) {
sb.append(':');
sb.append(extension);
}

if (!classifier.isEmpty()) {
sb.append(':');
sb.append(classifier);
}

sb.append(':');
sb.append(version);
return sb.toString();
}

// TODO String toPkgURL()

public Builder toBuilder() {
return new Builder()
.groupId(groupId)
.artifactId(artifactId)
.extension(extension)
.classifier(classifier)
.version(version);
}

// NOT public - this is a package private internal method!
// NB: Artifact does NOT include the Repository! Callers will use repository() to obtain that.
Artifact toArtifact() {
return new DefaultArtifact(groupId, artifactId, classifier, extension, version);
}
}
Loading