Apache Ant Javac Task

Ant Javac task is used to compile Java source file. It scans source and destination directory to compile the source file. It only compiles if either .class is not present or .class is older than Java file.

If the Java source file is located into any package, source file should have directory hierarchy.

Apache Ant Javac Attributes

AttributeDescriptionRequired
srcdirSource Java file.Yes
destdirDestination to store the class files.No
includesA list of patterns of files that must be included.No
includesfileEach line of this file is taken to be an include pattern.No
excludesA list of patterns of files that must be excluded.No; defaults to default excludes or none if defaultexcludes is no
excludesfileEach line of this file is taken to be an exclude pattern.No
classpathThe classpath to use.No
bootclasspathPath of bootstrap class files.No
extdirsPath of installed extensions.No
encodingEncoding of source files.No
nowarnIt is used to pass -nowarn switch to the compiler.No
debugTo debug file during compilation.No
debuglevelSet level of debug file.No
optimizeIndicates whether source should be compiled with optimization.No
deprecationCompile with with deprecation information.No
verboseAsks the compiler for verbose output.No
dependEnables dependency tracking for compilers that support it.No
includeAntRuntimeWhether to include the Ant run-time libraries in the classpath or not.No
forkWhether to execute javac using the JDK compiler externally or not.No
executableComplete path to the javac executable.No
memoryInitialSizeSet initial memory size.No
memoryMaximumSizeThe maximum size of the memory for the underlying JVM.No

Apache Ant Javac Example

The following code compiles a Java source file and store class file into destination directory.

// build.xml

Example: Setting debug mode true

This example will compile all Java source file located into srcdir and store .class into destdir directory. It will compile Java source in debug mode because the debug mode is on.

Example : Setting source level and target version

This example will compile all Java source file located into srcdir and store .class into destdir directory. The source level is 1.8 and the .class file will run under Jdk 1.8.





Latest Courses