Hi. I have a working Kotlin 1.3.61 project, using ...
# getting-started
j
Hi. I have a working Kotlin 1.3.61 project, using gradle 6.0.1, nothing fancy, simple jvm/jdk11 build. I want to create a few utility scripts that could be used for tooling purposes, nothing that would go to production. (Usually I do it by creating some kotlin file with
main()
inside test source root) So I created the new
script
directory and a
...ws.kts
file inside it, but here I have three issues: • the "use classpath of module:" toolbar menu is not present • script, when run, has working directory set to kotlin home, not project dir • although the build directory from project is present on classpath, the dependencies are not What am I doing wrong?
screenshot from my IDE
m
Assuming you're using Intellij's import Gradle project to setup your project, then it's because IntelliJ doesn't know 'script' is a source directory because it's not defined in your Gradle build. One way would be a script sourceset with appropriate configuration so IntelliJ, and Gradle know about the code, but it doesn't get included in your main/test sourcesets. I don't think adding the source manually in project structure would work as IntelliJ would erase it the next time you did an import. If these scripts are used across projects, you could put them in their own module, and import both modules into your IntelliJ project.
j
would that fix the non-present "use classpath of module" and wrong working directory?
m
I think "use classpath of module" is for scratch files, so that you can add your classes and dependencies to that scratch file. I don't think it's applicable to script files. Until IntelliJ knows that 'script' is a source directory, it won't know what to do with them. Depending on how the scripts are used, you may want to look at kscript. It requires all people on your team to install it, but then I believe you can treat your .kts files like you would bash scripts. Just write them, and other team members can just run them.
j
!! ah, I was mistaking kotlin scratch with kotlin script, thanks a lot
m
And IntelliJ has added 'worksheets' if it's a scratch that you want to share in your project.