Skip to content

Commit

Permalink
More mojos for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Mar 22, 2024
1 parent 8767703 commit 522045c
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.ResolutionRoot;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugins.annotations.Parameter;

/**
* Support class for "project aware" Mojos.
*/
public abstract class MPPluginMojoSupport extends MPMojoSupport {
/**
* The plugin key in the format {@code <groupId>:<artifactId>} to display tree for. If plugin is from "known"
* groupId (as configured in settings.xml) it may be in format of {@code :<artifactId>} and this mojo will find it.
* Finally, if plugin key is plain string like {@code "clean"}, this mojo will apply some heuristics to find it.
*/
@Parameter(property = "pluginKey", required = true)
private String pluginKey;

protected ResolutionRoot pluginAsResolutionRoot(ToolboxCommando toolboxCommando) throws Exception {
Plugin plugin = null;
if (pluginKey == null || pluginKey.trim().isEmpty()) {
throw new IllegalArgumentException("pluginKey must not be empty string");
}
if (pluginKey.startsWith(":")) {
for (String pluginGroup : settings.getPluginGroups()) {
plugin = mavenProject.getPlugin(pluginGroup + pluginKey);
if (plugin != null) {
break;
}
}
} else {
plugin = mavenProject.getPlugin(pluginKey);
if (plugin == null) {
for (Plugin p : mavenProject.getBuildPlugins()) {
if (p.getKey().contains(pluginKey)) {
plugin = p;
break;
}
}
}
}
if (plugin == null) {
throw new IllegalArgumentException("Plugin not found");
}
ResolutionRoot root =
toolboxCommando.loadGav(plugin.getGroupId() + ":" + plugin.getArtifactId() + ":" + plugin.getVersion());
if (!plugin.getDependencies().isEmpty()) {
root.getDependencies().addAll(toDependencies(plugin.getDependencies()));
}
return root;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.MPPluginMojoSupport;
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;

@Mojo(name = "plugin-classpath", threadSafe = true)
public class PluginClasspathMojo extends MPPluginMojoSupport {
/**
* 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.classpath(ResolutionScope.parse(scope), pluginAsResolutionRoot(toolboxCommando), output);
}
}
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.MPPluginMojoSupport;
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 = "plugin-list-repositories", threadSafe = true)
public final class PluginListRepositoriesMojo extends MPPluginMojoSupport {
/**
* 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), pluginAsResolutionRoot(toolboxCommando), output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,14 @@
*/
package eu.maveniverse.maven.toolbox.plugin.mp;

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

@Mojo(name = "plugin-tree", threadSafe = true)
public class PluginTreeMojo extends MPMojoSupport {
/**
* The plugin key in the format {@code <groupId>:<artifactId>} to display tree for. If plugin is from "known"
* groupId (as configured in settings.xml) it may be in format of {@code :<artifactId>} and this mojo will find it.
* Finally, if plugin key is plain string like {@code "clean"}, this mojo will apply some heuristics to find it.
*/
@Parameter(property = "pluginKey", required = true)
private String pluginKey;

public class PluginTreeMojo extends MPPluginMojoSupport {
/**
* The resolution scope to display, accepted values are "runtime", "compile", "test", etc.
*/
Expand All @@ -39,36 +29,7 @@ public class PluginTreeMojo extends MPMojoSupport {

@Override
protected void doExecute(ToolboxCommando toolboxCommando) throws Exception {
Plugin plugin = null;
if (pluginKey == null || pluginKey.trim().isEmpty()) {
throw new IllegalArgumentException("pluginKey must not be empty string");
}
if (pluginKey.startsWith(":")) {
for (String pluginGroup : settings.getPluginGroups()) {
plugin = mavenProject.getPlugin(pluginGroup + pluginKey);
if (plugin != null) {
break;
}
}
} else {
plugin = mavenProject.getPlugin(pluginKey);
if (plugin == null) {
for (Plugin p : mavenProject.getBuildPlugins()) {
if (p.getKey().contains(pluginKey)) {
plugin = p;
break;
}
}
}
}
if (plugin == null) {
throw new IllegalArgumentException("Plugin not found");
}
ResolutionRoot root =
toolboxCommando.loadGav(plugin.getGroupId() + ":" + plugin.getArtifactId() + ":" + plugin.getVersion());
if (!plugin.getDependencies().isEmpty()) {
root.getDependencies().addAll(toDependencies(plugin.getDependencies()));
}
toolboxCommando.tree(ResolutionScope.parse(scope), root, verboseTree, output);
toolboxCommando.tree(
ResolutionScope.parse(scope), pluginAsResolutionRoot(toolboxCommando), verboseTree, output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ public boolean visitLeave(DependencyNode node) {
}));
repositories.forEach((k, v) -> {
output.normal(k.toString());
output.verbose(" First introduced on {}", v == sentinel ? "root" : v);
output.normal(" First introduced on {}", v == sentinel ? "root" : v);
});
return !repositories.isEmpty();
}
Expand Down

0 comments on commit 522045c

Please sign in to comment.