Top Description Fields Constructors Methods
org.apache.tools.ant

public Class Main

extends Object
implements AntMain
Class Inheritance
All Implemented Interfaces
org.apache.tools.ant.launch.AntMain
Imports
java.io.File, .IOException, .InputStream, .PrintStream, java.nio.file.Files, .Paths, java.util.ArrayList, .Arrays, .Collections, .Enumeration, .HashMap, .HashSet, .Iterator, .List, .Map, .Properties, .Set, .Vector, java.util.stream.Collectors, org.apache.tools.ant.input.DefaultInputHandler, .InputHandler, org.apache.tools.ant.launch.AntMain, org.apache.tools.ant.listener.SilentLogger, org.apache.tools.ant.property.GetProperty, .ResolvePropertyMap, org.apache.tools.ant.util.ClasspathUtils, .FileUtils, .ProxySetup, .StreamUtils

Command line entry point into Ant. This class is entered via the canonical `public static void main` entry point and reads the command line arguments. It then assembles and executes an Ant project.

If you integrating Ant into some other tool, this is not the class to use as an entry point. Please see the source code of this class to see how it manipulates the Ant project classes.

Field Summary

Modifier and TypeField and Description
private boolean
allowInput

Indicates whether this build is to support interactive input

private static String
antVersion

Cache of the Ant version information when it has been loaded.

private File
buildFile

File that we are using for configuration.

public static final String
DEFAULT_BUILD_FILENAME

The default build file name.

private final Properties
definedProps

Set of properties that can be used by tasks.

private boolean
emacsMode

Whether or not output to the log is to be unadorned.

private PrintStream
err

Stream that we are using for logging error messages.

private final Map<Class<?>, List<String>>
private String
inputHandlerClassname

The Ant InputHandler class.

private boolean
isLogFileUsed

Whether or not a logfile is being used.

private boolean
keepGoingMode

keep going mode

private static final Set<String>
LAUNCH_COMMANDS

A Set of args that are handled by the launcher and should not be seen by Main.

private final Vector<String>
listeners

Names of classes to add as listeners to project.

private String
loggerClassname

The Ant logger class.

private int
msgOutputLevel

Our current message output status.

private static final GetProperty
private PrintStream
out

Stream to use for logging.

private boolean
projectHelp

Whether or not we should only parse and display the project help information.

private final Vector<String>
propertyFiles

File names of property files to load on startup.

private boolean
proxy

proxy flag: default is false

private boolean
readyToRun

Whether or not this instance has successfully been constructed and is ready to run.

private static String
shortAntVersion

Cache of the short Ant version information when it has been loaded.

private boolean
silent

Whether or not log output should be reduced to the minimum

private final Vector<String>
targets

The build targets.

private Integer
threadPriority

optional thread priority

Constructor Summary

AccessConstructor and Description
public
Main()

Constructor used when creating Main for later arg processing and startup

protected
Main(final String[]
Command line arguments. Must not be null.
args
)

Deprecated since 1.6.x
Sole constructor, which parses and deals with command line arguments.

Method Summary

Modifier and TypeMethod and Description
protected void
addBuildListeners(final Project
The project to add listeners to. Must not be null.
project
)

Adds the listeners specified in the command line arguments, along with the default listener, to the specified project.

private void
addInputHandler(final Project
the project instance.
project
)

Creates the InputHandler and adds it to the project.

private BuildLogger

Returns:

the logger instance for this build.
createLogger
()

Creates the default build logger for sending build events to the ant log.

protected void
exit(final int
code to exit with
exitCode
)

This operation is expected to call System#exit(int), which is what the base version does.

private File

Returns:

A handle to the build file if one is found, null if not
findBuildFile
(final String
Leaf directory of search. Must not be null.
start
,
final String
Suffix filename to look for in parents. Must not be null.
suffix
)

Search parent directories for the build file.

private static int

Returns:

the correct place in the list for the given name
findTargetPosition
(final Vector<String>
The current list of names. Must not be null.
names
,
final String
The name to find a place for. Must not be null.
name
)

Searches for the correct place to insert a name into a list so as to keep the list sorted alphabetically.

public static synchronized String

Returns:

the Ant version information as a String (always non-null)
getAntVersion
()

Returns the Ant version information, if available.

private File

Returns:

Parent file or null if none
getParentFile
(final File
File to find parent of. Must not be null.
file
)

Deprecated since 1.6.x
Helper to get the parent file for a given file.
public static String

Returns:

the short Ant version information as a String (always non-null)
getShortAntVersion
()

