Client library for communicating with the Kotlin compilation daemon, enabling remote compilation services and incremental compilation workflows.
npx @tessl/cli install tessl/maven-org-jetbrains-kotlin--kotlin-daemon-client@2.2.00
# Kotlin Daemon Client
1
2
The Kotlin Daemon Client is a comprehensive client library for communicating with the Kotlin compilation daemon, enabling remote compilation services and incremental compilation workflows. It provides APIs for establishing compilation sessions, managing compiler services, and handling communication between build tools (like Gradle or Maven) and the Kotlin compiler daemon process.
3
4
## Package Information
5
6
- **Package Name**: kotlin-daemon-client
7
- **Package Type**: maven
8
- **Language**: Kotlin
9
- **Group ID**: org.jetbrains.kotlin
10
- **Artifact ID**: kotlin-daemon-client
11
- **Installation**:
12
```gradle
13
implementation("org.jetbrains.kotlin:kotlin-daemon-client:2.2.0")
14
```
15
```xml
16
<dependency>
17
<groupId>org.jetbrains.kotlin</groupId>
18
<artifactId>kotlin-daemon-client</artifactId>
19
<version>2.2.0</version>
20
</dependency>
21
```
22
23
## Core Imports
24
25
```kotlin
26
import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient
27
import org.jetbrains.kotlin.daemon.client.CompilationServices
28
import org.jetbrains.kotlin.daemon.client.CompileServiceSession
29
import org.jetbrains.kotlin.daemon.client.DaemonReportingTargets
30
```
31
32
## Basic Usage
33
34
```kotlin
35
import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient
36
import org.jetbrains.kotlin.daemon.common.*
37
import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector
38
import java.io.File
39
40
// Set up daemon connection parameters
41
val compilerId = CompilerId()
42
val daemonJVMOptions = DaemonJVMOptions()
43
val daemonOptions = DaemonOptions()
44
val reportingTargets = DaemonReportingTargets(out = System.out)
45
46
// Connect to the daemon
47
val compileService = KotlinCompilerClient.connectToCompileService(
48
compilerId = compilerId,
49
daemonJVMOptions = daemonJVMOptions,
50
daemonOptions = daemonOptions,
51
reportingTargets = reportingTargets,
52
autostart = true
53
)
54
55
if (compileService != null) {
56
// Compile with the daemon
57
val messageCollector = PrintingMessageCollector(System.out, null, false)
58
val compilationResult = KotlinCompilerClient.compile(
59
compilerService = compileService,
60
sessionId = CompileService.NO_SESSION,
61
targetPlatform = CompileService.TargetPlatform.JVM,
62
args = arrayOf("MyClass.kt"),
63
messageCollector = messageCollector
64
)
65
66
println("Compilation result: $compilationResult")
67
}
68
```
69
70
## Architecture
71
72
The Kotlin Daemon Client is built around several key components:
73
74
- **KotlinCompilerClient**: Main singleton object providing daemon connection and compilation services
75
- **Service Facades**: Server implementations for handling compiler callbacks and services
76
- **REPL Support**: Complete REPL (Read-Eval-Print Loop) client functionality for interactive compilation
77
- **Session Management**: Lease/release mechanisms for managing compilation sessions
78
- **Stream Servers**: Remote input/output stream handling for daemon communication
79
- **Reporting System**: Comprehensive error reporting and diagnostic collection
80
81
## Capabilities
82
83
### Daemon Connection Management
84
85
Core functionality for finding, connecting to, and managing Kotlin compiler daemon processes. Handles daemon discovery, startup, and connection lifecycle.
86
87
```kotlin { .api }
88
object KotlinCompilerClient {
89
fun connectToCompileService(
90
compilerId: CompilerId,
91
daemonJVMOptions: DaemonJVMOptions,
92
daemonOptions: DaemonOptions,
93
reportingTargets: DaemonReportingTargets,
94
autostart: Boolean = true,
95
checkId: Boolean = true
96
): CompileService?
97
98
fun connectAndLease(
99
compilerId: CompilerId,
100
clientAliveFlagFile: File,
101
daemonJVMOptions: DaemonJVMOptions,
102
daemonOptions: DaemonOptions,
103
reportingTargets: DaemonReportingTargets,
104
autostart: Boolean,
105
leaseSession: Boolean,
106
sessionAliveFlagFile: File? = null
107
): CompileServiceSession?
108
}
109
```
110
111
[Daemon Connection Management](./daemon-connection.md)
112
113
### Compilation Services
114
115
Compilation execution through the daemon with support for different target platforms, compiler modes, and incremental compilation workflows.
116
117
```kotlin { .api }
118
fun compile(
119
compilerService: CompileService,
120
sessionId: Int,
121
targetPlatform: CompileService.TargetPlatform,
122
args: Array<out String>,
123
messageCollector: MessageCollector,
124
outputsCollector: ((File, List<File>) -> Unit)? = null,
125
compilerMode: CompilerMode = CompilerMode.NON_INCREMENTAL_COMPILER,
126
reportSeverity: ReportSeverity = ReportSeverity.INFO,
127
port: Int = SOCKET_ANY_FREE_PORT,
128
profiler: Profiler = DummyProfiler()
129
): Int
130
131
data class CompilationServices(
132
val incrementalCompilationComponents: IncrementalCompilationComponents? = null,
133
val lookupTracker: LookupTracker? = null,
134
val compilationCanceledStatus: CompilationCanceledStatus? = null
135
)
136
```
137
138
[Compilation Services](./compilation-services.md)
139
140
### REPL Client
141
142
Interactive Kotlin compilation and evaluation through the daemon with state management and history tracking.
143
144
```kotlin { .api }
145
class KotlinRemoteReplCompilerClient(
146
protected val compileService: CompileService,
147
clientAliveFlagFile: File?,
148
targetPlatform: CompileService.TargetPlatform,
149
args: Array<out String>,
150
messageCollector: MessageCollector,
151
templateClasspath: List<File>,
152
templateClassName: String,
153
port: Int = SOCKET_ANY_FREE_PORT
154
) : ReplCompiler {
155
fun dispose()
156
override fun createState(lock: ReentrantReadWriteLock): IReplStageState<*>
157
override fun check(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCheckResult
158
override fun compile(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCompileResult
159
}
160
```
161
162
[REPL Client](./repl-client.md)
163
164
### Service Facades
165
166
Server implementations for handling compiler callbacks, incremental compilation services, and communication with the daemon process.
167
168
```kotlin { .api }
169
class BasicCompilerServicesWithResultsFacadeServer(
170
val messageCollector: MessageCollector,
171
val outputsCollector: ((File, List<File>) -> Unit)? = null,
172
port: Int = SOCKET_ANY_FREE_PORT
173
) : CompilerServicesFacadeBase
174
175
class CompilerCallbackServicesFacadeServer(
176
val incrementalCompilationComponents: IncrementalCompilationComponents? = null,
177
val lookupTracker: LookupTracker? = null,
178
val compilationCanceledStatus: CompilationCanceledStatus? = null,
179
// ... additional parameters
180
port: Int = SOCKET_ANY_FREE_PORT
181
) : CompilerCallbackServicesFacade
182
```
183
184
[Service Facades](./service-facades.md)
185
186
### Stream Servers
187
188
Remote input/output stream handling for daemon communication, enabling data transfer between client and daemon processes.
189
190
```kotlin { .api }
191
class RemoteInputStreamServer(
192
val `in`: InputStream,
193
port: Int = SOCKET_ANY_FREE_PORT
194
) : RemoteInputStream
195
196
class RemoteOutputStreamServer(
197
val out: OutputStream,
198
port: Int = SOCKET_ANY_FREE_PORT
199
) : RemoteOutputStream
200
```
201
202
[Stream Servers](./stream-servers.md)
203
204
## Types
205
206
### Core Data Classes
207
208
```kotlin { .api }
209
data class CompileServiceSession(
210
val compileService: CompileService,
211
val sessionId: Int
212
)
213
214
data class DaemonReportMessage(
215
val category: DaemonReportCategory,
216
val message: String
217
)
218
219
class DaemonReportingTargets(
220
val out: PrintStream? = null,
221
val messages: MutableCollection<DaemonReportMessage>? = null,
222
val messageCollector: MessageCollector? = null,
223
val compilerServices: CompilerServicesFacadeBase? = null
224
)
225
```