private static void compileAbilities(File javaDir, File classDir) { if (!javaDir.exists()) return; // Make ready a new list of files to compile. List<File> toCompile = getSourceFilesToCompile(javaDir, classDir); // No files to compile? if (toCompile.isEmpty()) { return; } // Notify the console. Messenger.info("Compiling abilities: " + fileListToString(toCompile)); // Get the compiler JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); // Generate some JavaFileObjects try { Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(toCompile); // Include the MobArena.jar on the classpath, and set the destination folder. List<String> options = Arrays.asList("-classpath", classpath, "-d", classDir.getPath()); // Set up the compilation task. JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, compilationUnits); // Call the task. task.call(); // And close the file manager. fileManager.close(); } catch (Exception e) { Messenger.severe("Compilation step failed..."); e.printStackTrace(); } }
/** * Load the custom abilities from the specified directory. * * @param classDir a directory of .class (and/or .java) files */ public static void loadCustomAbilities(File classDir) { if (abilities == null) abilities = new HashMap<String, Class<? extends Ability>>(); // Grab the source directory. File javaDir = new File(classDir, "src"); /* If the source directory exists, we need to verify that the system * has a java compiler before attempting anything. If not, we need to * skip the compiling step and just go straight to loading in the * existing class files. */ if (javaDir.exists()) { if (ToolProvider.getSystemJavaCompiler() != null) { compileAbilities(javaDir, classDir); } else { Messenger.warning( "Found plugins/MobArena/abilities/src/ folder, but no Java compiler. The source files will not be compiled!"); } } // Load all the custom abilities. loadClasses(classDir); }