|
| 1 | +package org.twdata.maven.cli; |
| 2 | + |
| 3 | +import jline.ConsoleReader; |
| 4 | +import org.apache.maven.execution.MavenSession; |
| 5 | +import org.apache.maven.model.Plugin; |
| 6 | +import org.apache.maven.plugin.AbstractMojo; |
| 7 | +import org.apache.maven.plugin.MojoExecutionException; |
| 8 | +import org.apache.maven.plugin.PluginManager; |
| 9 | +import org.apache.maven.project.MavenProject; |
| 10 | +import static org.twdata.maven.mojoexecutor.MojoExecutor.*; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.OutputStreamWriter; |
| 14 | +import java.util.*; |
| 15 | + |
| 16 | +/** |
| 17 | + * Provides an interactive command line interface for Maven plugins, allowing users to execute plugins directly. |
| 18 | + * |
| 19 | + * @requiresDependencyResolution execute |
| 20 | + * @goal execute |
| 21 | + */ |
| 22 | +public class ExecuteCliMojo |
| 23 | + extends AbstractMojo |
| 24 | +{ |
| 25 | + private final Map<String,String> defaultAliases = Collections.unmodifiableMap(new HashMap<String,String>() {{ |
| 26 | + put("compile", "org.apache.maven.plugins:maven-compiler-plugin:compile"); |
| 27 | + put("testCompile", "org.apache.maven.plugins:maven-compiler-plugin:testCompile"); |
| 28 | + put("jar", "org.apache.maven.plugins:maven-jar-plugin:jar"); |
| 29 | + put("war", "org.apache.maven.plugins:maven-war-plugin:war"); |
| 30 | + put("resources", "org.apache.maven.plugins:maven-resources-plugin:resources"); |
| 31 | + put("install", "org.apache.maven.plugins:maven-install-plugin:install"); |
| 32 | + put("deploy", "org.apache.maven.plugins:maven-deploy-plugin:deploy"); |
| 33 | + put("test", "org.apache.maven.plugins:maven-surefire-plugin:test"); |
| 34 | + put("clean", "org.apache.maven.plugins:maven-clean-plugin:clean"); |
| 35 | + }}); |
| 36 | + |
| 37 | + /** |
| 38 | + * Command aliases. Commands should be in the form GROUP_ID:ARTIFACT_ID:GOAL |
| 39 | + * |
| 40 | + * @parameter |
| 41 | + */ |
| 42 | + private Map<String,String> commands; |
| 43 | + |
| 44 | + /** |
| 45 | + * The Maven Project Object |
| 46 | + * |
| 47 | + * @parameter expression="${project}" |
| 48 | + * @required |
| 49 | + * @readonly |
| 50 | + */ |
| 51 | + protected MavenProject project; |
| 52 | + |
| 53 | + /** |
| 54 | + * The Maven Session Object |
| 55 | + * |
| 56 | + * @parameter expression="${session}" |
| 57 | + * @required |
| 58 | + * @readonly |
| 59 | + */ |
| 60 | + protected MavenSession session; |
| 61 | + |
| 62 | + /** |
| 63 | + * The Maven PluginManager Object |
| 64 | + * |
| 65 | + * @component |
| 66 | + * @required |
| 67 | + */ |
| 68 | + protected PluginManager pluginManager; |
| 69 | + |
| 70 | + public void execute() |
| 71 | + throws MojoExecutionException |
| 72 | + { |
| 73 | + // build a list of command aliases |
| 74 | + Map<String,String> aliases = new HashMap<String,String>(); |
| 75 | + aliases.putAll(defaultAliases); |
| 76 | + aliases.putAll(commands); |
| 77 | + |
| 78 | + getLog().info("Waiting for commands"); |
| 79 | + try { |
| 80 | + ConsoleReader reader = new ConsoleReader(System.in, new OutputStreamWriter(System.out)); |
| 81 | + String line; |
| 82 | + |
| 83 | + while ((line = readCommand(reader)) != null) |
| 84 | + { |
| 85 | + if ("quit".equals(line)) { |
| 86 | + break; |
| 87 | + } else { |
| 88 | + List<MojoCall> calls = new ArrayList<MojoCall>(); |
| 89 | + try { |
| 90 | + parseCommand(line, aliases, calls); |
| 91 | + } catch (IllegalArgumentException ex) { |
| 92 | + getLog().error("Invalid command: "+line); |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + for (MojoCall call : calls) { |
| 97 | + getLog().info("Executing: "+call); |
| 98 | + long start = System.currentTimeMillis(); |
| 99 | + executeMojo( |
| 100 | + plugin( |
| 101 | + groupId(call.getGroupId()), |
| 102 | + artifactId(call.getArtifactId()), |
| 103 | + version(call.getVersion(project)) |
| 104 | + ), |
| 105 | + goal(call.getGoal()), |
| 106 | + configuration(), |
| 107 | + executionEnvironment(project, session, pluginManager) |
| 108 | + ); |
| 109 | + long now = System.currentTimeMillis(); |
| 110 | + getLog().info("Execution time: "+(now - start)+" ms"); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } catch (IOException e) { |
| 115 | + throw new MojoExecutionException("Unable to execute cli commands", e); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Recursively parses commands to resolve all aliases |
| 121 | + * @param text The text to evaluate |
| 122 | + * @param aliases The list of aliases available |
| 123 | + * @param commands The list of commands found so far |
| 124 | + */ |
| 125 | + private static void parseCommand(String text, Map<String,String> aliases, List<MojoCall> commands) { |
| 126 | + String[] tokens = text.split(" "); |
| 127 | + if (tokens.length > 1) { |
| 128 | + for (String token : tokens) { |
| 129 | + parseCommand(token, aliases, commands); |
| 130 | + } |
| 131 | + } else if (aliases.containsKey(text)) { |
| 132 | + parseCommand(aliases.get(text), aliases, commands); |
| 133 | + } else { |
| 134 | + String[] parsed = text.split(":"); |
| 135 | + if (parsed.length < 3) { |
| 136 | + throw new IllegalArgumentException("Invalid command: "+text); |
| 137 | + } |
| 138 | + commands.add(new MojoCall(parsed[0], parsed[1], parsed[2])); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private String readCommand(ConsoleReader reader) throws IOException { |
| 143 | + System.out.println(""); |
| 144 | + System.out.print("maven2> "); |
| 145 | + return reader.readLine(); |
| 146 | + } |
| 147 | + |
| 148 | + private static class MojoCall { |
| 149 | + private final String groupId; |
| 150 | + private final String artifactId; |
| 151 | + private final String goal; |
| 152 | + |
| 153 | + public MojoCall(String groupId, String artifactId, String goal) { |
| 154 | + this.groupId = groupId; |
| 155 | + this.artifactId = artifactId; |
| 156 | + this.goal = goal; |
| 157 | + } |
| 158 | + |
| 159 | + |
| 160 | + public String getGroupId() { |
| 161 | + return groupId; |
| 162 | + } |
| 163 | + |
| 164 | + public String getArtifactId() { |
| 165 | + return artifactId; |
| 166 | + } |
| 167 | + |
| 168 | + public String getGoal() { |
| 169 | + return goal; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Tries to determine what version of the plugin has been already configured for this project. If unknown, |
| 174 | + * "RELEASE" is used. |
| 175 | + * @param project The maven project |
| 176 | + * @return The discovered plugin version |
| 177 | + */ |
| 178 | + public String getVersion(MavenProject project) { |
| 179 | + String version = null; |
| 180 | + List<Plugin> plugins = project.getBuildPlugins(); |
| 181 | + for (Plugin plugin : plugins) { |
| 182 | + if (groupId.equals(plugin.getGroupId()) && artifactId.equals(plugin.getArtifactId())) { |
| 183 | + version = plugin.getVersion(); |
| 184 | + break; |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + if (version == null) { |
| 189 | + plugins = project.getPluginManagement().getPlugins(); |
| 190 | + for (Plugin plugin : plugins) { |
| 191 | + if (groupId.equals(plugin.getGroupId()) && artifactId.equals(plugin.getArtifactId())) { |
| 192 | + version = plugin.getVersion(); |
| 193 | + break; |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + if (version == null) { |
| 199 | + version = "RELEASE"; |
| 200 | + } |
| 201 | + return version; |
| 202 | + } |
| 203 | + |
| 204 | + public String toString() { |
| 205 | + StringBuilder sb = new StringBuilder(); |
| 206 | + sb.append(groupId).append(":").append(artifactId); |
| 207 | + sb.append(" [").append(goal).append("]"); |
| 208 | + return sb.toString(); |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments