001: /*
002: * Created on 01-Dec-2003
003: *
004: * To change the template for this generated file go to
005: * Window>Preferences>Java>Code Generation>Code and Comments
006: */
007: package net.xoetrope.builder.editor.ant;
008:
009: import java.io.File;
010: import java.io.FileInputStream;
011: import java.io.FileOutputStream;
012: import java.io.IOException;
013: import java.util.jar.JarEntry;
014: import java.util.jar.JarOutputStream;
015:
016: import net.xoetrope.builder.editor.ant.taskdefs.Jar;
017:
018: /**
019: * Class to generate a jar file for distribution. Includes class files, screens
020: * and resources. Uses Ant to build so the Ant jar file needs to be included in
021: * the classpath. The version of Ant used for initial development was 1.5.
022: */
023: public class JarBuilder {
024:
025: public JarBuilder() {
026: }
027:
028: /**
029: * Build the jar for the project including classes, screens and resources
030: * @param fileName The name of the jar file to be built
031: * @param rootDir the root directory of the project
032: */
033: public void buildJar(String fileName, String rootDir) {
034: createBuildDir(rootDir);
035: Jar jar = new Jar();
036: try {
037: JarOutputStream jos = new JarOutputStream(
038: new FileOutputStream(rootDir + "\\build\\"
039: + fileName));
040: jos.setLevel(0);
041: // jar.initZipOutputStream( jos );
042: zipDir(jos, jar, rootDir + "\\classes", "");
043: zipDir(jos, jar, rootDir + "\\pages", "");
044: zipDir(jos, jar, rootDir + "\\resources", "");
045: jos.flush();
046: jos.close();
047: } catch (Exception ex) {
048: ex.printStackTrace();
049: }
050: }
051:
052: /**
053: * recursive function which builds up the directory based on the new directory
054: * name found
055: * @param zos ZippedOutputStream for the jar file
056: * @param jar the Ant Jar object used for writing out the file streams
057: * @param rootDir the root directory for the files (the project root)
058: * @param dir the offset dir being build up (as in the case of packages)
059: */
060: public void zipDir(JarOutputStream jos, Jar jar, String rootDir,
061: String dir) {
062: try {
063: File folder = new File(rootDir + File.separatorChar);
064: File[] files = folder.listFiles();
065: for (int i = 0; i < files.length; i++) {
066: if (files[i].isDirectory()) {
067: zipDir(jos, jar, files[i].getAbsolutePath(), dir
068: + files[i].getName() + "\\");
069: } else {
070: putJarEntry(jos, files[i], dir + files[i].getName());
071: }
072: }
073: } catch (Exception ex) {
074: ex.printStackTrace();
075: }
076: }
077:
078: private void putJarEntry(JarOutputStream jos, File f, String dir) {
079: try {
080: JarEntry je = new JarEntry(dir);
081: jos.putNextEntry(je);
082: FileInputStream bais = new FileInputStream(f);
083:
084: byte b[] = new byte[1024];
085: byte fileBytes[];
086: int len = bais.read(b);
087: while (len > -1) {
088: fileBytes = new byte[len];
089: System.arraycopy(b, 0, fileBytes, 0, len);
090: jos.write(fileBytes, 0, len);
091: b = new byte[1024];
092: len = bais.read(b);
093: }
094: jos.closeEntry();
095: } catch (IOException ex) {
096: ex.printStackTrace();
097: }
098: }
099:
100: /**
101: * Create the build directory
102: * @param rootDir The project root directory
103: */
104: private void createBuildDir(String rootDir) {
105: File file = new File(rootDir + "\\build");
106: try {
107: file.mkdir();
108: } catch (Exception ex) {
109: ex.printStackTrace();
110: }
111: }
112: }
|