Eclipse - How to add Kotlin to a Tomcat Project -

2020-07-17 06:08发布

问题:

I need help adding Kotlin compiling to an existing eclipse java tomcat project.

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.

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.

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.

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...

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.

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)

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).

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 states the plugin is installed.

So my question is:

Given an existing conventional WTP Tomcat servlet project, HOW can we get it to identify and a compile *.kt files? How and why do I not have the Kotlin menu functions? (I downloaded and tested both the Oxygen Java and J2EE version - installed the Kotlin plugin - and no menus there either to add a Kotlin Nature. I also then tried the nightly build of the plugin. No menus appeared. Obviously the puglin is installed compiling other Kotlin projects and works in the editor.

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???

If this is a "Kotlin Project" eclipse compiles Kotlin fine.

Any Ideas? Seems like an IntelliJ conspiracy.

Thanks,

回答1:

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>

If you use IDE you need to install kotlin language support plugin to make kotlin compile or test run more user friendly.



回答2:

OK I am narrowing in on this. I found that the kotlin classes are being compiled to this location under runtime lib? Maybe this will be something I can figure to build to my normal classes folder.



回答3:

OK - To get all the Kotlin menus - turns out you need to be on PACKAGE EXPLORER tab not PROJECT EXPLORER . arggh just found that by fluke.

The creation of a .kt file will automatically add the runtime and libs for Kotlin to your project.

Now on the the issue of the build / compile now. Still outstanding.



回答4:

OK Update - Downloaded IntelliJ, and "IT" works out of the box, in 15 min I had a Hybrid Java/Kotlin Tomcat Project running.

Still Interested in the Eclipse solution but I am probably just going to shell out the cash for IntelliJ. Time is money.