Returns the short Ant version information, if available.

private int
handleArgBuildFile(final String[] args, int pos)

Handle the -buildfile, -file, -f argument

private int
handleArgDefine(final String[] args, int argPos)

Handler -D argument

private int
handleArgInputHandler(final String[] args, int pos)

Handle the -inputhandler argument.

private int
handleArgListener(final String[] args, int pos)

Handle -listener argument

private int
handleArgLogger(final String[] args, int pos)

Handle the -logger argument.

private int
handleArgNice(final String[] args, int pos)

Handle the -nice argument.

private int
handleArgPropertyFile(final String[] args, int pos)

Handle the -propertyfile argument.

private void
handleLogfile()

Close logfiles, if we have been writing to them.

private void
loadPropertyFiles()

Load the property files specified by -propertyfile

public static void
main(final String[]
Command line arguments. Must not be null.
args
)

Command line entry point.

private static void
printDescription(final Project
The project to display a description of. Must not be null.
project
)

Prints the description of a project (if there is one) to System.out.

private static void
printMessage(final Throwable
Throwable to print the message of. Must not be null.
t
)

Prints the message of the Throwable if it (the message) is not null.

private static void
printTargets(final Project
The project to display a description of. Must not be null.
project
,
boolean
Whether or not subtarget names should also be printed.
printSubTargets
,
final boolean printDependencies)

Prints a list of all targets in the specified project to System.out, optionally including subtargets.

private static void
printTargets(final Project
the project instance.
project
,
final Vector<String>
The names to be printed. Must not be null.
names
,
final Vector<String>
The associated target descriptions. May be null, in which case no descriptions are displayed. If non-null, this should have as many elements as names.
descriptions
,
final Vector<Enumeration<String>> dependencies, final String
The heading to display. Should not be null.
heading
,
final int
The maximum length of the names of the targets. If descriptions are given, they are padded to this position so they line up (so long as the names really are shorter than this).
maxlen
)

Writes a formatted list of target names to System.out with an optional description.

private static void
printUsage()

Prints the usage information for this class to System.out.

private static void
printVersion(final int logLevel)

Prints the Ant version information to System.out.

private void
processArgs(final String[]
the command line arguments.
args
)

Process command line arguments.

private static Map<String, Target>

Returns:

the filtered targets.
removeDuplicateTargets
(final Map<String, Target>
the targets to filter.
targets
)

Targets in imported files with a project name and not overloaded by the main build file will be in the target map twice.

private void
runBuild(final ClassLoader
The classloader to use to find core classes. May be null, in which case the system classloader is used.
coreLoader
)

Executes the build.

private void
setProperties(final Project project)

public static void
start(final String[]
Command line arguments. Must not be null.
args
,
final Properties
Any extra properties to use in this build. May be null, which is the equivalent to passing in an empty set of properties.
additionalUserProperties
,
final ClassLoader
Classloader used for core classes. May be null in which case the system classloader is used.
coreLoader
)

Creates a new instance of this class using the arguments specified, gives it any extra user properties which have been specified, and then runs the build using the classloader provided.

public void
startAnt(final String[]
command line args
args
,
final Properties
properties to set beyond those that may be specified on the args list
additionalUserProperties
,
final ClassLoader
- not used
coreLoader
)

Implements org.apache.tools.ant.launch.AntMain.startAnt.

Start Ant
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

allowInputback to summary
private boolean allowInput

Indicates whether this build is to support interactive input

antVersionback to summary
private static String antVersion

Cache of the Ant version information when it has been loaded.

buildFileback to summary
private File buildFile

File that we are using for configuration.

DEFAULT_BUILD_FILENAMEback to summary
public static final String DEFAULT_BUILD_FILENAME

The default build file name. build.xml

definedPropsback to summary
private final Properties definedProps

Set of properties that can be used by tasks.

emacsModeback to summary
private boolean emacsMode

Whether or not output to the log is to be unadorned.

errback to summary
private PrintStream err

Stream that we are using for logging error messages.

extraArgumentsback to summary
private final Map<Class<?>, List<String>> extraArguments
inputHandlerClassnameback to summary
private String inputHandlerClassname

The Ant InputHandler class. There may be only one input handler.

isLogFileUsedback to summary
private boolean isLogFileUsed

Whether or not a logfile is being used. This is used to check if the output streams must be closed.

keepGoingModeback to summary
private boolean keepGoingMode

keep going mode

LAUNCH_COMMANDSback to summary
private static final Set<String> LAUNCH_COMMANDS

