Skip to content

Commit

Permalink
Add more tools (#143)
Browse files Browse the repository at this point in the history
That are usable for example in ITs
  • Loading branch information
cstamas authored Nov 28, 2024
1 parent e475b76 commit 858dcfd
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import eu.maveniverse.maven.toolbox.shared.internal.ToolboxCommandoImpl;
import eu.maveniverse.maven.toolbox.shared.output.Output;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -25,6 +26,7 @@
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.collection.CollectResult;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.metadata.Metadata;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactDescriptorException;
import org.eclipse.aether.resolution.VersionRangeResolutionException;
Expand Down Expand Up @@ -220,6 +222,25 @@ public RecordStats(boolean active, int recordedCount) {
*/
Result<String> recordStop();

/**
* Returns the base path of local repository.
*/
Result<Path> localRepository() throws Exception;

/**
* Returns the path in local repository of requested artifact. Remote repository is nullable, if present,
* a "remote artifact" (cached) path will be returned, otherwise "local artifact".
* The returned path is relative to local repository base.
*/
Result<Path> artifactPath(Artifact artifact, RemoteRepository repository) throws Exception;

/**
* Returns the path in local repository of requested metadata. Remote repository is nullable, if present,
* a "remote metadata" (cached) path will be returned, otherwise "local metadata".
* The returned path is relative to local repository base.
*/
Result<Path> metadataPath(Metadata metadata, RemoteRepository repository) throws Exception;

/**
* Resolves given artifacts.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import org.eclipse.aether.graph.DependencyFilter;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.graph.DependencyVisitor;
import org.eclipse.aether.metadata.Metadata;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactDescriptorException;
import org.eclipse.aether.resolution.ArtifactResolutionException;
Expand Down Expand Up @@ -536,6 +537,42 @@ public Result<String> recordStop() {
return result ? Result.success("Stopped") : Result.failure("No recorder state changed");
}

@Override
public Result<Path> localRepository() throws Exception {
Result<Path> result =
Result.success(session.getLocalRepository().getBasedir().toPath());
result.getData().ifPresent(path -> output.tell(path.toString()));
return result;
}

@Override
public Result<Path> artifactPath(Artifact artifact, RemoteRepository repository) throws Exception {
Result<Path> result;
if (repository == null) {
result =
Result.success(Paths.get(session.getLocalRepositoryManager().getPathForLocalArtifact(artifact)));
} else {
result = Result.success(Paths.get(
session.getLocalRepositoryManager().getPathForRemoteArtifact(artifact, repository, "toolbox")));
}
result.getData().ifPresent(path -> output.tell(path.toString()));
return result;
}

@Override
public Result<Path> metadataPath(Metadata metadata, RemoteRepository repository) throws Exception {
Result<Path> result;
if (repository == null) {
result =
Result.success(Paths.get(session.getLocalRepositoryManager().getPathForLocalMetadata(metadata)));
} else {
result = Result.success(Paths.get(
session.getLocalRepositoryManager().getPathForRemoteMetadata(metadata, repository, "toolbox")));
}
result.getData().ifPresent(path -> output.tell(path.toString()));
return result;
}

@Override
public Result<List<Artifact>> resolve(
Source<Artifact> artifactSource, boolean sources, boolean javadoc, boolean signature, Sink<Artifact> sink)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package eu.maveniverse.maven.toolbox.plugin;

import eu.maveniverse.maven.toolbox.plugin.gav.GavArtifactPathMojo;
import eu.maveniverse.maven.toolbox.plugin.gav.GavClasspathMojo;
import eu.maveniverse.maven.toolbox.plugin.gav.GavCopyGavMojo;
import eu.maveniverse.maven.toolbox.plugin.gav.GavCopyMojo;
Expand All @@ -25,6 +26,8 @@
import eu.maveniverse.maven.toolbox.plugin.gav.GavListAvailablePluginsMojo;
import eu.maveniverse.maven.toolbox.plugin.gav.GavListMojo;
import eu.maveniverse.maven.toolbox.plugin.gav.GavListRepositoriesMojo;
import eu.maveniverse.maven.toolbox.plugin.gav.GavLocalRepositoryPathMojo;
import eu.maveniverse.maven.toolbox.plugin.gav.GavMetadataPathMojo;
import eu.maveniverse.maven.toolbox.plugin.gav.GavRecordMojo;
import eu.maveniverse.maven.toolbox.plugin.gav.GavReplMojo;
import eu.maveniverse.maven.toolbox.plugin.gav.GavResolveMojo;
Expand All @@ -42,6 +45,7 @@
@CommandLine.Command(
name = "toolbox",
subcommands = {
GavArtifactPathMojo.class,
GavClasspathMojo.class,
GavCopyGavMojo.class,
GavCopyMojo.class,
Expand All @@ -60,6 +64,8 @@
GavListAvailablePluginsMojo.class,
GavListMojo.class,
GavListRepositoriesMojo.class,
GavLocalRepositoryPathMojo.class,
GavMetadataPathMojo.class,
GavRecordMojo.class,
GavReplMojo.class,
GavResolveMojo.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ public final Integer call() {
@Parameter(defaultValue = "${session.request.showErrors}", readonly = true, required = true)
protected boolean mojoErrors;

@Parameter(property = "forceStdout")
protected boolean forceStdout;

/**
* Option to run potentially destructive commands without performing any IO.
*/
Expand All @@ -283,7 +286,12 @@ public final void execute() throws MojoExecutionException, MojoFailureException
CONTEXT.compareAndSet(null, new HashMap<>());
getOrCreate(Runtime.class, Runtimes.INSTANCE::getRuntime);
getOrCreate(Context.class, () -> get(Runtime.class).create(createMojoContextOverrides()));
getOrCreate(Output.class, () -> OutputFactory.createMojoOutput(!mojoInteractiveMode, mojoErrors, verbosity));
if (forceStdout) {
getOrCreate(Output.class, () -> OutputFactory.createCliOutput(!mojoInteractiveMode, mojoErrors, verbosity));
} else {
getOrCreate(
Output.class, () -> OutputFactory.createMojoOutput(!mojoInteractiveMode, mojoErrors, verbosity));
}
try {
Result<?> result = doExecute();
if (!result.isSuccess() && failOnLogicalFailure) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2023-2024 Maveniverse Org.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*/
package eu.maveniverse.maven.toolbox.plugin.gav;

import eu.maveniverse.maven.toolbox.plugin.GavMojoSupport;
import eu.maveniverse.maven.toolbox.shared.Result;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import java.nio.file.Path;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.repository.RemoteRepository;
import picocli.CommandLine;

/**
* Prints Maven Artifact path in local repository.
*/
@CommandLine.Command(name = "artifact-path", description = "Prints path of Maven Artifacts in local repository")
@Mojo(name = "gav-artifact-path", requiresProject = false, threadSafe = true)
public class GavArtifactPathMojo extends GavMojoSupport {
/**
* The GAV of artifact.
*/
@CommandLine.Parameters(index = "0", description = "The Artifact coordinates", arity = "1")
@Parameter(property = "gav", required = true)
private String gav;

/**
* The optional remote repository spec string. It is expected to be in form of {@code id::url}, but we are really
* interested in repository ID only.
*/
@CommandLine.Option(
names = {"--repository"},
description =
"The optional remote repository spec string. It is expected to be in form of {@code id::url}, but we are really interested in repository ID only.")
@Parameter(property = "repository")
private String repository;

@Override
protected Result<Path> doExecute() throws Exception {
ToolboxCommando toolboxCommando = getToolboxCommando();
RemoteRepository rr = null;
if (repository != null) {
rr = toolboxCommando.parseRemoteRepository(repository);
}
return toolboxCommando.artifactPath(new DefaultArtifact(gav), rr);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2023-2024 Maveniverse Org.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*/
package eu.maveniverse.maven.toolbox.plugin.gav;

import eu.maveniverse.maven.toolbox.plugin.GavMojoSupport;
import eu.maveniverse.maven.toolbox.shared.Result;
import java.nio.file.Path;
import org.apache.maven.plugins.annotations.Mojo;
import picocli.CommandLine;

/**
* Prints Maven Local Repository path.
*/
@CommandLine.Command(name = "local-repository-path", description = "Prints path of Maven Local Repository")
@Mojo(name = "gav-local-repository-path", requiresProject = false, threadSafe = true)
public class GavLocalRepositoryPathMojo extends GavMojoSupport {
@Override
protected Result<Path> doExecute() throws Exception {
return getToolboxCommando().localRepository();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2023-2024 Maveniverse Org.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*/
package eu.maveniverse.maven.toolbox.plugin.gav;

import eu.maveniverse.maven.toolbox.plugin.GavMojoSupport;
import eu.maveniverse.maven.toolbox.shared.Result;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import java.nio.file.Path;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.eclipse.aether.metadata.DefaultMetadata;
import org.eclipse.aether.metadata.Metadata;
import org.eclipse.aether.repository.RemoteRepository;
import picocli.CommandLine;

/**
* Prints Maven Metadata path in local repository.
*/
@CommandLine.Command(name = "metadata-path", description = "Prints path of Maven Metadata in local repository")
@Mojo(name = "gav-metadata-path", requiresProject = false, threadSafe = true)
public class GavMetadataPathMojo extends GavMojoSupport {
/**
* The metadata coordinates in form of {@code [G]:[A]:[V]:[type]}. Absence of {@code A} implies absence of {@code V}
* as well (in other words, it can be {@code G}, {@code G:A} or {@code G:A:V}). The absence of {@code type} implies
* it is "maven-metadata.xml". The simplest spec string is {@code :::}.
* <p>
* Examples:
* <ul>
* <li>{@code :::} is root metadata named "maven-metadata.xml"</li>
* <li>{@code :::my-metadata.xml} is root metadata named "my-metadata.xml"</li>
* <li>{@code G:::} equals to {@code G:::maven-metadata.xml}</li>
* <li>{@code G:A::} equals to {@code G:A::maven-metadata.xml}</li>
* </ul>
*/
@CommandLine.Parameters(index = "0", description = "The Metadata coordinates", arity = "1")
@Parameter(property = "gav", required = true)
private String gav;

/**
* The optional remote repository spec string. It is expected to be in form of {@code id::url}, but we are really
* interested in repository ID only.
*/
@CommandLine.Option(
names = {"--repository"},
description =
"The optional remote repository spec string. It is expected to be in form of {@code id::url}, but we are really interested in repository ID only.")
@Parameter(property = "repository")
private String repository;

@Override
protected Result<Path> doExecute() throws Exception {
ToolboxCommando toolboxCommando = getToolboxCommando();
RemoteRepository rr = null;
if (repository != null) {
rr = toolboxCommando.parseRemoteRepository(repository);
}

String groupId = null;
String artifactId = null;
String version = null;
String type = "maven-metadata.xml";
String[] elems = gav.split(":", 0);
if (elems.length > 0) {
groupId = elems[0];
}
if (elems.length > 1) {
artifactId = elems[1];
}
if (elems.length > 2) {
version = elems[2];
}
if (elems.length > 3) {
type = elems[3];
}
if (elems.length > 4) {
throw new IllegalArgumentException("Invalid gav: " + gav);
}
return toolboxCommando.metadataPath(
new DefaultMetadata(groupId, artifactId, version, type, Metadata.Nature.RELEASE_OR_SNAPSHOT), rr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ public class GavResolveMojo extends GavMojoSupport {
@Parameter(property = "gav", required = true)
private String gav;

/**
* Comma separated list of BOMs to apply.
*/
@CommandLine.Option(
names = {"--boms"},
defaultValue = "",
description = "Comma separated list of BOMs to apply")
@Parameter(property = "boms")
private String boms;

/**
* Resolve sources JAR as well (derive coordinates from GAV).
*/
Expand Down

0 comments on commit 858dcfd

Please sign in to comment.