Eclipse-如何将Kotlin添加到Tomcat项目中-编译Kotlin和Java文件 [英] Eclipse - How to add Kotlin to a Tomcat Project - Compile Kotlin AND Java Files
问题描述
我需要帮助,将Kotlin编译添加到现有的eclipse java tomcat项目中.
I need help adding Kotlin compiling to an existing eclipse java tomcat project.
为了记录,我使用的是Eclipse Oxygen 4.7,并安装了Kotlin插件.我已经成功创建并运行了基于gradle的spring boot Kotlin rest api演示,并且可以编译和运行"Kotlin Only Projects"并使用我的其他Java库-AWESOME.
For the record I am on Eclipse Oxygen 4.7, and have installed the Kotlin plugin. I have successfully created and ran gradle based spring boot Kotlin rest api demo and can compile and run "Kotlin Only Projects" and use my other java libs - AWESOME.
但是现在有了Kotlin,我想将其用于我的其他遗留" Tomcat 8项目,这些项目是Java J2EE-动态WTP ...我想在Kotlin中编写的新servlet.
However now that I have Kotlin I want to use it in work for my other 'legacy' Tomcat 8 projects that are Java J2EE - Dynamic WTP ... new servlets I want to write in Kotlin.
我希望能够在以前的任何软件包的java src文件夹中添加Kotlin类,以便在进行战争时在逻辑上将它们全部在一起.
I want to be able to add a Kotlin class in the java src folder in any of my previous packages so when I build a war it all logically together.
IDE似乎还可以-我在一个包中创建了一个Kotlin类(在tomcat项目的java src包文件夹中),它在解析,导入或依赖方面都没有问题...
The IDE seems ok with this - I created a Kotlin class in a package (in a tomcat project java src package folder) and it have no issues on resolving, imports, or dependency...
我当时在想WOW !,现在可以在WTP中重新启动Tomcat服务器,并且我可以执行我的Kotlin Servlet.否-404404404.
I was thinking WOW!, Ok now to restart the Tomcat server in WTP and I'll be able to execute my Kotlin Servlet. No - 404 404 404.
我检查了WEB-INF/classes构建文件夹以及所有Java类文件...猜我看到了什么?!名为KotlinServlet.kt的文件(那是我的测试servlet的名称,源版本不是编译类版本)
I inspected the WEB-INF/classes build folder and along all the Java class files... guess what I saw?! A file called KotlinServlet.kt ( thats the name of my test servlet, the source version not a compile class version)
因此出于某种原因,构建/package/部署不会费心编译* .kt文件,而只是将其复制过来(也许这是常规WTP部署的默认设置).
So for some reason the build /package/ deployment did not bother to compile the *.kt file and just copied it over (maybe that is the default for general WTP deploy).
我想知道是否可以通过添加Kotlin自然"来解决这个问题,奇怪的是我没有使用Eclipse Kotlin菜单功能.如果我右键单击我的项目,那么我将没有任何Kotlin菜单选项.
I wonder if this would be solved by "Add Kotlin Nature" , the strange part is that none of the eclipse Kotlin menu functions are available to me. If I right click my project I get no Kotlin menu options.
Eclipse指出已安装插件.
Eclipse states the plugin is installed.
所以我的问题是:
考虑到现有的常规WTP Tomcat servlet项目,我们如何才能识别并编译* .kt文件?为什么以及为什么我没有Kotlin菜单功能? (我下载并测试了Oxygen Java和J2EE版本-安装了Kotlin插件-并且那里没有菜单来添加Kotlin Nature.然后,我还尝试了每晚构建的插件.没有菜单出现.显然puglin已安装编译其他Kotlin项目并在编辑器中工作.
根据我的研究-添加Kotlin性质"应该可以完成Java/Kotlin联合编译的技巧,但是菜单功能在任何地方都不可用???
From my research - "Add Kotlin Nature" is supposed to do the trick for joint Java / Kotlin compiling but that menu function is not available anywhere???
如果这是科特林计划"蚀,则编译Kotlin罚款.
If this is a "Kotlin Project" eclipse compiles Kotlin fine.
有什么想法吗?似乎是IntelliJ的阴谋.
Any Ideas? Seems like an IntelliJ conspiracy.
谢谢
推荐答案
您需要添加相应的依赖项.管理项目依赖性的最简单方法是使用构建工具( maven , gradle , Ant + Ivy ...).在Maven示例中,您需要:
You need add correspond dependencies. The easiest way to manage project dependencies is to use build tools (maven, gradle, Ant+Ivy...). In maven example you need:
<properties>
<!-- your properties... and define version fir kotlin -->
<kotlin.version>1.1.4</kotlin.version>
</properties>
<dependencies>
<!-- your project dependencies... and add one for kotlin -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jre8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- your project plugins... and add one for kotlin -->
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
</plugins>
</build>
如果使用IDE,则需要安装kotlin
语言支持插件,以使kotlin
compile
或test run
更加用户友好.
If you use IDE you need to install kotlin
language support plugin to make kotlin
compile
or test run
more user friendly.
这篇关于Eclipse-如何将Kotlin添加到Tomcat项目中-编译Kotlin和Java文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!