quarta-feira, 19 de janeiro de 2011

Gaia Framework - utilizando API do Ant para gerar Jar

Como primeiro post técnico sobre o Gaia Framework escolhi a integração feita com a API do Ant. Mais especificamente a task que fazia a compactação dos arquivos e criar um Jar bacana. Basicamente o XML visto a baixo:

<project default="createJar">
   <target
name="createJar" >
     <jar
destfile="gaia-dynamic-entity-2.0.jar" basedir="." />
   </target>
</project>


O foda é fazer esse XML virar código Java. Depois de muito procurar no Google eu descobri que não sei procurar ahhahhahaha. Por isso parti pro velho e sábio método milenar da "Tentativa e Erro" ... basicamente é simples ... adicionamos o jar do Ant no projeto e vamos chutando as classes a serem usadas. Não deve ser muito difícil né? eheheheheheh

Bom para inlcuir o Jar do Ant no projeto basta que seja importada a seguinte dependência:

<dependency>
   <groupId>
org.apache.ant</groupId>
   <artifactId>
ant</artifactId>
   <version>
1.8.2</version>
</dependency>


Segundo passo é decobrir as classes que tinha que usar, mas até que foi simples. O nome delas é igual ao das tags, veja o código Java:

package net.sf.gaia.util;

import java.io.File;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.taskdefs.Jar;

public abstract class GaiaUtils {

  private static final Log logger = LogFactory.getLog(GaiaUtils.class);

  public final static String getGaiaPath() {
    String path = System.getProperty("gaia.dir");
    if (path == null) {
       path = System.getProperty("user.dir");
    }
    return path;
  }

  public final static String getGaiaTempPath() {
    return getGaiaPath() + File.separator + "temp";
  }

  public final static String getGaiaServicePath() {
    String path = System.getProperty("gaia.service.dir");
    if (path == null) {
       path = System.getProperty("user.dir");
    }
    return path;
  }
  public final static void createJarFile(String jarName, String classPath) {
    try {
       if (classPath == null) {
          classPath = getGaiaTempPath() + File.separator + "classes";
       }
       
       logger.info("Creating pack classes from: " + classPath + " ...");
       long l = System.currentTimeMillis();

       if (!jarName.endsWith(".jar")) {
          jarName += ".jar";
       }
       
       File jarDir = new File(getGaiaServicePath());
       if (!jarDir.exists()) {
          jarDir.mkdir();
       }
       
       File jarTempDir = new File(classPath);
       if (!jarTempDir.exists()) {
          jarTempDir.mkdir();
       }

       Project proj = new Project();
       Target target = new Target();
       Jar jar = new Jar();
       jar.setBasedir(jarTempDir);
       
       File fileJar = new File(jarDir, jarName);
       // Delete the jar file if it already exists
       if (fileJar.exists() == true) {
          fileJar.delete();
       }
       
       jar.setDestFile(fileJar);

       jar.setProject(proj);
       
       target.addTask(jar);
       target.setName("createJar");
       target.setProject(proj);
       
       proj.addTarget(target);
       proj.setDefault("createJar");
       
       jar.execute();
       logger.info("Created jar file: " + jarName + " ... " + (System.currentTimeMillis() - l) + " ms");
    } catch (Exception e) {
       logger.error("Can't create " + jarName, e);
    }
  }
}

Nenhum comentário:

Postar um comentário