Ant
Getting the Ant tasks
Kotlin provides three tasks for Ant:
kotlinc
: Kotlin compiler targeting the JVMkotlin2js
: Kotlin compiler targeting JavaScriptwithKotlin
: Task to compile Kotlin files when using the standard javac Ant task
These tasks are defined in the kotlin-ant.jar library which is located in the lib
folder in the Kotlin Compiler archive. Ant version 1.8.2+ is required.
Targeting JVM with Kotlin-only source
When the project consists of exclusively Kotlin source code, the easiest way to compile the project is to use the kotlinc
task:
where ${kotlin.lib}
points to the folder where the Kotlin standalone compiler was unzipped.
Targeting JVM with Kotlin-only source and multiple roots
If a project consists of multiple source roots, use src
as elements to define paths:
Targeting JVM with Kotlin and Java source
If a project consists of both Kotlin and Java source code, while it is possible to use kotlinc
, to avoid repetition of task parameters, it is recommended to use withKotlin
task:
You can also specify the name of the module being compiled as the moduleName
attribute:
Targeting JavaScript with single source folder
Targeting JavaScript with Prefix, PostFix and sourcemap options
Targeting JavaScript with single source folder and metaInfo option
The metaInfo
option is useful, if you want to distribute the result of translation as a Kotlin/JavaScript library. If metaInfo
was set to true
, then during compilation additional JS file with binary metadata will be created. This file should be distributed together with the result of translation:
References
Complete list of elements and attributes are listed below:
Attributes common for kotlinc and kotlin2js
Name | Description | Required | Default Value |
---|---|---|---|
| Kotlin source file or directory to compile | Yes | |
| Suppresses all compilation warnings | No | false |
| Does not include the Kotlin standard library into the classpath | No | false |
| Fails the build if errors are detected during the compilation | No | true |
kotlinc attributes
Name | Description | Required | Default Value |
---|---|---|---|
| Destination directory or .jar file name | Yes | |
| Compilation class path | No | |
| Compilation class path reference | No | |
| If | No | true |
| Name of the module being compiled | No | The name of the target (if specified) or the project |
kotlin2js attributes
Name | Description | Required |
---|---|---|
| Destination file | Yes |
| Paths to Kotlin libraries | No |
| Prefix to use for generated JavaScript files | No |
| Suffix to use for generated JavaScript files | No |
| Whether sourcemap file should be generated | No |
| Whether metadata file with binary descriptors should be generated | No |
| Should compiler generated code call the main function | No |
Passing raw compiler arguments
To pass custom raw compiler arguments, you can use <compilerarg>
elements with either value
or line
attributes. This can be done within the <kotlinc>
, <kotlin2js>
, and <withKotlin>
task elements, as follows:
The full list of arguments that can be used is shown when you run kotlinc -help
.