@@ -196,15 +196,7 @@ public Class<?> compile(Reader reader) throws ScriptException {
196196 }
197197 }
198198
199- // make class loader
200- String [] paths = project .getClassPath (false ).split (
201- File .pathSeparator );
202- URL [] urls = new URL [paths .length ];
203- for (int i = 0 ; i < urls .length ; i ++)
204- urls [i ] = new URL ("file:" + paths [i ]
205- + (paths [i ].endsWith (".jar" ) ? "" : "/" ));
206- URLClassLoader classLoader = new URLClassLoader (urls ,
207- Thread .currentThread ().getContextClassLoader ());
199+ ClassLoader classLoader = makeClassLoader (project );
208200
209201 // needed for annotation processing
210202 Thread .currentThread ().setContextClassLoader (classLoader );
@@ -231,20 +223,53 @@ public Class<?> compile(Reader reader) throws ScriptException {
231223 /**
232224 * Compiles the specified {@code .java} file.
233225 *
234- * @param file the source code
235- * @param errorWriter where to write the errors
226+ * @param file
227+ * the source code
236228 */
237- public void compile (final File file , final Writer errorWriter ) {
229+ public Class <?> compile (final File file ) {
230+ final Writer errorWriter = getContext ().getErrorWriter ();
238231 try {
239232 final Builder builder = new Builder (file , null , errorWriter );
233+ final MavenProject project = builder .project ;
240234 try {
241- builder .project .build ();
235+ project .build ();
236+
237+ final String mainClass = project .getMainClass ();
238+ if (mainClass == null ) {
239+ throw new ScriptException ("No main class found for file "
240+ + file );
241+ }
242+
243+ // load main class
244+ return makeClassLoader (project ).loadClass (mainClass );
242245 } finally {
243246 builder .cleanup ();
244247 }
245248 } catch (Throwable t ) {
246249 printOrThrow (t , errorWriter );
247250 }
251+ return null ;
252+ }
253+
254+ /**
255+ * Create ClassLoader for maven project.
256+ *
257+ * @param project
258+ * maven project to create the ClassLoader for.
259+ * @return the newly created ClassLoader.
260+ * @throws IOException
261+ * @throws ParserConfigurationException
262+ * @throws SAXException
263+ */
264+ private ClassLoader makeClassLoader (MavenProject project )
265+ throws IOException , ParserConfigurationException , SAXException {
266+ String [] paths = project .getClassPath (false ).split (File .pathSeparator );
267+ URL [] urls = new URL [paths .length ];
268+ for (int i = 0 ; i < urls .length ; i ++)
269+ urls [i ] = new URL ("file:" + paths [i ]
270+ + (paths [i ].endsWith (".jar" ) ? "" : "/" ));
271+ return new URLClassLoader (urls , Thread .currentThread ()
272+ .getContextClassLoader ());
248273 }
249274
250275 /**
0 commit comments