0
# Dependency Management
1
2
The dependency management system provides JVM classpath dependency handling including direct file dependencies, ClassLoader-based dependencies, and automatic classpath detection from execution context. It supports flexible dependency resolution for various deployment scenarios.
3
4
## Capabilities
5
6
### JVM Dependency
7
8
Direct classpath dependency representation using file paths to JAR files and directories.
9
10
```kotlin { .api }
11
/**
12
* Represents JVM classpath dependencies using file paths
13
* @param classpath List of JAR files and directories for classpath
14
*/
15
data class JvmDependency(val classpath: List<File>) : ScriptDependency {
16
/**
17
* Alternative constructor for vararg file entries
18
* @param classpathEntries Variable number of File arguments
19
*/
20
constructor(vararg classpathEntries: File)
21
}
22
```
23
24
**Usage Examples:**
25
26
```kotlin
27
import kotlin.script.experimental.jvm.JvmDependency
28
import java.io.File
29
30
// Using list of files
31
val dependency1 = JvmDependency(listOf(
32
File("/path/to/library1.jar"),
33
File("/path/to/library2.jar"),
34
File("/path/to/classes/directory")
35
))
36
37
// Using vararg constructor
38
val dependency2 = JvmDependency(
39
File("/path/to/library1.jar"),
40
File("/path/to/library2.jar")
41
)
42
43
// Adding to script compilation configuration
44
val config = ScriptCompilationConfiguration {
45
dependencies(dependency1, dependency2)
46
}
47
```
48
49
### ClassLoader-Based Dependency
50
51
Dependency resolved from a ClassLoader, enabling dynamic dependency resolution based on runtime context.
52
53
```kotlin { .api }
54
/**
55
* Dependency resolved from a ClassLoader
56
* @param classLoaderGetter Function that provides ClassLoader based on configuration
57
*/
58
class JvmDependencyFromClassLoader(
59
val classLoaderGetter: ClassLoaderByConfiguration
60
) : ScriptDependency {
61
/**
62
* Get the ClassLoader for the given configuration
63
* @param configuration Script compilation configuration
64
* @return ClassLoader containing the dependency classes
65
*/
66
fun getClassLoader(configuration: ScriptCompilationConfiguration): ClassLoader
67
}
68
69
/**
70
* Type alias for function that provides ClassLoader based on configuration
71
*/
72
typealias ClassLoaderByConfiguration = (ScriptCompilationConfiguration) -> ClassLoader
73
```
74
75
**Usage Examples:**
76
77
```kotlin
78
import kotlin.script.experimental.jvm.JvmDependencyFromClassLoader
79
80
// Using current thread context ClassLoader
81
val dependency1 = JvmDependencyFromClassLoader { _ ->
82
Thread.currentThread().contextClassLoader
83
}
84
85
// Using application ClassLoader
86
val dependency2 = JvmDependencyFromClassLoader { _ ->
87
MyApplication::class.java.classLoader
88
}
89
90
// Using configuration-specific ClassLoader
91
val dependency3 = JvmDependencyFromClassLoader { config ->
92
// Determine ClassLoader based on configuration properties
93
when (config.jvm.jvmTarget.value) {
94
"1.8" -> legacyClassLoader
95
else -> modernClassLoader
96
}
97
}
98
99
// Adding to compilation configuration
100
val config = ScriptCompilationConfiguration {
101
dependencies(dependency1, dependency2, dependency3)
102
}
103
```
104
105
### JavaScript Dependency
106
107
Support for JavaScript dependencies in mixed JVM/JS environments.
108
109
```kotlin { .api }
110
/**
111
* JavaScript dependency representation
112
* @param path Path to JavaScript file or module
113
*/
114
data class JsDependency(val path: String) : ScriptDependency
115
```
116
117
**Usage Example:**
118
119
```kotlin
120
import kotlin.script.experimental.jvm.JsDependency
121
122
val jsDependency = JsDependency("/path/to/script.js")
123
124
val config = ScriptCompilationConfiguration {
125
dependencies(jsDependency)
126
}
127
```
128
129
### Dependency Resolution Extensions
130
131
Extension functions for automatic dependency resolution from various sources.
132
133
```kotlin { .api }
134
/**
135
* Configure dependencies from classes in the current classpath context
136
* @param classes Classes to extract classpath from
137
* @param wholeClasspath Whether to include entire classpath or just specific classes
138
*/
139
fun JvmScriptCompilationConfigurationBuilder.dependenciesFromClassContext(
140
vararg classes: KClass<*>,
141
wholeClasspath: Boolean = false
142
)
143
144
/**
145
* Configure dependencies from current execution context
146
* @param wholeClasspath Whether to include entire current classpath
147
*/
148
fun JvmScriptCompilationConfigurationBuilder.dependenciesFromCurrentContext(
149
wholeClasspath: Boolean = false
150
)
151
152
/**
153
* Configure dependencies from a specific ClassLoader
154
* @param classLoader Source ClassLoader for dependency extraction
155
* @param wholeClasspath Whether to extract entire ClassLoader classpath
156
*/
157
fun JvmScriptCompilationConfigurationBuilder.dependenciesFromClassloader(
158
classLoader: ClassLoader,
159
wholeClasspath: Boolean = false
160
)
161
```
162
163
**Usage Examples:**
164
165
```kotlin
166
// Extract dependencies from specific classes
167
val config1 = ScriptCompilationConfiguration {
168
jvm {
169
dependenciesFromClassContext(
170
String::class,
171
ArrayList::class,
172
MyCustomClass::class,
173
wholeClasspath = false
174
)
175
}
176
}
177
178
// Include entire current classpath
179
val config2 = ScriptCompilationConfiguration {
180
jvm {
181
dependenciesFromCurrentContext(wholeClasspath = true)
182
}
183
}
184
185
// Use specific ClassLoader with full classpath
186
val config3 = ScriptCompilationConfiguration {
187
jvm {
188
dependenciesFromClassloader(
189
MyPlugin::class.java.classLoader,
190
wholeClasspath = true
191
)
192
}
193
}
194
```
195
196
### Classpath Utilities
197
198
Utility functions for classpath manipulation and dependency management.
199
200
```kotlin { .api }
201
/**
202
* Create new configuration with updated classpath
203
* @param classpath Collection of files to add to classpath
204
* @return New configuration with updated dependencies
205
*/
206
fun ScriptCompilationConfiguration.withUpdatedClasspath(
207
classpath: Collection<File>
208
): ScriptCompilationConfiguration
209
210
/**
211
* Update classpath in configuration builder
212
* @param classpath Collection of files to add to classpath (null to keep existing)
213
*/
214
fun ScriptCompilationConfiguration.Builder.updateClasspath(
215
classpath: Collection<File>?
216
)
217
218
/**
219
* Update classpath in JVM configuration builder
220
* @param classpath Collection of files to add to classpath (null to keep existing)
221
*/
222
fun JvmScriptCompilationConfigurationBuilder.updateClasspath(
223
classpath: Collection<File>?
224
)
225
```
226
227
**Usage Examples:**
228
229
```kotlin
230
import java.io.File
231
232
// Update existing configuration with additional classpath
233
val originalConfig = ScriptCompilationConfiguration {
234
jvm {
235
dependenciesFromCurrentContext()
236
}
237
}
238
239
val additionalClasspath = listOf(
240
File("/opt/myapp/plugins/plugin1.jar"),
241
File("/opt/myapp/plugins/plugin2.jar")
242
)
243
244
val updatedConfig = originalConfig.withUpdatedClasspath(additionalClasspath)
245
246
// Build configuration with dynamic classpath
247
val dynamicConfig = ScriptCompilationConfiguration {
248
jvm {
249
val runtimeClasspath = discoverRuntimeDependencies()
250
updateClasspath(runtimeClasspath)
251
}
252
}
253
```
254
255
### Legacy Dependency Bridge
256
257
Bridge functionality for compatibility with legacy dependency resolution systems.
258
259
```kotlin { .api }
260
/**
261
* Convert script dependencies to legacy classpath format
262
* @return List of File objects representing classpath entries
263
*/
264
fun List<ScriptDependency>?.toClassPathOrEmpty(): List<File>
265
266
/**
267
* Convert configuration to legacy dependencies format
268
* @param classpath List of classpath files
269
* @return Legacy ScriptDependencies object
270
*/
271
fun ScriptCompilationConfiguration.toDependencies(
272
classpath: List<File>
273
): ScriptDependencies
274
275
/**
276
* Refine configuration with additional dependencies
277
* @return Result containing refined configuration or diagnostics
278
*/
279
fun ScriptCompilationConfiguration.refineWith(
280
/* parameters */
281
): ResultWithDiagnostics<ScriptCompilationConfiguration>
282
```
283
284
**Usage Example:**
285
286
```kotlin
287
// Convert modern dependencies to legacy format
288
val modernConfig = ScriptCompilationConfiguration {
289
dependencies(JvmDependency(File("/path/to/lib.jar")))
290
}
291
292
val legacyDependencies = modernConfig.dependencies.toClassPathOrEmpty()
293
val scriptDependencies = modernConfig.toDependencies(legacyDependencies)
294
295
// Use with legacy scripting APIs
296
val refinedConfig = modernConfig.refineWith(/* additional parameters */)
297
```
298
299
## Integration Patterns
300
301
### Spring Boot Integration
302
303
```kotlin
304
// Configure dependencies for Spring Boot application
305
val springBootConfig = ScriptCompilationConfiguration {
306
jvm {
307
dependenciesFromClassContext(
308
SpringApplication::class,
309
wholeClasspath = true
310
)
311
}
312
}
313
```
314
315
### Plugin System Integration
316
317
```kotlin
318
// Dynamic plugin dependency loading
319
class PluginScriptCompiler {
320
fun createConfigurationForPlugin(pluginClassLoader: ClassLoader) =
321
ScriptCompilationConfiguration {
322
jvm {
323
dependenciesFromClassloader(pluginClassLoader, wholeClasspath = false)
324
// Add core application dependencies
325
dependenciesFromCurrentContext(wholeClasspath = false)
326
}
327
}
328
}
329
```
330
331
### Test Environment Integration
332
333
```kotlin
334
// Test-specific dependency configuration
335
val testConfig = ScriptCompilationConfiguration {
336
jvm {
337
dependenciesFromClassContext(
338
JUnit5::class,
339
TestClass::class,
340
wholeClasspath = false
341
)
342
}
343
}
344
```