"Milos Kleint" <[EMAIL PROTECTED]> writes:

> the second option is true now unfortunately. You need to copy your
> cluster(s) into a pre-packages netbeans platform installation

I really needed the first option, so I've written a `make-zip' task
which grabs clusters, launchers, and config-files from
${netbeans.installation} and zips them all up into
${project.build.directory}/dist/${zipName}.zip.

The contents of the zipfile will run standalone---at least as far as my
limited testing has ascertained.

Jeremy


package org.codehaus.mojo.nbm;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Copy;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.selectors.FileSelector;


/**
 * Package a branded application into a zip archive along with required
 * netbeans modules and launchers.
 * @author <a href="mailto:[EMAIL PROTECTED]">Jeremy Hughes</a>
 * @goal make-zip
 * @aggregator
 * @requiresDependencyResolution runtime
 * 
 */
public class ApplicationZipMojo extends AbstractMojo {

	/**
	 * Name of application zip.
	 * @parameter default-value="app"
	 * @required
	 */
	protected String zipName;
	
	/**
	 * Directory where zip package should be built.
	 * @parameter default-value="${project.build.directory}/dist"
	 * @required
	 */
	protected String distDir;
	
	/**
	 * Directory where the module(s)' netbeans cluster(s) are located.
	 * Is related to nbm:cluster goal.
	 * @parameter default-value="${project.build.directory}/netbeans_clusters"
	 * @required
	 */
	protected String clusterBuildDir;
	
	/**
	 * Directory where the NetBeans platform/IDE installation is.
	 * Denotes the root directory of netbeans installation.
	 * @parameter expression="${netbeans.installation}"
	 * @required
	 */
	protected String netbeansInstallation;
	
	/**
	 * The branding token for the application based on NetBeans platform.
	 * @parameter expression="${netbeans.branding.token}"
	 * @required
	 */
	protected String brandingToken;
	
	/**
	 * List of enabled clusters. At least platform cluster needs to be
	 * included.
	 * @parameter
	 * @required
	 */
	protected List/*<String>*/ enabledClusters;
	
	/**
	 * 
	 */
	private Map matchers = new HashMap();
	
        /**
         * 
         */
        private Project antProject;
        
