使用 Kotlin 开发 Eclipse 插件

use*_*459 5 java eclipse eclipse-plugin maven kotlin

是否可以使用 Kotlin 为 Eclipse 开发插件?我正在从事的项目使用 Maven Tycho 和 OSGi。

为了防止误解,这不是关于Kotlin Eclipse Plugin,而是关于使用 Kotlin 语言开发自定义插件。

我在我的 中添加了对 Kotlin stdlib 的依赖pom.xml,但这似乎还不够。

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-stdlib</artifactId>
    <version>${kotlin.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

由于有很多潜在的相关文件 ( MANIFEST.MF, pom.xml, ...) 我认为链接 GitHub 存储库可能更容易:https : //github.com/Tornac/slr-toolkit/tree/kotlin/plugins/de .tudresden.slr .问卷

只有其中的代码plugins/de.tudresden.slr.questionnaire是我的,我只关心将 Kotlin 代码添加到该特定插件中。可以在kotlin分支上找到我集成 Kotlin 的尝试。所有更改都可以在此提交中找到:https : //github.com/Tornac/slr-toolkit/commit/18f8f6de3a35644ef1ae595024667f4bdd10b604

在编译过程中,简要显示以下错误: ERROR: Cannot access built-in declaration 'kotlin.Unit'. Ensure that you have a dependency on the Kotlin standard library (5, 14).

科特林:

// file: KtTest
package de.tudresden.slr.questionnaire

class KtTest {
    fun doStuff() {
        System.out.println("hello world, this is kt!");
    }
}
Run Code Online (Sandbox Code Playgroud)

爪哇:

// trying to call KtTest's method from Java
new KtTest().doStuff();
Run Code Online (Sandbox Code Playgroud)

调用 doStuff() 引起的堆栈跟踪:

java.lang.NoClassDefFoundError: de/tudresden/slr/questionnaire/KtTest
    at de.tudresden.slr.questionnaire.QuestionnaireView$1.mouseDown(QuestionnaireView.java:78)
    at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:192)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4362)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1113)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4180)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3769)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$4.run(PartRenderingEngine.java:1127)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1018)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:156)
    at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:694)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:606)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)
    at de.tudresden.slr.app.Application.start(Application.java:20)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:380)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:235)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:669)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:608)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1515)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1488)
Caused by: java.lang.ClassNotFoundException: de.tudresden.slr.questionnaire.KtTest cannot be found by de.tudresden.slr.questionnaire_0.2.6.qualifier
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:439)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:352)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:344)
    at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:160)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 29 more
Run Code Online (Sandbox Code Playgroud)

小智 1

一般来说,是可以的。但是,它似乎仅适用于使用 JDK 8 的旧版本 Eclipse。

我无法真正评论 Maven 构建,但在没有 Maven 插件的情况下直接从 Eclipse 构建时,我遇到了类似的问题。我做了相当广泛的研究来找到问题的根源。特别是,在创建新的插件项目时,我使用 Eclipse 提供的插件模板创建了一个最小的设置。从模板创建这样一个项目,添加 Kotlin 性质并将其中一个类移植到 Kotlin 可靠地生成java.lang.NoClassDefFoundError,而 IDE 不会抱怨任何错误。

查看 Kotlin 插件的开发人员文档,他们建议使用基于 Eclipse Oxygen 和 JDK 8 的设置。事实上,我发现这适用于我的最小插件设置。我做了更多测试,发现适合我的设置的最新 Eclipse 版本是带有 JDK 8 的 Eclipse 2020-06。较新版本的 Eclipse 需要 JDK 11 并显示错误java.lang.NoClassDefFoundError。我还尝试了使用 JDK 11 的 Eclipse 2020-06,也显示了相同的错误。

请注意,对于 Eclipse 中的插件开发来说,这似乎根本不是问题。这似乎是 Kotlin 插件以及开发混合 Java 和 Kotlin 应用程序的一个基本问题。请参阅此处我的问题:Is ist possible to build mix Kotlin and Java applications with a recent Eclipse and JDK11?