3131
3232package org .scijava .plugins .scripting .java ;
3333
34- import imagej .build .minimaven .BuildEnvironment ;
35- import imagej .build .minimaven .Coordinate ;
36- import imagej .build .minimaven .MavenProject ;
37-
3834import java .io .BufferedReader ;
3935import java .io .ByteArrayInputStream ;
4036import java .io .ByteArrayOutputStream ;
4339import java .io .FileWriter ;
4440import java .io .IOException ;
4541import java .io .PrintStream ;
42+ import java .io .PrintWriter ;
4643import java .io .Reader ;
4744import java .io .StringReader ;
4845import java .io .Writer ;
6966import org .scijava .command .Command ;
7067import org .scijava .command .CommandInfo ;
7168import org .scijava .command .CommandService ;
69+ import org .scijava .minimaven .BuildEnvironment ;
70+ import org .scijava .minimaven .Coordinate ;
71+ import org .scijava .minimaven .MavenProject ;
7272import org .scijava .plugin .Parameter ;
7373import org .scijava .plugin .Plugin ;
7474import org .scijava .plugin .PluginService ;
@@ -108,92 +108,187 @@ public Object eval(String script) throws ScriptException {
108108 @ SuppressWarnings ({ "unchecked" })
109109 @ Override
110110 public Object eval (Reader reader ) throws ScriptException {
111- File temporaryDirectory = null ;
111+ final String path = (String )get (FILENAME );
112+ File file = path == null ? null : new File (path );
113+
112114 final Writer writer = getContext ().getErrorWriter ();
113- final PrintStream err ;
114- if (writer == null ) {
115- err = null ;
116- } else {
117- err = new PrintStream (new LineOutputStream () {
118-
119- @ Override
120- public void println (final String line ) throws IOException {
121- writer .append (line ).append ('\n' );
115+ try {
116+ final Builder builder = new Builder (file , reader , writer );
117+ final MavenProject project = builder .project ;
118+ String mainClass = builder .mainClass ;
119+
120+ try {
121+ project .build (true );
122+ if (mainClass == null ) {
123+ mainClass = project .getMainClass ();
124+ if (mainClass == null ) {
125+ throw new ScriptException (
126+ "No main class found for file " + file );
127+ }
122128 }
123129
124- });
130+ // make class loader
131+ String [] paths = project .getClassPath (false ).split (
132+ File .pathSeparator );
133+ URL [] urls = new URL [paths .length ];
134+ for (int i = 0 ; i < urls .length ; i ++)
135+ urls [i ] = new URL ("file:" + paths [i ]
136+ + (paths [i ].endsWith (".jar" ) ? "" : "/" ));
137+ URLClassLoader classLoader = new URLClassLoader (urls ,
138+ getClass ().getClassLoader ());
139+
140+ // needed for sezpoz
141+ Thread .currentThread ().setContextClassLoader (classLoader );
142+
143+ // launch main class
144+ final Class <?> clazz = classLoader .loadClass (mainClass );
145+ if (Command .class .isAssignableFrom (clazz )) {
146+ final Plugin annotation = clazz .getAnnotation (Plugin .class );
147+ final CommandInfo info = new CommandInfo (mainClass ,
148+ annotation ) {
149+
150+ @ Override
151+ public Class <? extends Command > loadClass () {
152+ return (Class <? extends Command >) clazz ;
153+ }
154+ };
155+ pluginService .addPlugin (info );
156+ commandService .run (info , true );
157+ } else {
158+ Method main = clazz .getMethod ("main" ,
159+ new Class [] { String [].class });
160+ main .invoke (null , new Object [] { new String [0 ] });
161+ }
162+ } finally {
163+ builder .cleanup ();
164+ }
165+ } catch (Exception e ) {
166+ if (writer != null ) {
167+ final PrintWriter err = new PrintWriter (writer );
168+ e .printStackTrace (err );
169+ err .flush ();
170+ } else {
171+ if (e instanceof ScriptException )
172+ throw (ScriptException ) e ;
173+ throw new ScriptException (e );
174+ }
125175 }
176+ return null ;
177+ }
126178
179+ public void compile (final File file , final Writer errorWriter ) {
127180 try {
128- boolean verbose = "true" .equals (get ("verbose" ));
129- boolean debug = "true" .equals (get ("debug" ));
130- BuildEnvironment env = new BuildEnvironment (err , true , verbose , debug );
131- final MavenProject project ;
132- String mainClass = null ;
133-
134- final String path = (String )get (FILENAME );
135- File file = path == null ? null : new File (path );
136- if (file == null || !file .exists ()) try {
137- project = writeTemporaryProject (env , reader );
138- temporaryDirectory = project .getDirectory ();
139- mainClass = project .getMainClass ();
140- } catch (Exception e ) {
141- throw new ScriptException (e );
142- } else {
143- if (file .getName ().equals ("pom.xml" )) {
144- project = env .parse (file , null );
145- } else {
146- mainClass = getFullClassName (file );
147- project = getMavenProject (env , file , mainClass );
148- }
181+ final Builder builder = new Builder (file , null , errorWriter );
182+ try {
183+ builder .project .build ();
184+ } finally {
185+ builder .cleanup ();
149186 }
187+ } catch (Throwable t ) {
188+ printOrThrow (t , errorWriter );
189+ }
190+ }
150191
151- project .build (true );
152- if (mainClass == null ) {
153- mainClass = project .getMainClass ();
154- if (mainClass == null ) {
155- throw new ScriptException ("No main class found for file " + file );
192+ public void makeJar (final File file , final boolean includeSources , final File output , final Writer errorWriter ) {
193+ try {
194+ final Builder builder = new Builder (file , null , errorWriter );
195+ try {
196+ builder .project .build (true , true , includeSources );
197+ final File target = builder .project .getTarget ();
198+ if (output != null && !target .equals (output )) {
199+ BuildEnvironment .copyFile (target , output );
156200 }
201+ } finally {
202+ builder .cleanup ();
157203 }
204+ } catch (Throwable t ) {
205+ printOrThrow (t , errorWriter );
206+ }
207+ }
158208
159- // make class loader
160- String [] paths = project .getClassPath (false ).split (File .pathSeparator );
161- URL [] urls = new URL [paths .length ];
162- for (int i = 0 ; i < urls .length ; i ++)
163- urls [i ] = new URL ("file:" + paths [i ] + (paths [i ].endsWith (".jar" ) ? "" : "/" ));
164- URLClassLoader classLoader = new URLClassLoader (urls , getClass ().getClassLoader ());
165-
166- // needed for sezpoz
167- Thread .currentThread ().setContextClassLoader (classLoader );
209+ private void printOrThrow (Throwable t , Writer errorWriter ) {
210+ RuntimeException e = t instanceof RuntimeException ? (RuntimeException ) t
211+ : new RuntimeException (t );
212+ if (errorWriter == null ) {
213+ throw e ;
214+ }
215+ final PrintWriter err = new PrintWriter (errorWriter );
216+ e .printStackTrace (err );
217+ err .flush ();
218+ }
168219
169- // launch main class
170- final Class <?> clazz = classLoader .loadClass (mainClass );
171- if (Command .class .isAssignableFrom (clazz )) {
172- final Plugin annotation = clazz .getAnnotation (Plugin .class );
173- final CommandInfo info = new CommandInfo (mainClass , annotation ) {
220+ private class Builder {
221+ private final PrintStream err ;
222+ private final File temporaryDirectory ;
223+ private String mainClass ;
224+ private MavenProject project ;
225+
226+ /**
227+ * Constructs a wrapper around a possibly temporary project.
228+ *
229+ * @param file the {@code .java} file to build (or null, if {@code reader} is set).
230+ * @param reader provides the Java source if {@code file} is {@code null}
231+ * @param errorWriter where to write the error output.
232+ * @throws ScriptException
233+ * @throws IOException
234+ * @throws ParserConfigurationException
235+ * @throws SAXException
236+ * @throws TransformerConfigurationException
237+ * @throws TransformerException
238+ * @throws TransformerFactoryConfigurationError
239+ */
240+ private Builder (final File file , final Reader reader ,
241+ final Writer errorWriter ) throws ScriptException , IOException ,
242+ ParserConfigurationException , SAXException ,
243+ TransformerConfigurationException , TransformerException ,
244+ TransformerFactoryConfigurationError {
245+ if (errorWriter == null ) {
246+ err = null ;
247+ } else {
248+ err = new PrintStream (new LineOutputStream () {
174249
175250 @ Override
176- public Class <? extends Command > loadClass () {
177- return ( Class <? extends Command >) clazz ;
251+ public void println ( final String line ) throws IOException {
252+ errorWriter . append ( line ). append ( '\n' ) ;
178253 }
179- };
180- pluginService .addPlugin (info );
181- commandService .run (info , true );
182- } else {
183- Method main = clazz .getMethod ("main" , new Class [] { String [].class });
184- main .invoke (null , new Object [] { new String [0 ] });
254+
255+ });
185256 }
186- } catch (Exception e ) {
187- if (err != null ) err .close ();
188- if (e instanceof ScriptException ) throw (ScriptException )e ;
189- throw new ScriptException (e );
190- } finally {
191- if (err != null ) err .close ();
192- if (temporaryDirectory != null && !FileUtils .deleteRecursively (temporaryDirectory )) {
257+
258+ boolean verbose = "true" .equals (get ("verbose" ));
259+ boolean debug = "true" .equals (get ("debug" ));
260+ BuildEnvironment env = new BuildEnvironment (err , true , verbose ,
261+ debug );
262+
263+ if (file == null || !file .exists ())
264+ try {
265+ project = writeTemporaryProject (env , reader );
266+ temporaryDirectory = project .getDirectory ();
267+ mainClass = project .getMainClass ();
268+ } catch (Exception e ) {
269+ throw new ScriptException (e );
270+ }
271+ else {
272+ temporaryDirectory = null ;
273+ if (file .getName ().equals ("pom.xml" )) {
274+ project = env .parse (file , null );
275+ } else {
276+ mainClass = getFullClassName (file );
277+ project = getMavenProject (env , file , mainClass );
278+ }
279+ }
280+ }
281+
282+ private void cleanup () {
283+ if (err != null )
284+ err .close ();
285+ if (err != null )
286+ err .close ();
287+ if (temporaryDirectory != null
288+ && !FileUtils .deleteRecursively (temporaryDirectory )) {
193289 temporaryDirectory .deleteOnExit ();
194290 }
195291 }
196- return null ;
197292 }
198293
199294 private MavenProject getMavenProject (final BuildEnvironment env ,
@@ -208,7 +303,7 @@ private MavenProject getMavenProject(final BuildEnvironment env,
208303 path = path .substring (0 , path .length () - mainClass .length () - 5 );
209304 if (path .replace (File .separatorChar , '/' ).endsWith ("/src/main/java/" )) {
210305 path = path .substring (0 , path .length () - "src/main/java/" .length ());
211- final File pom = new File (path + "pom.xml" );
306+ final File pom = new File (path , "pom.xml" );
212307 if (pom .exists ()) return env .parse (pom , null );
213308 }
214309 final File rootDirectory = file .getParentFile ();
0 commit comments