Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse - How to add Kotlin to a Tomcat Project - Compile Kotlin AND Java Files

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,

like image 356
Neil Avatar asked Oct 18 '22 09:10

Neil


1 Answers

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.

like image 193
Sergii Avatar answered Oct 24 '22 11:10

Sergii