BuildGWT.java :  » GWT » reactive4java » hu » akarnokd » tools » Java Open Source

Java Open Source » GWT » reactive4java 
reactive4java » hu » akarnokd » tools » BuildGWT.java
/*
 * Copyright 2011 David Karnok
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package hu.akarnokd.tools;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Creates a GWT JAR fire from the src and gwt-override directories.
 * @author karnokd, 2011.02.07.
 */
public final class BuildGWT {

  /**
   * 
   */
  private BuildGWT() {
  }
  /**
   * Process the contents of the given directory.
   * @param baseDir the base directory
   * @param currentDir the current directory
   * @param zout the output stream
   * @param filter the optional file filter
   * @throws IOException on error
   */
  static void processDirectory(String baseDir, String currentDir, ZipOutputStream zout,
      FilenameFilter filter) throws IOException {
    File[] files = new File(currentDir).listFiles(new FileFilter() {
      @Override
      public boolean accept(File pathname) {
        return !pathname.isHidden();
      }
    });
    if (files != null) {
      for (File f : files) {
        if (f.isDirectory()) {
          processDirectory(baseDir, f.getPath(), zout, filter);
        } else {
          String fpath = f.getPath();
          String fpath2 = fpath.substring(baseDir.length());
          
          if (filter == null || filter.accept(f.getParentFile(), f.getName())) {
            System.out.printf("Adding %s as %s%n", fpath, fpath2);
            ZipEntry ze = new ZipEntry(fpath2.replace('\\', '/'));
            ze.setSize(f.length());
            ze.setTime(f.lastModified());
            zout.putNextEntry(ze);
            
            zout.write(IOUtils.load(f));
          }
        }
      }
    }
  }
  /**
   * Main program, no arguments.
   * @param args no arguments
   * @throws Exception ignored
   */
  public static void main(String[] args) throws Exception {
    ZipOutputStream zout = null;
    
    String baseProject = ".\\";
    String targetJar = "reactive4java-gwt";
    String version = "0.90";
    
    zout = new ZipOutputStream(new BufferedOutputStream(
        new FileOutputStream(baseProject + targetJar + "-" + version + ".jar"), 1024 * 1024));
    zout.setLevel(9);
    try {
      processDirectory(baseProject + ".\\src\\", baseProject + ".\\src", zout, new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
          String path = dir.getAbsolutePath().replace('\\', '/');
          return !name.equals(".svn") && path.contains("hu/akarnokd/reactive4java");
        }
      });
      processDirectory(baseProject + ".\\war\\WEB-INF\\classes\\", baseProject + ".\\war\\WEB-INF\\classes", zout, new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
          String path = dir.getAbsolutePath().replace('\\', '/');
          return name.endsWith(".class") && !name.equals(".svn") && path.contains("hu/akarnokd/reactive4java");
        }
      });
      processDirectory(baseProject + ".\\gwt-overrides\\", baseProject + ".\\gwt-overrides", zout, new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
          return !name.equals(".svn");
        }
      });
      
      addFile("META-INF/MANIFEST.MF", baseProject + "META-INF/MANIFEST.MF", zout);
      addFile("LICENSE.txt", baseProject + "LICENSE.txt", zout);
    } finally {
      zout.close();
    }

  }
  /**
   * Add file to the zip.
   * @param entryName the target entry name
   * @param file the source file name
   * @param zout the zip output stream
   * @throws IOException on error
   */
  static void addFile(String entryName, String file, ZipOutputStream zout) throws IOException {
    ZipEntry mf = new ZipEntry(entryName);
    File mfm = new File(file);
    mf.setSize(mfm.length());
    mf.setTime(mfm.lastModified());
    zout.putNextEntry(mf);
    zout.write(IOUtils.load(mfm));
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.