Skip to content

Commit

Permalink
Make it usable in 3.6.x-3.9.x maven versions
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Mar 21, 2024
1 parent 75c1a5a commit 84e5684
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ protected void push(String key, Object object) {
deque.push(object);
}

protected Object pop(String key) {
ArrayDeque<Object> deque = EXECUTION_CONTEXT.computeIfAbsent(key, k -> new ArrayDeque<>());
return deque.pop();
}

@Override
public String[] getVersion() {
return new String[] {
Expand Down Expand Up @@ -212,6 +217,11 @@ protected Context getContext() {
return (Context) getOrCreate(Context.class.getName(), () -> getRuntime().create(getContextOverrides()));
}

protected ToolboxCommando getToolboxCommando(Context context) {
return (ToolboxCommando)
getOrCreate(ToolboxCommando.class.getName(), () -> ToolboxCommando.create(getRuntime(), context));
}

protected void verbose(String format, Object... args) {
if (!verbose) {
return;
Expand Down Expand Up @@ -371,7 +381,7 @@ private void writeThrowable(Throwable t, PrintStream stream, String caption, Str
printStackTrace(t, stream, prefix);
}

protected String getLocation(final StackTraceElement e) {
private String getLocation(final StackTraceElement e) {
assert e != null;

if (e.isNativeMethod()) {
Expand All @@ -385,10 +395,6 @@ protected String getLocation(final StackTraceElement e) {
}
}

protected ToolboxCommando getToolboxCommando(Context context) {
return ToolboxCommando.getOrCreate(getRuntime(), context);
}

@Override
public final Integer call() {
Ansi.setEnabled(!batch);
Expand Down
4 changes: 4 additions & 0 deletions maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

<name>${project.groupId}:${project.artifactId}</name>

<prerequisites>
<maven>3.6.3</maven>
</prerequisites>

<dependencies>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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;

import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import org.apache.maven.plugins.annotations.Mojo;

@Mojo(name = "dump", threadSafe = true)
public class DumpMojo extends ProjectMojoSupport {
@Override
protected void doExecute(ToolboxCommando toolboxCommando) {
toolboxCommando.dump(verbose, output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class GavTreeMojo extends AbstractMojo {
public void execute() throws MojoExecutionException, MojoFailureException {
Runtime runtime = Runtimes.INSTANCE.getRuntime();
try (Context context = runtime.create(ContextOverrides.create().build())) {
ToolboxCommando toolboxCommando = ToolboxCommando.getOrCreate(runtime, context);
ToolboxCommando toolboxCommando = ToolboxCommando.create(runtime, context);
toolboxCommando.tree(
ResolutionScope.parse(scope), toolboxCommando.loadGav(gav, boms), verbose, new Slf4jOutput(logger));
} catch (RuntimeException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected ResolutionRoot projectAsResolutionRoot() {
public final void execute() throws MojoExecutionException, MojoFailureException {
Runtime runtime = Runtimes.INSTANCE.getRuntime();
try (Context context = runtime.create(ContextOverrides.create().build())) {
doExecute(ToolboxCommando.getOrCreate(runtime, context));
doExecute(ToolboxCommando.create(runtime, context));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import static java.util.Objects.requireNonNull;

import eu.maveniverse.maven.mima.context.Context;
import eu.maveniverse.maven.mima.context.ContextOverrides;
import eu.maveniverse.maven.mima.context.Runtime;
import eu.maveniverse.maven.toolbox.shared.internal.ToolboxCommandoImpl;
import java.io.Closeable;
Expand All @@ -34,31 +33,14 @@ public interface ToolboxCommando extends Closeable {
* Gets or creates context. This method should be used to get {@link ToolboxResolver} instance that may be shared
* across context (session).
*/
static ToolboxCommando getOrCreate(Runtime runtime, Context context) {
static ToolboxCommando create(Runtime runtime, Context context) {
requireNonNull(runtime, "runtime");
requireNonNull(context, "context");
return (ToolboxCommando)
context.repositorySystemSession().getData().computeIfAbsent(ToolboxCommando.class, () -> {
ToolboxCommandoImpl result = new ToolboxCommandoImpl(runtime, context);
context.repositorySystem().addOnSystemEndedHandler(result::close);
return result;
});
return new ToolboxCommandoImpl(runtime, context);
}

/**
* Removes toolbox from context (session).
*/
static void unset(Context context) {
requireNonNull(context, "context");
context.repositorySystemSession().getData().set(ToolboxCommando.class, null);
}

/**
* Derives new customized commando instance.
*/
ToolboxCommando derive(ContextOverrides contextOverrides);

/**
* Closes this instance. Closed instance should be used anymore.
* Closes this instance. Closed instance should be used anymore. Also closes underlying Context.
*/
void close();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import static org.apache.maven.search.api.request.Query.query;

import eu.maveniverse.maven.mima.context.Context;
import eu.maveniverse.maven.mima.context.ContextOverrides;
import eu.maveniverse.maven.mima.context.HTTPProxy;
import eu.maveniverse.maven.mima.context.MavenSystemHome;
import eu.maveniverse.maven.mima.context.MavenUserHome;
Expand Down Expand Up @@ -99,11 +98,6 @@ public ToolboxCommandoImpl(Runtime runtime, Context context) {
new ToolboxResolverImpl(context.repositorySystem(), session, context.remoteRepositories());
}

@Override
public ToolboxCommando derive(ContextOverrides contextOverrides) {
return new ToolboxCommandoImpl(runtime, context.customize(contextOverrides));
}

@Override
public void close() {
context.close();
Expand Down

0 comments on commit 84e5684

Please sign in to comment.