Compiler.java revision 3062:15bdc18525ff
1/* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24import java.io.BufferedWriter; 25import java.io.IOException; 26import java.io.UncheckedIOException; 27import java.nio.file.Files; 28import java.nio.file.Path; 29import java.nio.file.Paths; 30import java.util.jar.Attributes; 31import java.util.jar.Manifest; 32 33public class Compiler { 34 35 private final ToolBox tb = new ToolBox(); 36 37 public Path getClassDir() { 38 String classes = ToolBox.testClasses; 39 if (classes == null) { 40 return Paths.get("build"); 41 } else { 42 return Paths.get(classes); 43 } 44 } 45 46 public Path getPath(String path) { 47 return getPath(Paths.get(path)); 48 } 49 50 public Path getPath(Path path) { 51 return getClassDir().resolve(path); 52 } 53 54 public void compile(String...sources) { 55 compile(Paths.get("."), sources); 56 } 57 58 public void compile(Path directory, String...sources) { 59 Path classDir = getClassDir(); 60 tb.new JavacTask() 61 .options("-d", classDir.resolve(directory).toString()) 62 .sources(sources) 63 .run(); 64 } 65 66 public void jar(String jarName, String...files) { 67 jar(Paths.get("."), jarName, files); 68 } 69 70 public void jar(Path directory, String jarName, String...files) { 71 Manifest manifest = new Manifest(); 72 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); 73 Path classDirPath = getClassDir(); 74 Path baseDir = classDirPath.resolve(directory); 75 Path jarPath = baseDir.resolve(jarName); 76 tb.new JarTask(jarPath.toString()) 77 .manifest(manifest) 78 .baseDir(baseDir.toString()) 79 .files(files).run(); 80 } 81 82 public void writeToFile(Path path, String...sources) { 83 try { 84 if (path.getParent() != null) { 85 Files.createDirectories(path.getParent()); 86 } 87 } catch (IOException e) { 88 throw new UncheckedIOException(e); 89 } 90 try (BufferedWriter writer = Files.newBufferedWriter(path)) { 91 for (String source : sources) { 92 writer.append(source); 93 writer.append('\n'); 94 } 95 } catch (IOException e) { 96 throw new UncheckedIOException(e); 97 } 98 } 99} 100