# How to add compiler plugin with scripting

**URL:** <https://discuss.kotlinlang.org/t/how-to-add-compiler-plugin-with-scripting/25451>\
**Category:** Support\
**Created:** [August 14, 2022, 6:16am UTC](https://discuss.kotlinlang.org/t/how-to-add-compiler-plugin-with-scripting/25451 "2022-08-14T06:16:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![WayZer](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@WayZer](https://discuss.kotlinlang.org/u/WayZer)\
**Post date:** [August 14, 2022, 6:16am UTC](https://discuss.kotlinlang.org/t/how-to-add-compiler-plugin-with-scripting/25451/1 "2022-08-14T06:16:24Z")

</div>

I am writing scripting host, but can’t find a api to add compiler plugin.

How to add one like `kotlinx.serialization` or kapt.  
Is it able to config custom plugin using annotation like `@file:DependsOn`

---

<div class="post-metadata">

**Author:** ![LSafer](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lsafer/32/9366_2.png) [@LSafer](https://discuss.kotlinlang.org/u/LSafer)\
**Post date:** [August 16, 2022, 2:16pm UTC](https://discuss.kotlinlang.org/t/how-to-add-compiler-plugin-with-scripting/25451/2 "2022-08-16T14:16:53Z")

</div>

First, you should define know what kind of plugin you need

- You might need a `gradle` plugin to work with gradle (the easiest and most flexible)
- You might need a `kotlin compiler` plugin to work with the compiler
- You might need an annotation processor if you want to only work with annotations (it has some limitations)
- You might need an IDE plugin if you want special syntax highlighting

Every one of these has its own manual and can be searched by its name.  
But the thing is, you must first determine what you want to do and where  
you want it to be done. (And if it is worth it or not)

---

<div class="post-metadata">

**Author:** ![WayZer](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@WayZer](https://discuss.kotlinlang.org/u/WayZer)\
**Post date:** [September 2, 2022, 1:42am UTC](https://discuss.kotlinlang.org/t/how-to-add-compiler-plugin-with-scripting/25451/3 "2022-09-02T01:42:12Z")

</div>

I say `compiler plugin` or `kotlin compiler plugin`.

And my question is about `kotlin scripting`, I can’t find any doc about that.

---

<div class="post-metadata">

**Author:** ![Hronom](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/hronom/32/9449_2.png) [@Hronom](https://discuss.kotlinlang.org/u/Hronom)\
**Post date:** [August 13, 2024, 1:14am UTC](https://discuss.kotlinlang.org/t/how-to-add-compiler-plugin-with-scripting/25451/4 "2024-08-13T01:14:07Z")

</div>

Any solution so far? For Kotlin scripting

---

<div class="post-metadata">

**Author:** ![Hronom](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/hronom/32/9449_2.png) [@Hronom](https://discuss.kotlinlang.org/u/Hronom)\
**Post date:** [August 13, 2024, 1:29am UTC](https://discuss.kotlinlang.org/t/how-to-add-compiler-plugin-with-scripting/25451/5 "2024-08-13T01:29:25Z")

</div>

Here some workarounds [https://youtrack.jetbrains.com/issue/KT-47384](https://youtrack.jetbrains.com/issue/KT-47384)

Please vote for it too

---

<div class="post-metadata">

**Author:** ![Hronom](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/hronom/32/9449_2.png) [@Hronom](https://discuss.kotlinlang.org/u/Hronom)\
**Post date:** [August 13, 2024, 1:40am UTC](https://discuss.kotlinlang.org/t/how-to-add-compiler-plugin-with-scripting/25451/6 "2024-08-13T01:40:46Z")

</div>

**Offtopic**

In my case I switched to Jackson for serialization.  
Example:

```auto
#!/usr/bin/env kotlin

@file:DependsOn("com.fasterxml.jackson.core:jackson-databind:2.17.2")
@file:DependsOn("com.fasterxml.jackson.module:jackson-module-kotlin:2.17.2")

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.fasterxml.jackson.module.kotlin.registerKotlinModule

data class User(val name: String, val age: Int, val email: String)

val jsonString = """{"name":"John Doe","age":30,"email":"johndoe@example.com"}"""

val mapper = jacksonObjectMapper().apply {
    registerKotlinModule()
}

// Deserialize JSON to a Kotlin object
val user: User = mapper.readValue(jsonString)
println("Deserialized: $user")

// Serialize Kotlin object to JSON
val serializedUser = mapper.writeValueAsString(user)
println("Serialized: $serializedUser")

```
