Skip to content

Commit

Permalink
More mojos
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Mar 22, 2024
1 parent b17aceb commit 8767703
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import picocli.CommandLine;

/**
* Resolves transitively a given GAV and outputs classpath path.
* Resolves transitively a given GAV and outputs used repositories.
*/
@CommandLine.Command(name = "listRepositories", description = "Lists repositories used to resolve given GAV")
public final class ListRepositories extends ResolverCommandSupport {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Settings;
import org.eclipse.aether.artifact.ArtifactTypeRegistry;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.graph.Dependency;
Expand All @@ -25,9 +23,6 @@
* Support class for "project aware" Mojos.
*/
public abstract class MPMojoSupport extends MojoSupport {
@Parameter(defaultValue = "${settings}", readonly = true, required = true)
protected Settings settings;

@Component
protected MavenProject mavenProject;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.settings.Settings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -27,6 +29,9 @@ public abstract class MojoSupport extends AbstractMojo {
protected final Logger logger = LoggerFactory.getLogger(getClass());
protected final Output output = new Slf4jOutput(logger);

@Parameter(defaultValue = "${settings}", readonly = true, required = true)
protected Settings settings;

@Override
public final void execute() throws MojoExecutionException, MojoFailureException {
Runtime runtime = Runtimes.INSTANCE.getRuntime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@

@Mojo(name = "gav-classpath", requiresProject = false, threadSafe = true)
public class GavClasspathMojo extends GavMojoSupport {
/**
* The resolution scope to display, accepted values are "runtime", "compile", "test", etc.
*/
@Parameter(property = "scope", defaultValue = "runtime", required = true)
private String scope;

/**
* The artifact coordinates in the format {@code <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>}
* to display tree for.
*/
@Parameter(property = "gav", required = true)
private String gav;

/**
* The resolution scope to display, accepted values are "runtime", "compile", "test", etc.
*/
@Parameter(property = "scope", defaultValue = "runtime", required = true)
private String scope;

/**
* Apply BOMs, if needed. Comma separated GAVs.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.ToolboxCommando;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

/**
* List available plugins.
*/
@Mojo(name = "gav-list-available-plugins", requiresProject = false, threadSafe = true)
public class GavListAvailablePluginsMojo extends GavMojoSupport {
/**
* The groupIds to list.
*/
@Parameter(property = "groupIds")
private String groupIds;

@Override
protected void doExecute(ToolboxCommando toolboxCommando) throws Exception {
if (groupIds == null || groupIds.trim().isEmpty()) {
groupIds = String.join(",", settings.getPluginGroups());
}
toolboxCommando.listAvailablePlugins(csv(groupIds), output);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.ResolutionScope;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

/**
* Resolves transitively a given GAV and outputs used repositories.
*/
@Mojo(name = "gav-list-repositories", threadSafe = true)
public final class GavListRepositoriesMojo extends GavMojoSupport {
/**
* The resolution scope to display, accepted values are "runtime", "compile", "test", etc.
*/
@Parameter(property = "scope", defaultValue = "runtime", required = true)
private String scope;

/**
* The artifact coordinates in the format {@code <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>}
* to display tree for.
*/
@Parameter(property = "gav", required = true)
private String gav;

/**
* Apply BOMs, if needed. Comma separated GAVs.
*/
@Parameter(property = "boms", defaultValue = "")
private String boms;

@Override
protected void doExecute(ToolboxCommando toolboxCommando) throws Exception {
toolboxCommando.listRepositories(ResolutionScope.parse(scope), toolboxCommando.loadGav(gav, csv(boms)), output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@

@Mojo(name = "gav-tree", requiresProject = false, threadSafe = true)
public class GavTreeMojo extends GavMojoSupport {
/**
* The resolution scope to display, accepted values are "runtime", "compile", "test", etc.
*/
@Parameter(property = "scope", defaultValue = "runtime", required = true)
private String scope;

/**
* The artifact coordinates in the format {@code <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>}
* to display tree for.
*/
@Parameter(property = "gav", required = true)
private String gav;

/**
* The resolution scope to display, accepted values are "runtime", "compile", "test", etc.
*/
@Parameter(property = "scope", defaultValue = "runtime", required = true)
private String scope;

/**
* Set it {@code true} for verbose tree.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.mp;

import eu.maveniverse.maven.toolbox.plugin.MPMojoSupport;
import eu.maveniverse.maven.toolbox.shared.ResolutionScope;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

/**
* Resolves transitively a given GAV and outputs used repositories.
*/
@Mojo(name = "list-repositories", threadSafe = true)
public final class ListRepositoriesMojo extends MPMojoSupport {
/**
* The resolution scope to display, accepted values are "runtime", "compile", "test", etc.
*/
@Parameter(property = "scope", defaultValue = "runtime", required = true)
private String scope;

@Override
protected void doExecute(ToolboxCommando toolboxCommando) throws Exception {
toolboxCommando.listRepositories(ResolutionScope.parse(scope), projectAsResolutionRoot(), output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.security.NoSuchAlgorithmException;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -270,20 +271,29 @@ public boolean listRepositories(ResolutionScope resolutionScope, ResolutionRoot
output.verbose("Collecting graph of: {}", resolutionRoot.getArtifact());
CollectResult collectResult = toolboxResolver.collect(
resolutionScope, root.getArtifact(), root.getDependencies(), root.getManagedDependencies(), false);
HashSet<RemoteRepository> repositories = new HashSet<>();
final HashMap<RemoteRepository, Artifact> repositories = new HashMap<>();
Artifact sentinel = new DefaultArtifact("sentinel:sentinel:sentinel");
context.remoteRepositories().forEach(r -> repositories.put(r, sentinel));
final ArrayDeque<Artifact> path = new ArrayDeque<>();
collectResult.getRoot().accept(new TreeDependencyVisitor(new DependencyVisitor() {
@Override
public boolean visitEnter(DependencyNode node) {
repositories.addAll(node.getRepositories());
Artifact parent = path.peek() == null ? sentinel : path.peek();
node.getRepositories().forEach(r -> repositories.putIfAbsent(r, parent));
path.push(node.getArtifact());
return true;
}

@Override
public boolean visitLeave(DependencyNode node) {
path.pop();
return true;
}
}));
repositories.forEach(r -> output.normal(r.toString()));
repositories.forEach((k, v) -> {
output.normal(k.toString());
output.verbose(" First introduced on {}", v == sentinel ? "root" : v);
});
return !repositories.isEmpty();
}

Expand Down

0 comments on commit 8767703

Please sign in to comment.