de.tsl2.nano.execution.AntRunner Maven / Gradle / Ivy
Show all versions of tsl2.nano.common Show documentation
/*
* File: $HeadURL$
* Id : $Id$
*
* created by: Thomas Schneider
* created on: Oct 7, 2011
*
* Copyright: (c) Thomas Schneider 2011, all rights reserved
*/
package de.tsl2.nano.execution;
import java.io.File;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.BuildListener;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Location;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Expand;
import org.apache.tools.ant.taskdefs.Jar;
import org.apache.tools.ant.taskdefs.MatchingTask;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.resources.Files;
import org.apache.tools.ant.types.selectors.FileSelector;
import de.tsl2.nano.core.ManagedException;
import de.tsl2.nano.core.cls.BeanAttribute;
import de.tsl2.nano.core.cls.BeanClass;
import de.tsl2.nano.core.exception.Message;
import de.tsl2.nano.core.log.LogFactory;
import de.tsl2.nano.core.util.StringUtil;
import de.tsl2.nano.core.util.Util;
/**
* is able to run ant targets given by name and properties. If task is a {@link MatchingTask}, an array of
* {@link FileSet} can be given.
*
* please have a look at the setter methods of the task to evaluate the properties to set. the properties are case
* sensitive.
* if use the task 'Expand', the class org.apache.tools.ant.taskdefs.Expand
will be loaded and
* instantiated. you must set the property 'destFile' with java.io.File. the AntRunner will call
* Expand.setDestFile(File).
*
* see @link{http://ant.apache.org/manual} for a list of standard ant tasks.
*
* short list of tasks:
* Ant AntCall ANTLR AntStructure AntVersion Apply/ExecOn Apt Attrib Augment Available Basename Bindtargets BuildNumber
* BUnzip2 BZip2 Cab Continuus/Synergy Tasks CvsChangeLog Checksum Chgrp Chmod Chown Clearcase Tasks Componentdef Concat
* Condition Supported conditions Copy Copydir Copyfile Cvs CVSPass CvsTagDiff CvsVersion Defaultexcludes Delete Deltree
* Depend Dependset Diagnostics Dirname Ear Echo Echoproperties EchoXML EJB Tasks Exec Fail Filter FixCRLF FTP GenKey
* Get GUnzip GZip Hostinfo Image Import Include Input Jar Jarlib-available Jarlib-display Jarlib-manifest
* Jarlib-resolve Java Javac JavaCC Javadoc/Javadoc2 Javah JDepend JJDoc JJTree Jlink JspC JUnit JUnitReport Length
* LoadFile LoadProperties LoadResource Local MacroDef Mail MakeURL Manifest ManifestClassPath MimeMail Mkdir Move
* Native2Ascii NetRexxC Nice Parallel Patch PathConvert Perforce Tasks PreSetDef ProjectHelper Property PropertyFile
* PropertyHelper Pvcs Record Rename RenameExtensions Replace ReplaceRegExp ResourceCount Retry RExec Rmic Rpm
* SchemaValidate Scp Script Scriptdef Sequential ServerDeploy Setproxy SignJar Sleep SourceOffSite Sound Splash Sql
* Sshexec Sshsession Subant Symlink Sync Tar Taskdef Telnet Tempfile Touch Translate Truncate TStamp Typedef Unjar
* Untar Unwar Unzip Uptodate Microsoft Visual SourceSafe Tasks Waitfor War WhichResource Weblogic JSP Compiler
* XmlProperty XmlValidate XSLT/Style Zip
*
* @author Thomas Schneider
* @version $Revision$
*/
public class AntRunner {
public static final String TASK_PATH = Jar.class.getPackage().getName();
public static final String TASK_JAR = "Jar";
public static final String TASK_ZIP = "Zip";
/** usefull to unzip files. see {@link Expand} */
public static final String TASK_UNJAR = "Expand";
public static final String TASK_COPY = "Copy";
public static final String TASK_MOVE = "Move";
public static final String TASK_DELETE = "Delete";
/** useful to list and search files. see {@link Files} */
public static final String TASK_FILES = "Files";
public static final String TASK_REPLACE_REGEXP = "optional.ReplaceRegExp";
public static final String TASK_XSLT = "XSLTProcess";
public static final String TASK_SQL = "Sql";
private static final Log LOG = LogFactory.getLog(AntRunner.class);
/**
* delegates to {@link #runTask(String, Map, FileSet...)} using {@link #createFileSets(String)} to create the
* {@link FileSet}s.
*/
public static void runTask(String name, Properties taskProperties, String fileSetExpression) {
runTask(name, taskProperties, fileSetExpression != null ? createFileSets(fileSetExpression) : (FileSet[]) null);
}
/**
* starts the task by name using its properties and perhaps some filesets.
*
* See here for an overview of ants standard tasks.
*
*
* Example:
* FileSet[] fileSets = AntRunner.createFileSets("./:{**\*.*ml}**\*.xml;" + basedir.getPath() + ":{*.txt}");
* Properties props = new Properties();
* props.put("destFile", new File(destFile));
* AntRunner.runTask("Jar", props, fileSets);
*
*
* @param name task name
* @param taskProperties task properties
* @param fileSets optional filesets (depends on the task!)
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void runTask(String name, Map taskProperties, FileSet... fileSets) {
Class> taskType = null;
Task task = null;
try {
taskType = BeanClass.load(TASK_PATH + "." + name);
task = (Task) taskType.newInstance();
} catch (final Exception e) {
ManagedException.forward(e);
}
task.setProject(new Project());
task.getProject().setName(name);
task.getProject().init();
task.setTaskType("AntRunner." + taskType);
task.setTaskName("AntRunner." + taskType);
task.setOwningTarget(new Target());
task.setLocation(new Location(System.getProperty("user.dir")));
task.getProject().addBuildListener(createLogfileBuildListener());
task.getProject().addBuildListener(createPipedAntBuildListener(new PipedOutputStream()));
task.getProject().addBuildListener(createMessageListener());
/*
* now we use the properties to fill bean attributes of ant task
*/
final Set