c# print barcode zebra Executing a Java program from within an Ant task in Java

Create ECC200 in Java Executing a Java program from within an Ant task

Listing 19.6 Executing a Java program from within an Ant task
Encoding ECC200 In Java
Using Barcode maker for Java Control to generate, create ECC200 image in Java applications.
www.OnBarcode.com
Decoding Data Matrix ECC200 In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
package org.example.antbook.tasks; import java.io.File; import import import import import org.apache.tools.ant.BuildException; org.apache.tools.ant.Task; org.apache.tools.ant.taskdefs.Java; org.apache.tools.ant.types.Path; org.apache.tools.ant.types.Reference;
GS1 DataBar-14 Drawer In Java
Using Barcode encoder for Java Control to generate, create GS1 DataBar image in Java applications.
www.OnBarcode.com
Generating 1D Barcode In Java
Using Barcode creation for Java Control to generate, create Linear Barcode image in Java applications.
www.OnBarcode.com
public class SearcherTask extends Task { private Path classpath; private File indexDir; private String query; public void setClasspath(Path classpath) { this.classpath = classpath; }
QR Printer In Java
Using Barcode encoder for Java Control to generate, create QR-Code image in Java applications.
www.OnBarcode.com
USS Code 128 Creation In Java
Using Barcode creation for Java Control to generate, create Code 128A image in Java applications.
www.OnBarcode.com
Supports
PDF417 Creator In Java
Using Barcode generation for Java Control to generate, create PDF 417 image in Java applications.
www.OnBarcode.com
Generating Bookland EAN In Java
Using Barcode maker for Java Control to generate, create ISBN - 13 image in Java applications.
www.OnBarcode.com
classpath
Create DataMatrix In Java
Using Barcode generation for Java Control to generate, create Data Matrix 2d barcode image in Java applications.
www.OnBarcode.com
ECC200 Maker In C#
Using Barcode encoder for .NET framework Control to generate, create Data Matrix ECC200 image in Visual Studio .NET applications.
www.OnBarcode.com
attribute
QR Code Generation In Java
Using Barcode maker for BIRT reports Control to generate, create QR Code JIS X 0510 image in BIRT applications.
www.OnBarcode.com
GTIN - 13 Scanner In VB.NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
EXECUTING A JAVA PROGRAM WITHIN A TASK
2D Barcode Maker In .NET
Using Barcode creation for ASP.NET Control to generate, create Matrix image in ASP.NET applications.
www.OnBarcode.com
Generate 2D In Visual C#
Using Barcode generator for .NET framework Control to generate, create 2D Barcode image in .NET framework applications.
www.OnBarcode.com
public void setClasspathRef(Reference ref) { createClasspath().setRefid(ref); } public Path createClasspath() { if (classpath == null) { classpath = new Path(this.getProject()); } return classpath.createPath(); } public void setIndex(File indexDir) { this.indexDir = indexDir; } public void setQuery(String query) { this.query = query; }
Decode Code 128 Code Set A In VB.NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Generating Code 128 Code Set A In VB.NET
Using Barcode generator for .NET Control to generate, create Code 128A image in .NET applications.
www.OnBarcode.com
Supports
QR Code Maker In None
Using Barcode printer for Font Control to generate, create QR image in Font applications.
www.OnBarcode.com
Painting UPC-A In C#
Using Barcode maker for VS .NET Control to generate, create UPC Code image in .NET applications.
www.OnBarcode.com
classpathref
Print Barcode In VS .NET
Using Barcode maker for .NET framework Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Decode GS1 - 12 In VB.NET
Using Barcode reader for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
attribute
Supports nested
classpath
elements
Reuses the
<java> task
internally
public void execute() throws BuildException { Java javaTask = (Java) getProject().createTask("java"); javaTask.setTaskName(getTaskName()); javaTask.setClassname("org.example.antbook.tasks.Searcher"); javaTask.setClasspath(classpath); javaTask.createArg().setFile(indexDir); javaTask.createArg().setValue(query); javaTask.setFork(true); if (javaTask.executeJava() != 0) { throw new BuildException("error"); } } }
We call setTaskName with our current task name, from getTaskName(), so the output is prefixed with our custom task name instead of [java]. In this case, output from Searcher will be prefixed by [searcher]. The SearcherTask shown in listing 19.6 demonstrates several key techniques useful for wrapping a Java program within a task fa ade. Most importantly it provides flexibility in how the classpath is specified, allowing either a classpath or classpathref attribute, or nested <classpath> elements. Each of these required work in our task code, but the effort was minimal thanks to Ant s API. The main trick employed in SearcherTask is the use of the <java> task internally. While this does have a hackish feel to it, it s the easiest way to deal with the complexities of forking, classpath, command-line parameters, and JVM parameters. From the knowledge you ve gained in this chapter about how Ant populates tasks with data, you are equipped with the know-how to see how this is working. We simply call the setters and other special methods, such as the create/add-prefixed methods, just as Ant would do if we used <java> literally in a build file. 492
CHAPTER 1 9
WRITING ANT TASKS
19.10 SUPPORTING ARBITRARILY NAMED ELEMENTS
AND ATTRIBUTES
Before Ant 1.5, tasks could not dynamically add new attributes or elements at run time. As an example of this, you cannot add new <condition> tests, or other elements, inside <ejbjar> without changing Ant s source. Any task that needed to be dynamically extensible had to do so in an ugly manner. For example, a task that needed to support user-defined parameters would typically be specified like this in a build file:
<paramtask> <param name="username" value="erik"/> <param name="hostname" value="localhost"/> </paramtask>
It would be written in Java code in this manner:
package org.example.antbook.tasks; import org.apache.tools.ant.Task; import java.util.Vector; import java.util.Iterator; public class ParamTask extends Task { private Vector params = new Vector(); public Param createParam() { Param p = new Param(); params.add(p); return p; } public void execute() { Iterator iter = params.iterator(); while (iter.hasNext()) { Param p = (Param) iter.next(); log(p.getName() + " = " + p.getValue()); } } public static class Param { private String name; private String value; public String getName() { return name; } public void setName(String name) { this.name = name; }
SUPPORTING ARBITRARILY NAMED ELEMENTS AND ATTRIBUTES
public String getValue() { return value; } public void setValue(String value) { this.value = value; } } }
It is nicer to have user-defined parameters specified as attributes to our task, rather than the clunkier <param> subelements.
<dynatask username="erik" hostname="localhost"/>
Ant 1.5 added a dynamic configuration mechanism to let you do just that. The changes haven t trickled into Ant s own tasks, yet, but you can use it in your own code. With the DynamicConfigurator interface, your task can support new attributes and elements. See listing 19.7.
Listing 19.7 DynamicConfigurator allows tasks to dynamically support new attributes and elements
package org.example.antbook.tasks; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DynamicConfigurator; import org.apache.tools.ant.Task; import java.util.Enumeration; import java.util.Properties; public class DynaTask extends Task implements DynamicConfigurator { private Properties params = new Properties(); public void setDynamicAttribute(String name, String value) throws BuildException { params.setProperty(name, value); }
Copyright © OnBarcode.com . All rights reserved.