	/**
	 * 
	 * @throws org.apache.maven.plugin.MojoExecutionException
	 * @throws org.apache.maven.plugin.MojoFailureException
	 */
	public void execute() throws MojoExecutionException, MojoFailureException {
		antProject = new Project();
		antProject.init();
                        
		File clusterRoot = new File(clusterBuildDir);
		if (!clusterRoot.exists())
			throw new MojoExecutionException("There are no additional clusters in " + clusterBuildDir);
		
		// Make sure the final cluster list is numbered.
		enabledClusters = (enabledClusters == null) ? new ArrayList() : enabledClusters;
		Iterator it = enabledClusters.iterator();
		while (it.hasNext()) {
			String clus = (String) it.next();
			matchers.put(clus, Pattern.compile(clus + "(\\d)*"));
		}
		
		List nbClusters = findClusters(new File(netbeansInstallation).listFiles());
		List appClusters = findClusters(clusterRoot.listFiles());
		
		//if (platform == null)
		//	throw new MojoExecutionException("Cannot find platform* cluster within NetBeans installation at " + netbeansInstallation);
		
		if (matchers.size() > 0) {
			getLog().error("Cannot find following clusters, ignoring:");
			it = matchers.keySet().iterator();
			while (it.hasNext())
				getLog().error("      " + it.next());
		}
		
		System.out.println("CLUSTERS:" + enabledClusters.size());
		String appDir = distDir + File.separator + zipName;
		new File(appDir).mkdirs();
		String etcDir = appDir + File.separator + "etc";
		String binDir = appDir + File.separator + "bin";
		new File(etcDir).mkdir();
		new File(binDir).mkdir();
		
		Writer clusterFile = null;
		String harnessDir = netbeansInstallation + File.separator + "harness" + File.separator;
		String nbEtcDir = harnessDir + "etc" + File.separator;
		String nbBinDir = harnessDir + "launchers" + File.separator;
		try {
			copyFile(
					nbEtcDir + "app.conf",
					etcDir + File.separator + zipName + ".conf");
			copyFile(
					nbEtcDir + "applicationIcon.icns",
					etcDir + File.separator + "applicationIcon.icns");
			copyFile(
					nbEtcDir + "Info.plist",
					etcDir + File.separator + "Info.plist");
			copyFile(
					nbBinDir + "app.sh",
					binDir + File.separator + zipName);
			Runtime.getRuntime().exec("chmod 755 " + binDir + File.separator + zipName);
			copyFile(
					nbBinDir + "app.exe",
					binDir + File.separator + zipName + ".exe");
			copyFile(
					nbBinDir + "app_w.exe",
					binDir + File.separator + zipName + "_w.exe");
			
			clusterFile = new FileWriter(etcDir + File.separator + zipName + ".clusters");
			for (int i = 0; i < nbClusters.size(); i++)
				clusterFile.write(((File) nbClusters.get(i)).getName() + "\n");
			for (int i = 0; i < appClusters.size(); i++)
				clusterFile.write(((File) appClusters.get(i)).getName() + "\n");
			clusterFile.close();
			
		} catch (FileNotFoundException e) {
			throw new MojoExecutionException("Config file does not exist.", e);
		} catch (IOException e) {
			throw new MojoExecutionException("Error creating config files.", e);
		}
		
		try {
			for (int i = 0; i < nbClusters.size(); i++) {
				File clusterDir = (File) nbClusters.get(i);
				String name = clusterDir.getName();
				copyDirectory(clusterDir, new File(appDir + File.separator + name));
			}
			for (int i = 0; i < appClusters.size(); i++) {
				File clusterDir = (File) appClusters.get(i);
				String name = clusterDir.getName();
				copyDirectory(clusterDir, new File(appDir + File.separator + name));
			}
		} catch (IOException e) {
			throw new MojoExecutionException("Copying clusters failed.", e);
		}
		
		try {
			Zip zipTask = (Zip) antProject.createTask("zip");
                        zipTask.setBasedir(new File(distDir));
                        zipTask.setIncludes(zipName + "/**");
			zipTask.setDestFile(new File(distDir + File.separator + zipName + ".zip"));
			zipTask.execute();
		} catch (BuildException e) {
			throw new MojoExecutionException("Zipping application directory failed.", e);
		}
	}
	
	private List findClusters(File[] files) {
		List clusters = new ArrayList();
		for (int i = 0; i < files.length; i++)
			if (files[i].isDirectory()) {
				String folderName = files[i].getName();
                Iterator it2 = matchers.entrySet().iterator();
                while (it2.hasNext()) {
                    Map.Entry en = (Map.Entry)it2.next();
                    Pattern match = (Pattern) en.getValue();
                    if (match.matcher(folderName).matches()) {
                        clusters.add(files[i]);
                        it2.remove();
                        break;
                    }
                }
            }
		return clusters;
	}
	
	private void copyDirectory(File from, File to)
	throws IOException {
            Copy copyTask = (Copy) antProject.createTask("copy");
            copyTask.setTodir(to);
            FileSet fset = new FileSet();
            fset.setDir(from);
            copyTask.addFileset(fset);
            copyTask.execute();
	}
	
	private static void copyFile(String in, String out) throws IOException {
		copyResource(new FileInputStream(in), new FileOutputStream(out));
	}
	
	private static void copyResource(InputStream in, OutputStream out) throws IOException {
		byte[] buf = new byte[1024];
		int len;
		while ((len = in.read(buf)) > 0)
			out.write(buf, 0, len);
		in.close();
		out.close();
	}

}

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Reply via email to