简体   繁体   English

在 IntelliJ IDEA 中,创建和执行依赖于我的项目代码的 kotlin 脚本 (.kts) 的正确方法是什么?

[英]In IntelliJ IDEA, what is the proper way to create and execute kotlin scripts (.kts) that depends on code in my project?

I have a simple IntelliJ IDEA kotlin project with standard directory layout, ie:我有一个具有标准目录布局的简单 IntelliJ IDEA kotlin 项目,即:

src/
  main/
    kotlin/
      ClassA.kts
      ClassB.kts
    resources/
  test/
    kotlin/
      TestClassA.kts
      TestClassB.kts

In this project I want to use kotlin scripts (.kts files) and I want to use IntelliJ to edit and execute these scripts.在这个项目中,我想使用 kotlin 脚本(.kts 文件),我想使用 IntelliJ 来编辑和执行这些脚本。

Inside the scripts I need to use classes, functions etc. defined in main , like ClassA (using stuff from test would also be nice).在脚本中,我需要使用在main中定义的类、函数等,比如ClassA (使用来自test的东西也很好)。 To aid in writing the scripts, I would like to get code completion and other coding assistance from IntelliJ.为了帮助编写脚本,我想从 IntelliJ 获得代码完成和其他编码帮助。

Questions:问题:

  • Where should I put the .kts files?我应该把.kts文件放在哪里?
  • How do I specify that the script(s) should use my project's classpath when executing scripts?如何指定脚本在执行脚本时应使用我的项目的类路径?
  • How do I make IntelliJ provide code completion in the script file?如何让 IntelliJ 在脚本文件中提供代码补全?
  • To me it looks like only kotlin scratch files and kotlin worksheet files work properly (see https://kotlinlang.org/docs/run-code-snippets.html ).在我看来,只有 kotlin 临时文件和 kotlin 工作表文件可以正常工作(请参阅https://kotlinlang.org/docs/run-code-snippets.html )。
  • What I want is proper scripts that are stored along the other source code in the project, and a way to run them either from IntelliJ run configurations or from the terminal.我想要的是与项目中的其他源代码一起存储的适当脚本,以及一种从 IntelliJ 运行配置或终端运行它们的方法。

I use IntelliJ IDEA 2022.3.1 / kotlin plugin 223-1.7.21-release-272-IJ8214.52.我使用 IntelliJ IDEA 2022.3.1 / kotlin 插件 223-1.7.21-release-272-IJ8214.52。

See also the IntelliJ issue tracker: https://youtrack.jetbrains.com/issue/KT-55833/Kotlin-scripting-should-have-more-documentation-and-it-should-be-easier-to-execute-scripts-from-the-command-line另请参阅 IntelliJ 问题跟踪器: https://youtrack.jetbrains.com/issue/KT-55833/Kotlin-scripting-should-have-more-documentation-and-it-should-be-easier-to-execute-scripts -从命令行

The point of .main.kts is that it's simple and doesn't require a build system. .main.kts的要点在于它很简单并且不需要构建系统。 So depending on a project which is built with Maven/Gradle/Bazel is only possible through dependencies.因此,依赖于使用 Maven/Gradle/Bazel 构建的项目只能通过依赖项实现。

"IntelliJ IDEA kotlin project" sounds like you might not even have a build system which pretty much rules out running from command line, even if direct linking was possible. “IntelliJ IDEA kotlin 项目”听起来你甚至可能没有一个构建系统,它几乎排除了从命令行运行的可能性,即使直接链接是可能的。 In the next sections I'll assume Gradle, because that's what I'm familiar with, but equivalents could work otherwise in other systems.在接下来的部分中,我将假定为 Gradle,因为这是我所熟悉的,但等效项在其他系统中可以以其他方式工作。

Before I go down the rabbit hole, I would like to call XYProblem , just because you might not need a.kts, a simple main.kt with fun main() could work as well.在我进入兔子洞 go 之前,我想调用XYProblem ,只是因为您可能不需要 a.kts,一个带有fun main()的简单main.kt也可以工作。 And then running with java -jar on the resulting app or gradlew:module:run --args "..." would just work normally without any special treatment.然后在生成的应用程序上运行java -jargradlew:module:run --args "..."将正常工作而无需任何特殊处理。 Oddly enough just today I read this article which describes in detail how to make a small easily-runnable Kotlin app.奇怪的是,就在今天,我阅读了这篇文章,其中详细描述了如何制作一个易于运行的小型 Kotlin 应用程序。

One option is that you use @file:Import , but I haven't gotten it to work, and it seems others neither .一种选择是您使用@file:Import ,但我还没有让它工作,而且似乎其他人也没有

<rabbithole> So the only workable idea I have for this is that you have your library publish a jar to local repository ( Gradle mavenLocal() ) or import from a local build folder ( flatDir or with maven { url = file:// ). <rabbithole>所以我唯一可行的想法是让你的图书馆将 jar 发布到本地存储库 ( Gradle mavenLocal() ) 或从本地构建文件夹导入( flatDir 或使用maven { url = file:// ) . Note that these are Gradle APIs, which means that Gradle can publish to these in some way.请注意,这些是 Gradle API,这意味着 Gradle 可以通过某种方式发布到这些 API。

At this point we have a.jar file of your library in src/main/kotlin on the local file system.此时我们在本地文件系统上的src/main/kotlin中有一个你的库的 .jar 文件。 .main.kts files can import JARs from Maven repositories ( example ). .main.kts文件可以从 Maven 存储库导入 JARs( 示例)。 If you're lucky this built-in @file:Repository also supports local file:// URIs, so it "just works".如果幸运的话,这个内置的@file:Repository也支持本地file:// URI,所以它“可以正常工作”。

Note: location of .main.kts files don't matter, only their file name, which makes a huge difference.注意: .main.kts文件的位置无关紧要,重要的是它们的文件名,这会产生巨大的差异。

The developer experience with this solution would be:开发人员使用此解决方案的经验是:

  • write code in src/main/kotlinsrc/main/kotlin中编写代码
  • write unit tests in src/test/kotlinsrc/test/kotlin中编写单元测试
  • build建造
  • refresh classpath of app.main.kts after build构建后刷新app.main.kts的类路径
  • code as normal because code completion works (navigation might not)代码正常,因为代码完成工作(导航可能不工作)

</rabbithole> , notice that the above is pretty much the same as having a main.kt file in your app, with the difference that main.kt is fully supported and you can do things like pre-compilation, minification, packaging. </rabbithole> ,请注意上面的内容与您的应用程序中有一个main.kt文件几乎相同,不同之处在于main.kt是完全支持的,您可以执行预编译、缩小、打包等操作。


You can find some .main.kts examples here: https://github.com/TWiStErRob/TWiStErRob-env/search?q=filename%3Amain.kts , but they're not using local code, all single-file only with mavenCentral() dependencies.您可以在此处找到一些.main.kts示例: https://github.com/TWiStErRob/TWiStErRob-env/search?q=filename%3Amain.kts ,但它们不使用本地代码,所有单文件仅包含mavenCentral()依赖项。

The workaround that I currently use is to put .kts files inside the source root, ie somewhere inside src/main/kotlin or src/test/kotlin .我目前使用的解决方法是将.kts文件放在源根目录中,即src/main/kotlinsrc/test/kotlin中的某处。

This works pretty well:这很好用:

  • code completion works代码完成工作
  • script can use dependencies脚本可以使用依赖项
  • script can use functions/classes from the project脚本可以使用项目中的函数/类
  • running and debugging from inside IntelliJ works从 IntelliJ 内部运行和调试工作

But I get the following warning in IntelliJ:但是我在 IntelliJ 中收到以下警告:

This script is not supposed to be inside source root.此脚本不应位于源根目录中。 After Kotlin 1.9 it will be ignored during the module compilation. Kotlin 1.9以后模块编译时会忽略。

在此处输入图像描述

The 'More info' link is https://youtrack.jetbrains.com/issue/KT-52735 . “更多信息”链接是https://youtrack.jetbrains.com/issue/KT-52735

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IntelliJ IDEA Ultimate 2018.3 认为我的 Java 9 项目是 Kotlin 项目 - IntelliJ IDEA Ultimate 2018.3 thinks my Java 9 Project is a Kotlin Project 我应该如何使用 Intellij Idea 创建我的 Kotlin 多模块项目? - How should I create my Kotlin multi-module project with Intellij Idea? 为什么 Intellij 不为新的 kotlin 项目创建 build.gradle.kts? - Why is Intellij not creating a build.gradle.kts for a new kotlin project? 我正在尝试使用 intellij idea 创建一个 kotlin 项目但无法在 src 文件下创建 kotlin 文件 - I am trying to create a kotlin project with intellij idea But can't create kotlin file under src file IntelliJ IDEA 未在 Kotlin 主脚本 (main.kts) 的调试中显示变量 - IntelliJ IDEA Not Showing Variables In Debug For Kotlin Main Script (main.kts) 当我只想执行 kotlin 脚本时,为什么 IntelliJ IDEA 正在构建 maven 项目? - Why IntelliJ IDEA is building the maven project when I only want to execute a kotlin script? 在我的 build.gradle.kts 文件中将一些代码片段从 groovy 转换为 kotlin KTS - Converting some code snippet from groovy to kotlin KTS in my build.gradle.kts file 如何在 IntelliJ IDEA 项目的 build.gradle.kts 中添加 ViewModel 实现? - How to add ViewModel implementation in build.gradle.kts for IntelliJ IDEA project? IntelliJ中两个JVM Kotlin“创建项目”选项有什么区别? - What is the difference in the two JVM Kotlin “create project” options in IntelliJ? Intellij Idea插件,创建Kotlin PSI访问者 - Intellij Idea Plugin, Create Kotlin PSI Visitor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM