summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/trunk/maven
diff options
context:
space:
mode:
authorantelder <antelder@13f79535-47bb-0310-9956-ffa450edef68>2011-03-31 10:26:02 +0000
committerantelder <antelder@13f79535-47bb-0310-9956-ffa450edef68>2011-03-31 10:26:02 +0000
commit4f89480664e31da931d3a7d5c418869ab93417c7 (patch)
tree37e2b9cd2b40aa2a853bd87273eb5944c4bca46e /sca-java-2.x/trunk/maven
parent4d0b0c7362fc48a1eb65929dea977eb167114f8a (diff)
Add support for invoking a main method instead of running the Shell. That enables using 'mvn tuscany:run' instead of 'mvn exec:java' to run a Tuscany sample that runs a regular Java SE class using Tuscany
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1087238 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/maven')
-rw-r--r--sca-java-2.x/trunk/maven/maven-tuscany-plugin/src/main/java/org/apache/tuscany/maven/plugin/TuscanyRunMojo.java45
1 files changed, 45 insertions, 0 deletions
diff --git a/sca-java-2.x/trunk/maven/maven-tuscany-plugin/src/main/java/org/apache/tuscany/maven/plugin/TuscanyRunMojo.java b/sca-java-2.x/trunk/maven/maven-tuscany-plugin/src/main/java/org/apache/tuscany/maven/plugin/TuscanyRunMojo.java
index d5cce76bc2..359762f71a 100644
--- a/sca-java-2.x/trunk/maven/maven-tuscany-plugin/src/main/java/org/apache/tuscany/maven/plugin/TuscanyRunMojo.java
+++ b/sca-java-2.x/trunk/maven/maven-tuscany-plugin/src/main/java/org/apache/tuscany/maven/plugin/TuscanyRunMojo.java
@@ -19,7 +19,10 @@
package org.apache.tuscany.maven.plugin;
import java.io.File;
+import java.lang.reflect.Method;
import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
@@ -98,7 +101,25 @@ public class TuscanyRunMojo extends AbstractMojo {
*/
private String[] contributions;
+ /**
+ * @parameter expression="${mainClass}"
+ */
+ private String mainClass;
+
+ /**
+ * @parameter expression="${arguments}"
+ */
+ private String[] arguments;
+
public void execute() throws MojoExecutionException, MojoFailureException {
+ if (mainClass != null) {
+ executeMainMethod();
+ } else {
+ executeShell();
+ }
+ }
+
+ private void executeShell() throws MojoExecutionException {
getLog().info("Starting Tuscany Shell...");
List<String> contributionList = new ArrayList<String>();
@@ -165,4 +186,28 @@ public class TuscanyRunMojo extends AbstractMojo {
throw new MojoExecutionException("", e);
}
}
+
+ public void executeMainMethod() throws MojoExecutionException, MojoFailureException {
+ getLog().info("Invoking " + mainClass + " class main method...");
+
+ if (arguments == null) {
+ arguments = new String[0];
+ }
+
+ try {
+ Method main = getMainClassLoader().loadClass(mainClass).getMethod("main", new Class[] {String[].class});
+ main.invoke(main, new Object[] {arguments});
+ } catch (NoSuchMethodException e) {
+ throw new MojoExecutionException("The specified mainClass doesn't contain a main method with appropriate signature", e);
+ } catch (Exception e) {
+ throw new MojoExecutionException("exception invoking main method", e);
+ }
+ }
+
+ private ClassLoader getMainClassLoader() throws MalformedURLException {
+ ClassLoader parent = Thread.currentThread().getContextClassLoader();
+ URL thisProject = new File( project.getBuild().getOutputDirectory()).toURI().toURL();
+ return new URLClassLoader(new URL[]{thisProject}, parent );
+ }
+
}