Skip to content

Commit ef4c096

Browse files
committed
Initial import
0 parents  commit ef4c096

File tree

4 files changed

+361
-0
lines changed

4 files changed

+361
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
target
2+
*~
3+
*.iml
4+
*.ipr
5+
*.iws
6+
release.properties

README

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is my readme

pom.xml

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>org.twdata.maven</groupId>
4+
<artifactId>maven-cli-plugin</artifactId>
5+
<packaging>maven-plugin</packaging>
6+
<version>0.1-SNAPSHOT</version>
7+
<name>Maven CLI Plugin</name>
8+
<url>http://maven.apache.org</url>
9+
<prerequisites>
10+
<maven>2.0.6</maven>
11+
</prerequisites>
12+
<dependencies>
13+
<dependency>
14+
<groupId>org.twdata.maven</groupId>
15+
<artifactId>mojo-executor</artifactId>
16+
<version>0.1</version>
17+
</dependency>
18+
<dependency>
19+
<groupId>jline</groupId>
20+
<artifactId>jline</artifactId>
21+
<version>0.9.94</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>commons-io</groupId>
25+
<artifactId>commons-io</artifactId>
26+
<version>1.4</version>
27+
<scope>test</scope>
28+
</dependency>
29+
<dependency>
30+
<groupId>junit</groupId>
31+
<artifactId>junit</artifactId>
32+
<version>3.8.1</version>
33+
<scope>test</scope>
34+
</dependency>
35+
</dependencies>
36+
<build>
37+
<pluginManagement>
38+
<plugins>
39+
<plugin>
40+
<artifactId>maven-compiler-plugin</artifactId>
41+
<configuration>
42+
<source>1.5</source>
43+
<target>1.5</target>
44+
</configuration>
45+
</plugin>
46+
</plugins>
47+
</pluginManagement>
48+
<extensions>
49+
<extension>
50+
<groupId>org.jvnet.wagon-svn</groupId>
51+
<artifactId>wagon-svn</artifactId>
52+
<version>1.8</version>
53+
</extension>
54+
</extensions>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-release-plugin</artifactId>
59+
<dependencies>
60+
<dependency>
61+
<groupId>org.apache.maven.scm</groupId>
62+
<artifactId>maven-scm-provider-gitexe</artifactId>
63+
<version>1.1-db-1</version>
64+
</dependency>
65+
</dependencies>
66+
</plugin>
67+
<plugin>
68+
<groupId>org.apache.maven.plugins</groupId>
69+
<artifactId>maven-scm-plugin</artifactId>
70+
<dependencies>
71+
<dependency>
72+
<groupId>org.apache.maven.scm</groupId>
73+
<artifactId>maven-scm-provider-gitexe</artifactId>
74+
<version>1.1-db-1</version>
75+
</dependency>
76+
</dependencies>
77+
</plugin>
78+
<plugin>
79+
<groupId>org.apache.maven.plugins</groupId>
80+
<artifactId>maven-shade-plugin</artifactId>
81+
<executions>
82+
<execution>
83+
<phase>package</phase>
84+
<goals>
85+
<goal>shade</goal>
86+
</goals>
87+
<configuration>
88+
<createDependencyReducedPom>true</createDependencyReducedPom>
89+
<artifactSet>
90+
<includes>
91+
<include>org.twdata.maven:mojo-executor</include>
92+
</includes>
93+
</artifactSet>
94+
<relocations>
95+
<relocation>
96+
<pattern>org.twdata.maven.mojoexecutor</pattern>
97+
<shadedPattern>org.shaded.mojoexecutor</shadedPattern>
98+
</relocation>
99+
</relocations>
100+
<transformers>
101+
<transformer implementation="org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer" />
102+
</transformers>
103+
</configuration>
104+
</execution>
105+
</executions>
106+
</plugin>
107+
<plugin>
108+
<groupId>org.twdata.maven</groupId>
109+
<artifactId>maven-cli-plugin</artifactId>
110+
<version>0.1-SNAPSHOT</version>
111+
<configuration>
112+
<commands>
113+
<foo>clean compile jar</foo>
114+
</commands>
115+
</configuration>
116+
</plugin>
117+
118+
</plugins>
119+
</build>
120+
<repositories>
121+
<repository>
122+
<id>mojo-executor-repository</id>
123+
<name>Mojo Executor Repository for Maven</name>
124+
<url>http://mojo-executor.googlecode.com/svn/repo/</url>
125+
</repository>
126+
<repository>
127+
<id>don-asf-repository</id>
128+
<url>http://people.apache.org/~mrdon/repository/</url>
129+
</repository>
130+
</repositories>
131+
<distributionManagement>
132+
<repository>
133+
<uniqueVersion>false</uniqueVersion>
134+
<id>twdata-m2-repository</id>
135+
<url>svn:https://twdata-m2-repository.googlecode.com/svn</url>
136+
</repository>
137+
</distributionManagement>
138+
139+
<scm>
140+
<connection>scm:git:git://github.com/mrdon/maven-cli-plugin.git</connection>
141+
<url>scm:git:git://github.com/mrdon/maven-cli-plugin.git</url>
142+
</scm>
143+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
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

Comments
 (0)