I have a Groovy DSL script which uses classes from libraries, for example org.joda.time.LocalDateTime.
In the actual Groovy DSL script I don't have the import to the class, it's added later when executing the script.
I wrote a GDSL script for code completion in IntelliJ, but IntelliJ doesn't recognize LocalDateTime class in my script.
When I try to do an import to that class in the GDSL script, I get an error saying that the class cannot be resolved, even though it's on the project's classpath, cause when I run the project's tests for example, it works.
How can I make IntelliJ see classes from external libraries in my DSL script using GDSL script?
Related
i use intellij with junit to run tests (new), when i debug my test code for practice, one of the very first lines of code that seems to run is some method called main from package called com.intellij.rt.junit. I've tried clicking on it as well as searching for it open that package or class to see what's inside but i can't find it all. anyone know what it is or where i could find it in intellij and why i can't just access it from the debugger like every other class? i have junit 5.8 installed in maven.
y.bedrov's analysis is quite right.
com.intellij.rt.junit.JUnitStarter is part of IDE. In your case, the IDE is IntelliJ IDEA. You may find the source code in this local path:
/Applications/IntelliJ IDEA.app/Contents/plugins/junit/lib/junit-rt.jar
then import the jar package into your own project to read the source code conveniently.
Kotlin script (.main.kts) files have the idea of providing executable Kotlin code in ONE single standalone file, which is immensely convenient for scripting or when sharing code snippets on StackOverflow for example. In contrast to that, currently almost all Java/Kotlin uses a build system (e.g. gradle) with cryptic build files and a deep folder structure.
While I like the Kotlin script idea a lot, it seems to be barely used, with only 22 questions on StackOverflow and extremely sparse documentation and precious few Google results. I am able to pull in dependencies using #file:DependsOn inside of the actual script rather than the traditional build file:
build.gradle:
dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.0'
}
foo.main.kts:
#file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.0")
However, I can't find a way to use "apply plugin" in my .main.kts file. It's not used in any of the code snippets I found online.
build.gradle:
apply plugin: 'kotlinx-serialization'
foo.main.kts:
???
For reference, I attached an MWE below. The error message says the class Node is not serializable, but as pointed out in this question that message is misleading and the actual issue that apply plugin is missing, which I do not know how to use outside of a build.gradle file:
#file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.0")
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
#Serializable
data class Node (val numbers: List<Int>)
val h = Json.decodeFromString<Node>(""" {"numbers": [1, 2, 3]} """)
Run it on Ubuntu:
snap install kotlin
kotlin foo.main.kts
kotlinx-serialization is a Gradle plugin, which adds to pipeline same-named compiler plugin - it generates the serializer() method for classes annotated with #Serializable.
When you compile Kotlin code with kotlinc compiler, you can attach the plugin by providing the path to its JAR file (it's bundled with the compiler) using the -Xplugin=/snap/kotlin/current/lib/kotlinx-serialization-compiler-plugin.jar compiler option.
For .kts files, there is a #file:CompilerOptions annotation, but currently (in Kotlin 1.5.10) this particular key is not supported (warning: the following compiler arguments are ignored on script compilation: -Xplugin)
Command line
On the command line you may use
kotlinc -script -Xplugin="/snap/kotlin/current/lib/kotlinx-serialization-compiler-plugin.jar" foo.main.kts
Script header
As a workaround you may use this shebang:
#!/usr/bin/env -S kotlinc -script -Xplugin="/snap/kotlin/current/lib/kotlinx-serialization-compiler-plugin.jar"
To run your script you need to turn it into an executable:
chmod u+x foo.main.kts
Now it could be run with:
./foo.main.kts
Is it possible to import classes from kotlinx package in a simple Kotlin script?
myscript.kts:
import kotlinx.serialization.*
import kotlinx.serialization.json.*
println("")
Running the above script with kotlinc -script myscript.kts gives this error:
error: unresolved reference: kotlinx
When I checked kotlinc/lib/ directory, there exists kotlinx-coroutines-core.jar and so on.
I am using Kotlin compiler version 1.4.0.
You can execute kotlin like a simple bash script with dependencies if you change the name to *.main.kts
https://raw.githubusercontent.com/yschimke/okurl-scripts/master/commands/tube-status.main.kts
$ ./tube-status.main.kts
Good Service
Good Service
Minor Delays
Part Closure
Minor Delays
Good Service
Minor Delays
Good Service
Part Closure
Good Service
Planned Closure
First of, the imports you have are related to the kotlin serialization library not the coroutines one.
As for running your script you need to specify the dependencies to the compiler. You can run your script like this :
kotlinc -script myscript.kts -classpath kotlinx-serialization-runtime-0.20.0-1.4.0-rc-95.jar
I've used the old kotlin serialization runtime but you'll need to adapt the command to your needs.
I am trying to run .kts scripts (using Script Engine) from a Kotlin program.
The Kotlin scripts reside in the resource folder of the project and normally they have support for auto-completion, IDE checks, auto-complete, etc.
The problem appeared when I wanted to pass external variables to the script using bindings. I am doing something like:
scriptEngine.eval(scriptContent, scriptEngine.createBindings().apply { putAll(bindings) })
The bindings is a Map<String, Any> that contains the variables I want to access in my script: bindings["variable"].
Problem is that IntellIJ doesn't recognize bindings in the .kts script, so it shows a "unresolved reference: bindings". If I run the script from command line everything is fine, but inside IntelliJ i have this problem.
Is there a way to overcome the issue ? Has any experienced the same problem?
I have a project in IntelliJ that has a number of syntactically correct and incorrect classes and a unit test that I've written for a particular class.
Is there a way to execute the test even though other classes, completely independent from the current class I want to test, have errors that prevent the entire project from compiling?
The class I want to test has no compile-time errors. When I try to run the test, I'm directed to those other classes with syntax errors.
Replace the Before launch Build step with Build, no error check:
Do this change for the default JUnit configuration (under Defaults node in the tree on the left) so that it's applied to all the new JUnit run/debug configurations automatically.
You can also remove the Build step and compile the test files manually (from the file right click context menu):