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.0The 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.
implementation("org.jetbrains.kotlin:kotlin-daemon-client:2.2.0")<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-daemon-client</artifactId>
<version>2.2.0</version>
</dependency>import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient
import org.jetbrains.kotlin.daemon.client.CompilationServices
import org.jetbrains.kotlin.daemon.client.CompileServiceSession
import org.jetbrains.kotlin.daemon.client.DaemonReportingTargetsimport org.jetbrains.kotlin.daemon.client.KotlinCompilerClient
import org.jetbrains.kotlin.daemon.common.*
import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector
import java.io.File
// Set up daemon connection parameters
val compilerId = CompilerId()
val daemonJVMOptions = DaemonJVMOptions()
val daemonOptions = DaemonOptions()
val reportingTargets = DaemonReportingTargets(out = System.out)
// Connect to the daemon
val compileService = KotlinCompilerClient.connectToCompileService(
compilerId = compilerId,
daemonJVMOptions = daemonJVMOptions,
daemonOptions = daemonOptions,
reportingTargets = reportingTargets,
autostart = true
)
if (compileService != null) {
// Compile with the daemon
val messageCollector = PrintingMessageCollector(System.out, null, false)
val compilationResult = KotlinCompilerClient.compile(
compilerService = compileService,
sessionId = CompileService.NO_SESSION,
targetPlatform = CompileService.TargetPlatform.JVM,
args = arrayOf("MyClass.kt"),
messageCollector = messageCollector
)
println("Compilation result: $compilationResult")
}The Kotlin Daemon Client is built around several key components:
Core functionality for finding, connecting to, and managing Kotlin compiler daemon processes. Handles daemon discovery, startup, and connection lifecycle.
object KotlinCompilerClient {
fun connectToCompileService(
compilerId: CompilerId,
daemonJVMOptions: DaemonJVMOptions,
daemonOptions: DaemonOptions,
reportingTargets: DaemonReportingTargets,
autostart: Boolean = true,
checkId: Boolean = true
): CompileService?
fun connectAndLease(
compilerId: CompilerId,
clientAliveFlagFile: File,
daemonJVMOptions: DaemonJVMOptions,
daemonOptions: DaemonOptions,
reportingTargets: DaemonReportingTargets,
autostart: Boolean,
leaseSession: Boolean,
sessionAliveFlagFile: File? = null
): CompileServiceSession?
}Compilation execution through the daemon with support for different target platforms, compiler modes, and incremental compilation workflows.
fun compile(
compilerService: CompileService,
sessionId: Int,
targetPlatform: CompileService.TargetPlatform,
args: Array<out String>,
messageCollector: MessageCollector,
outputsCollector: ((File, List<File>) -> Unit)? = null,
compilerMode: CompilerMode = CompilerMode.NON_INCREMENTAL_COMPILER,
reportSeverity: ReportSeverity = ReportSeverity.INFO,
port: Int = SOCKET_ANY_FREE_PORT,
profiler: Profiler = DummyProfiler()
): Int
data class CompilationServices(
val incrementalCompilationComponents: IncrementalCompilationComponents? = null,
val lookupTracker: LookupTracker? = null,
val compilationCanceledStatus: CompilationCanceledStatus? = null
)Interactive Kotlin compilation and evaluation through the daemon with state management and history tracking.
class KotlinRemoteReplCompilerClient(
protected val compileService: CompileService,
clientAliveFlagFile: File?,
targetPlatform: CompileService.TargetPlatform,
args: Array<out String>,
messageCollector: MessageCollector,
templateClasspath: List<File>,
templateClassName: String,
port: Int = SOCKET_ANY_FREE_PORT
) : ReplCompiler {
fun dispose()
override fun createState(lock: ReentrantReadWriteLock): IReplStageState<*>
override fun check(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCheckResult
override fun compile(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCompileResult
}Server implementations for handling compiler callbacks, incremental compilation services, and communication with the daemon process.
class BasicCompilerServicesWithResultsFacadeServer(
val messageCollector: MessageCollector,
val outputsCollector: ((File, List<File>) -> Unit)? = null,
port: Int = SOCKET_ANY_FREE_PORT
) : CompilerServicesFacadeBase
class CompilerCallbackServicesFacadeServer(
val incrementalCompilationComponents: IncrementalCompilationComponents? = null,
val lookupTracker: LookupTracker? = null,
val compilationCanceledStatus: CompilationCanceledStatus? = null,
// ... additional parameters
port: Int = SOCKET_ANY_FREE_PORT
) : CompilerCallbackServicesFacadeRemote input/output stream handling for daemon communication, enabling data transfer between client and daemon processes.
class RemoteInputStreamServer(
val `in`: InputStream,
port: Int = SOCKET_ANY_FREE_PORT
) : RemoteInputStream
class RemoteOutputStreamServer(
val out: OutputStream,
port: Int = SOCKET_ANY_FREE_PORT
) : RemoteOutputStreamdata class CompileServiceSession(
val compileService: CompileService,
val sessionId: Int
)
data class DaemonReportMessage(
val category: DaemonReportCategory,
val message: String
)
class DaemonReportingTargets(
val out: PrintStream? = null,
val messages: MutableCollection<DaemonReportMessage>? = null,
val messageCollector: MessageCollector? = null,
val compilerServices: CompilerServicesFacadeBase? = null
)