public static void doCompilation( CompilerConfiguration configuration, CompilationUnit unit, String[] filenames, boolean lookupUnnamedFiles) throws Exception { File tmpDir = null; // if there are any joint compilation options set stubDir if not set try { if ((configuration.getJointCompilationOptions() != null) && !configuration.getJointCompilationOptions().containsKey("stubDir")) { tmpDir = DefaultGroovyStaticMethods.createTempDir(null, "groovy-generated-", "-java-source"); configuration.getJointCompilationOptions().put("stubDir", tmpDir); } FileSystemCompiler compiler = new FileSystemCompiler(configuration, unit); if (lookupUnnamedFiles) { for (String filename : filenames) { File file = new File(filename); if (file.isFile()) { URL url = file.getAbsoluteFile().getParentFile().toURI().toURL(); compiler.unit.getClassLoader().addURL(url); } } } else { compiler .unit .getClassLoader() .setResourceLoader( new GroovyResourceLoader() { public URL loadGroovySource(String filename) throws MalformedURLException { return null; } }); } compiler.compile(filenames); } finally { try { if (tmpDir != null) deleteRecursive(tmpDir); } catch (Throwable t) { System.err.println("error: could not delete temp files - " + tmpDir.getPath()); } } }
public FileSystemCompiler(CompilerConfiguration configuration, CompilationUnit cu) throws ConfigurationException { if (cu != null) { unit = cu; } else if (configuration.getJointCompilationOptions() != null) { unit = new JavaAwareCompilationUnit(configuration); } else { unit = new CompilationUnit(configuration); } }
protected CompilationUnit makeCompileUnit() { Map<String, Object> options = configuration.getJointCompilationOptions(); if (options != null) { if (keepStubs) { options.put("keepStubs", Boolean.TRUE); } if (stubDir != null) { options.put("stubDir", stubDir); } else { try { File tempStubDir = FileSystemCompiler.createTempDir(); temporaryFiles.add(tempStubDir); options.put("stubDir", tempStubDir); } catch (IOException ioe) { throw new BuildException(ioe); } } return new JavaAwareCompilationUnit(configuration, buildClassLoaderFor()); } else { return new CompilationUnit(configuration, null, buildClassLoaderFor()); } }
/** * Copy constructor. Use this if you have a mostly correct configuration for your compilation but * you want to make a some changes programmatically. An important reason to prefer this approach * is that your code will most likely be forward compatible with future changes to this * configuration API.<br> * An example of this copy constructor at work:<br> * * <pre> * // In all likelihood there is already a configuration in your code's context * // for you to copy, but for the sake of this example we'll use the global default. * CompilerConfiguration myConfiguration = new CompilerConfiguration(CompilerConfiguration.DEFAULT); * myConfiguration.setDebug(true); * </pre> * * @param configuration The configuration to copy. */ public CompilerConfiguration(CompilerConfiguration configuration) { setWarningLevel(configuration.getWarningLevel()); setOutput(configuration.getOutput()); setTargetDirectory(configuration.getTargetDirectory()); setClasspathList(new LinkedList(configuration.getClasspath())); setVerbose(configuration.getVerbose()); setDebug(configuration.getDebug()); setTolerance(configuration.getTolerance()); setScriptBaseClass(configuration.getScriptBaseClass()); setRecompileGroovySource(configuration.getRecompileGroovySource()); setMinimumRecompilationInterval(configuration.getMinimumRecompilationInterval()); setTargetBytecode(configuration.getTargetBytecode()); setDefaultScriptExtension(configuration.getDefaultScriptExtension()); setSourceEncoding(configuration.getSourceEncoding()); setOutput(configuration.getOutput()); setTargetDirectory(configuration.getTargetDirectory()); Map jointCompilationOptions = configuration.getJointCompilationOptions(); if (jointCompilationOptions != null) { jointCompilationOptions = new HashMap(jointCompilationOptions); } setJointCompilationOptions(jointCompilationOptions); setPluginFactory(configuration.getPluginFactory()); }