Closed GoogleCodeExporter closed 9 years ago
Here is an example of a better algorithm. import java.io.File; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.MalformedURLException; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Scanner; import java.util.Vector; import java.util.jar.JarFile; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.project.MavenProject; import org.codehaus.classworlds.ClassRealm; import org.codehaus.classworlds.ClassWorld; import org.codehaus.classworlds.DuplicateRealmException; /** * @goal runjar * @phase package * @requiresDependencyResolution runtime * @requiresProject */ public class RunJarMojo extends AbstractMojo { /** * @parameter expression="${project}" * @required * @readonly */ private MavenProject project; public MavenProject getProject() { return project; } @SuppressWarnings("unchecked") public void execute() throws MojoExecutionException, MojoFailureException { File outputDirectory = new File(project.getBuild().getOutputDirectory()); try { ClassWorld world = new ClassWorld(); ClassRealm realm = world.newRealm("br.com.dextra.lib.plugin.run", Thread.currentThread().getContextClassLoader()); ClassRealm runRealm = realm.createChildRealm("runenv"); runRealm.addConstituent(outputDirectory.toURI().toURL()); Iterator i = getProject().getArtifacts().iterator(); while (i.hasNext()) { Artifact artifact = (Artifact) i.next(); runRealm.addConstituent(artifact.getFile().toURI().toURL()); } Thread.currentThread().setContextClassLoader( runRealm.getClassLoader()); } catch (MalformedURLException e) { throw new MojoExecutionException( "Unable to load output directory and dependencies URLs.", e); } catch (DuplicateRealmException e) { throw new MojoExecutionException( "Unable to create class loding context.", e); } String jarFileName = getProject().getBuild().getDirectory() + "/" + getProject().getBuild().getFinalName() + ".jar"; try { JarFile jar = new JarFile(jarFileName); String mainClass = jar.getManifest().getMainAttributes().getValue( "Main-Class"); Class main = Thread.currentThread().getContextClassLoader() .loadClass(mainClass); final Method mainMethod = main.getMethod("main", String[].class); final String[] parameters = getParameters(); Thread mainThread = new Thread(new Runnable() { public void run() { try { mainMethod.invoke(null, new Object[] { parameters }); } catch (IllegalArgumentException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } } }); mainThread.start(); getLog().info("Executing class " + main + "."); mainThread.join(); } catch (IOException e) { throw new RuntimeException(e); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (IllegalArgumentException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } } private String[] getParameters() { System.out .println("Inform the main method parameters and input Ctrl+D to execute the main method."); Vector<String> parameters = new Vector<String>(); String param; while ((param = readParameter()) != null) { parameters.add(param); } return parameters.toArray(new String[parameters.size()]); } private String readParameter() { System.out.print("Input parameter: "); try { String param = new Scanner(System.in).next(); return param; } catch (NoSuchElementException e) { System.out.println(); return null; } } }
Original issue reported on code.google.com by rafaferry@gmail.com on 7 Oct 2008 at 6:26
rafaferry@gmail.com
Original comment by rafaferry@gmail.com on 7 Oct 2008 at 6:26
Original comment by rafaferry@gmail.com on 9 Aug 2009 at 2:31
Original issue reported on code.google.com by
rafaferry@gmail.com
on 7 Oct 2008 at 6:26