@@ -107,92 +107,140 @@ public Object eval(String script) throws ScriptException {
107107 @ SuppressWarnings ({ "unchecked" })
108108 @ Override
109109 public Object eval (Reader reader ) throws ScriptException {
110- File temporaryDirectory = null ;
111- final Writer writer = getContext ().getErrorWriter ();
112- final PrintStream err ;
113- if (writer == null ) {
114- err = null ;
115- } else {
116- err = new PrintStream (new LineOutputStream () {
117-
118- @ Override
119- public void println (final String line ) throws IOException {
120- writer .append (line ).append ('\n' );
110+ final String path = (String )get (FILENAME );
111+ File file = path == null ? null : new File (path );
112+
113+ try {
114+ final Writer writer = getContext ().getErrorWriter ();
115+ final Builder builder = new Builder (file , reader , writer );
116+ final MavenProject project = builder .project ;
117+ String mainClass = builder .mainClass ;
118+
119+ try {
120+ project .build (true );
121+ if (mainClass == null ) {
122+ mainClass = project .getMainClass ();
123+ if (mainClass == null ) {
124+ throw new ScriptException (
125+ "No main class found for file " + file );
126+ }
121127 }
122128
123- });
129+ // make class loader
130+ String [] paths = project .getClassPath (false ).split (
131+ File .pathSeparator );
132+ URL [] urls = new URL [paths .length ];
133+ for (int i = 0 ; i < urls .length ; i ++)
134+ urls [i ] = new URL ("file:" + paths [i ]
135+ + (paths [i ].endsWith (".jar" ) ? "" : "/" ));
136+ URLClassLoader classLoader = new URLClassLoader (urls ,
137+ getClass ().getClassLoader ());
138+
139+ // needed for sezpoz
140+ Thread .currentThread ().setContextClassLoader (classLoader );
141+
142+ // launch main class
143+ final Class <?> clazz = classLoader .loadClass (mainClass );
144+ if (Command .class .isAssignableFrom (clazz )) {
145+ final Plugin annotation = clazz .getAnnotation (Plugin .class );
146+ final CommandInfo info = new CommandInfo (mainClass ,
147+ annotation ) {
148+
149+ @ Override
150+ public Class <? extends Command > loadClass () {
151+ return (Class <? extends Command >) clazz ;
152+ }
153+ };
154+ pluginService .addPlugin (info );
155+ commandService .run (info , true );
156+ } else {
157+ Method main = clazz .getMethod ("main" ,
158+ new Class [] { String [].class });
159+ main .invoke (null , new Object [] { new String [0 ] });
160+ }
161+ } finally {
162+ builder .cleanup ();
163+ }
164+ } catch (Exception e ) {
165+ if (e instanceof ScriptException )
166+ throw (ScriptException ) e ;
167+ throw new ScriptException (e );
124168 }
169+ return null ;
170+ }
171+
172+ private class Builder {
173+ private final PrintStream err ;
174+ private final File temporaryDirectory ;
175+ private String mainClass ;
176+ private MavenProject project ;
177+
178+ /**
179+ * Constructs a wrapper around a possibly temporary project.
180+ *
181+ * @param file the {@code .java} file to build (or null, if {@code reader} is set).
182+ * @param reader provides the Java source if {@code file} is {@code null}
183+ * @param errorWriter where to write the error output.
184+ * @throws ScriptException
185+ * @throws IOException
186+ * @throws ParserConfigurationException
187+ * @throws SAXException
188+ * @throws TransformerConfigurationException
189+ * @throws TransformerException
190+ * @throws TransformerFactoryConfigurationError
191+ */
192+ private Builder (final File file , final Reader reader ,
193+ final Writer errorWriter ) throws ScriptException , IOException ,
194+ ParserConfigurationException , SAXException ,
195+ TransformerConfigurationException , TransformerException ,
196+ TransformerFactoryConfigurationError {
197+ if (errorWriter == null ) {
198+ err = null ;
199+ } else {
200+ err = new PrintStream (new LineOutputStream () {
201+
202+ @ Override
203+ public void println (final String line ) throws IOException {
204+ errorWriter .append (line ).append ('\n' );
205+ }
206+
207+ });
208+ }
125209
126- try {
127210 boolean verbose = "true" .equals (get ("verbose" ));
128211 boolean debug = "true" .equals (get ("debug" ));
129- BuildEnvironment env = new BuildEnvironment (err , true , verbose , debug );
130- final MavenProject project ;
131- String mainClass = null ;
132-
133- final String path = ( String ) get ( FILENAME );
134- File file = path == null ? null : new File ( path );
135- if ( file == null || ! file . exists ()) try {
136- project = writeTemporaryProject ( env , reader );
137- temporaryDirectory = project . getDirectory ();
138- mainClass = project . getMainClass ( );
139- } catch ( Exception e ) {
140- throw new ScriptException ( e );
141- } else {
212+ BuildEnvironment env = new BuildEnvironment (err , true , verbose ,
213+ debug ) ;
214+
215+ if ( file == null || ! file . exists ())
216+ try {
217+ project = writeTemporaryProject ( env , reader );
218+ temporaryDirectory = project . getDirectory ();
219+ mainClass = project . getMainClass ( );
220+ } catch ( Exception e ) {
221+ throw new ScriptException ( e );
222+ }
223+ else {
224+ temporaryDirectory = null ;
142225 if (file .getName ().equals ("pom.xml" )) {
143226 project = env .parse (file , null );
144227 } else {
145228 mainClass = getFullClassName (file );
146229 project = getMavenProject (env , file , mainClass );
147230 }
148231 }
232+ }
149233
150- project .build (true );
151- if (mainClass == null ) {
152- mainClass = project .getMainClass ();
153- if (mainClass == null ) {
154- throw new ScriptException ("No main class found for file " + file );
155- }
156- }
157-
158- // make class loader
159- String [] paths = project .getClassPath (false ).split (File .pathSeparator );
160- URL [] urls = new URL [paths .length ];
161- for (int i = 0 ; i < urls .length ; i ++)
162- urls [i ] = new URL ("file:" + paths [i ] + (paths [i ].endsWith (".jar" ) ? "" : "/" ));
163- URLClassLoader classLoader = new URLClassLoader (urls , getClass ().getClassLoader ());
164-
165- // needed for sezpoz
166- Thread .currentThread ().setContextClassLoader (classLoader );
167-
168- // launch main class
169- final Class <?> clazz = classLoader .loadClass (mainClass );
170- if (Command .class .isAssignableFrom (clazz )) {
171- final Plugin annotation = clazz .getAnnotation (Plugin .class );
172- final CommandInfo info = new CommandInfo (mainClass , annotation ) {
173-
174- @ Override
175- public Class <? extends Command > loadClass () {
176- return (Class <? extends Command >) clazz ;
177- }
178- };
179- pluginService .addPlugin (info );
180- commandService .run (info , true );
181- } else {
182- Method main = clazz .getMethod ("main" , new Class [] { String [].class });
183- main .invoke (null , new Object [] { new String [0 ] });
184- }
185- } catch (Exception e ) {
186- if (err != null ) err .close ();
187- if (e instanceof ScriptException ) throw (ScriptException )e ;
188- throw new ScriptException (e );
189- } finally {
190- if (err != null ) err .close ();
191- if (temporaryDirectory != null && !FileUtils .deleteRecursively (temporaryDirectory )) {
234+ private void cleanup () {
235+ if (err != null )
236+ err .close ();
237+ if (err != null )
238+ err .close ();
239+ if (temporaryDirectory != null
240+ && !FileUtils .deleteRecursively (temporaryDirectory )) {
192241 temporaryDirectory .deleteOnExit ();
193242 }
194243 }
195- return null ;
196244 }
197245
198246 private MavenProject getMavenProject (final BuildEnvironment env ,
0 commit comments