A Set of args that are handled by the launcher and should not be seen by Main.

listenersback to summary
private final Vector<String> listeners

Names of classes to add as listeners to project.

loggerClassnameback to summary
private String loggerClassname

The Ant logger class. There may be only one logger. It will have the right to use the 'out' PrintStream. The class must implements the BuildLogger interface.

msgOutputLevelback to summary
private int msgOutputLevel

Our current message output status. Follows Project.MSG_XXX.

NOPROPERTIESback to summary
private static final GetProperty NOPROPERTIES
outback to summary
private PrintStream out

Stream to use for logging.

projectHelpback to summary
private boolean projectHelp

Whether or not we should only parse and display the project help information.

propertyFilesback to summary
private final Vector<String> propertyFiles

File names of property files to load on startup.

proxyback to summary
private boolean proxy

proxy flag: default is false

readyToRunback to summary
private boolean readyToRun

Whether or not this instance has successfully been constructed and is ready to run.

shortAntVersionback to summary
private static String shortAntVersion

Cache of the short Ant version information when it has been loaded.

silentback to summary
private boolean silent

Whether or not log output should be reduced to the minimum

targetsback to summary
private final Vector<String> targets

The build targets.

threadPriorityback to summary
private Integer threadPriority

optional thread priority

Constructor Detail

Mainback to summary
public Main()

Constructor used when creating Main for later arg processing and startup

Mainback to summary
protected Main(final String[] args) throws BuildException

Deprecated

since 1.6.x

Sole constructor, which parses and deals with command line arguments.

Parameters
args:String[]

Command line arguments. Must not be null.

Annotations
@Deprecated
Exceptions
BuildException:
if the specified build file doesn't exist or is a directory.

Method Detail

addBuildListenersback to summary
protected void addBuildListeners(final Project project)

Adds the listeners specified in the command line arguments, along with the default listener, to the specified project.

Parameters
project:Project

The project to add listeners to. Must not be null.

addInputHandlerback to summary
private void addInputHandler(final Project project) throws BuildException

Creates the InputHandler and adds it to the project.

Parameters
project:Project

the project instance.

Exceptions
BuildException:
if a specified InputHandler implementation could not be loaded.
createLoggerback to summary
private BuildLogger createLogger()

Creates the default build logger for sending build events to the ant log.

Returns:BuildLogger

the logger instance for this build.

exitback to summary
protected void exit(final int exitCode)

This operation is expected to call System#exit(int), which is what the base version does. However, it is possible to do something else.

Parameters
exitCode:int

code to exit with

findBuildFileback to summary
private File findBuildFile(final String start, final String suffix)

Search parent directories for the build file.

Takes the given target as a suffix to append to each parent directory in search of a build file. Once the root of the file-system has been reached null is returned.

Parameters
start:String

Leaf directory of search. Must not be null.

suffix:String

Suffix filename to look for in parents. Must not be null.

Returns:File

A handle to the build file if one is found, null if not

findTargetPositionback to summary
private static int findTargetPosition(final Vector<String> names, final String name)

Searches for the correct place to insert a name into a list so as to keep the list sorted alphabetically.

Parameters
names:Vector<String>

The current list of names. Must not be null.

name:String

The name to find a place for. Must not be null.

Returns:int

the correct place in the list for the given name

getAntVersionback to summary
public static synchronized String getAntVersion() throws BuildException

Returns the Ant version information, if available. Once the information has been loaded once, it's cached and returned from the cache on future calls.

Returns:String

the Ant version information as a String (always non-null)

Exceptions
BuildException:
if the version information is unavailable
getParentFileback to summary
private File getParentFile(final File file)

Deprecated

since 1.6.x

Helper to get the parent file for a given file.

Added to simulate File.getParentFile() from JDK 1.2.

Parameters
file:File

File to find parent of. Must not be null.

Returns:File

Parent file or null if none

Annotations
@Deprecated
getShortAntVersionback to summary
public static String getShortAntVersion() throws BuildException

Returns the short Ant version information, if available. Once the information has been loaded once, it's cached and returned from the cache on future calls.

Returns:String

the short Ant version information as a String (always non-null)

Exceptions
BuildException:
BuildException if the version information is unavailable
Since
Ant 1.9.3
handleArgBuildFileback to summary
private int handleArgBuildFile(final String[] args, int pos)

Handle the -buildfile, -file, -f argument

handleArgDefineback to summary
private int handleArgDefine(final String[] args, int argPos)

Handler -D argument

