Arquivo de Maio de 2008

Minha primeira tarefa no Ant

Eu precisava chamar o Wise Installation System através do Ant aqui no trabalho e para isso fiz uma classe simples que implementa uma tarefa personalizada do Ant. Fica bem bacana. :)

import java.io.File;
import java.io.IOException;
import java.util.*;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Property;

/**
 * Task that calls Wise Installation System in the command line.
 * Works only on Windows.
 * You need to place ant.jar in your classpath to compile this class.
 * @author Thiago Chaves
 */
public class WiseBuild extends Task {

    private File _project;

    private File _wiseProgram;

    private List _compilerVariables = new ArrayList(); 

    public WiseBuild() {
    }

    /**
     * Indicates where the Wise executable program is (Wise32.exe).
     * @param f Full path to Wise32.exe.
     */
    public void setWiseProgram(File f) {
        _wiseProgram = f;
    }

    /**
     * The WSE project file to use.
     * @param f Full path to the .wse file.
     */
    public void setProject(File f) {
        _project = f;
    }

    /**
     * Compiler variables are like properties, i.e., they have a name and a value.
     * These variables must be declared in the Compiler Variables section in the WSE
     * project file.
     * @param p A name-value pair.
     */
    public void addCompilervariable(Property p) {
        _compilerVariables.add(p);
    }

    /**
     * Run, Forrest, run!
     */
    public void execute() throws BuildException {
        List lcmd = new ArrayList();
        lcmd.add(_wiseProgram.getAbsolutePath());
        lcmd.add("/c");
        Iterator i = _compilerVariables.iterator();
        while (i.hasNext()) {
            Property p = (Property)i.next();
            lcmd.add("/d" + p.getName() + "=" + p.getValue());
        }
        lcmd.add(_project.getAbsolutePath());
        System.out.println("Executing... " + lcmd);
        String[] cmdarray = (String[])lcmd.toArray(new String[]{});
        try {
            Process proc = Runtime.getRuntime().exec(cmdarray);
            int returnValue = proc.waitFor();
            if (returnValue != 0) {
                throw new BuildException("Wise execution returned an error value.");
            }
        } catch (IOException e) {
            throw new BuildException(e);
        } catch (InterruptedException e) {
            throw new BuildException(e);
        }
    }
}

Para usar a classe num build.xml, basta:

<taskdef name="wise"
	classname="WiseBuild"
	classpath="${dir.resources}/wiseant.jar"/>

<target name="go wise">
	<wise wiseprogram="C:/Program Files/Wise Installation System/Wise32.exe"
		project="${basedir}/myscript.wse">
		<compilervariable name="_DEBUG_" value="on"/>
	</wise>
</target>

Adicionar comentário 15 de Maio de 2008 às 11:28 Thiago


Calendário

Maio 2008
S T Q Q S S D
« Abr   Jun »
 1234
567891011
12131415161718
19202122232425
262728293031  

Minhas Publicações Recentes

Publicações por Mês

Estatísticas

Meta