handleArgInputHandlerback to summary
private int handleArgInputHandler(final String[] args, int pos)

Handle the -inputhandler argument.

handleArgListenerback to summary
private int handleArgListener(final String[] args, int pos)

Handle -listener argument

handleArgLoggerback to summary
private int handleArgLogger(final String[] args, int pos)

Handle the -logger argument.

handleArgNiceback to summary
private int handleArgNice(final String[] args, int pos)

Handle the -nice argument.

handleArgPropertyFileback to summary
private int handleArgPropertyFile(final String[] args, int pos)

Handle the -propertyfile argument.

handleLogfileback to summary
private void handleLogfile()

Close logfiles, if we have been writing to them.

Since
Ant 1.6
loadPropertyFilesback to summary
private void loadPropertyFiles()

Load the property files specified by -propertyfile

mainback to summary
public static void main(final String[] args)

Command line entry point. This method kicks off the building of a project object and executes a build using either a given target or the default target.

Parameters
args:String[]

Command line arguments. Must not be null.

printDescriptionback to summary
private static void printDescription(final Project project)

Prints the description of a project (if there is one) to System.out.

Parameters
project:Project

The project to display a description of. Must not be null.

printMessageback to summary
private static void printMessage(final Throwable t)

Prints the message of the Throwable if it (the message) is not null.

Parameters
t:Throwable

Throwable to print the message of. Must not be null.

printTargetsback to summary
private static void printTargets(final Project project, boolean printSubTargets, final boolean printDependencies)

Prints a list of all targets in the specified project to System.out, optionally including subtargets.

Parameters
project:Project

The project to display a description of. Must not be null.

printSubTargets:boolean

Whether or not subtarget names should also be printed.

printTargetsback to summary
private static void printTargets(final Project project, final Vector<String> names, final Vector<String> descriptions, final Vector<Enumeration<String>> dependencies, final String heading, final int maxlen)

Writes a formatted list of target names to System.out with an optional description.

Parameters
project:Project

the project instance.

names:Vector<String>

The names to be printed. Must not be null.

descriptions:Vector<String>

The associated target descriptions. May be null, in which case no descriptions are displayed. If non-null, this should have as many elements as names.

heading:String

The heading to display. Should not be null.

maxlen:int

The maximum length of the names of the targets. If descriptions are given, they are padded to this position so they line up (so long as the names really are shorter than this).

printUsageback to summary
private static void printUsage()

Prints the usage information for this class to System.out.

printVersionback to summary
private static void printVersion(final int logLevel) throws BuildException

Prints the Ant version information to System.out.

Exceptions
BuildException:
if the version information is unavailable
processArgsback to summary
private void processArgs(final String[] args)

Process command line arguments. When ant is started from Launcher, launcher-only arguments do not get passed through to this routine.

Parameters
args:String[]

the command line arguments.

Since
Ant 1.6
removeDuplicateTargetsback to summary
private static Map<String, Target> removeDuplicateTargets(final Map<String, Target> targets)

Targets in imported files with a project name and not overloaded by the main build file will be in the target map twice. This method removes the duplicate target.

Parameters
targets:Map<String, Target>

the targets to filter.

Returns:Map<String, Target>

the filtered targets.

runBuildback to summary
private void runBuild(final ClassLoader coreLoader) throws BuildException

Executes the build. If the constructor for this instance failed (e.g. returned after issuing a warning), this method returns immediately.

Parameters
coreLoader:ClassLoader

The classloader to use to find core classes. May be null, in which case the system classloader is used.

Exceptions
BuildException:
if the build fails
setPropertiesback to summary
private void setProperties(final Project project)
startback to summary
public static void start(final String[] args, final Properties additionalUserProperties, final ClassLoader coreLoader)

Creates a new instance of this class using the arguments specified, gives it any extra user properties which have been specified, and then runs the build using the classloader provided.

Parameters
args:String[]

Command line arguments. Must not be null.

additionalUserProperties:Properties

Any extra properties to use in this build. May be null, which is the equivalent to passing in an empty set of properties.

coreLoader:ClassLoader

Classloader used for core classes. May be null in which case the system classloader is used.

startAntback to summary
public void startAnt(final String[] args, final Properties additionalUserProperties, final ClassLoader coreLoader)

Implements org.apache.tools.ant.launch.AntMain.startAnt.

Start Ant

Parameters
args:String[]

command line args

additionalUserProperties:Properties

properties to set beyond those that may be specified on the args list

coreLoader:ClassLoader

- not used

Since
Ant 1.6