Help setting up kotlin in webapp using Maven


#1

Hi everyone!

I’m working on a project where I want to use kotlin to write the Javascript for the UI code.

We are using Java 8 and JEE 7 and maven 3 as the build tool, and below you can see a part of the application structure:

myapp-admin
  src/
  main/
  webapp/
  index.html
  pom.xml  

myapp-admin-kotlin
  src/
  main/
  kotlin/
  Hello.kt

  pom.xml
  target/
  js/
  myapp-admin-kotlin.js
  myapp-admin-kotlin.meta.js
  myapp-admin-kontlin-1.0-SNAPSHOT.jar
  …
  

myapp-admin-kotlin/pom.xml:

<?xml version=“1.0” encoding=“UTF-8”?>
<project xmlns=“http://maven.apache.org/POM/4.0.0
  xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance
  xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd>
  <modelVersion>4.0.0</modelVersion>

  …
  <packaging>jar</packaging>
  <artifactId>myapp-admin-kotlin</artifactId>

    <build>
  <sourceDirectory>src/main/kotlin</sourceDirectory>
  <testSourceDirectory>src/test/kotlin</testSourceDirectory>
  <outputDirectory>{project.build.directory}/js&lt;/<span>outputDirectory</span>&gt; &nbsp;&nbsp;&lt;<span>plugins</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&lt;<span>plugin</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&lt;<span>artifactId</span>&gt;kotlin-maven-plugin&lt;/<span>artifactId</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&lt;<span>groupId</span>&gt;org.jetbrains.kotlin&lt;/<span>groupId</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&lt;<span>version</span>&gt;{kotlin.version}</version>
           <executions>
                   <execution>
                   <id>js</id>
                   <goals>
                           <goal>js</goal>
                   </goals>
                   </execution>
           </executions>
           </plugin>
  </plugins>
  </build>
  <dependencies>
  <dependency>
           <groupId>org.jetbrains.kotlin</groupId>
           <artifactId>kotlin-stdlib</artifactId>
           <version>{kotlin.version}&lt;/<span>version</span>&gt; &nbsp;&nbsp;&lt;/<span>dependency</span>&gt; &nbsp;&nbsp;&lt;<span>dependency</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&lt;<span>groupId</span>&gt;org.jetbrains.kotlin&lt;/<span>groupId</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&lt;<span>artifactId</span>&gt;kotlin-js-library&lt;/<span>artifactId</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&lt;<span>version</span>&gt;{kotlin.version}</version>
  </dependency>
  </dependencies>
</project>


myapp-admin/pom.xml:

<?xml version=“1.0” encoding=“UTF-8”?>
<project xmlns=“http://maven.apache.org/POM/4.0.0
  xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance
  xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd>
  …

  <packaging>war</packaging>

  <artifactId>myapp-admin</artifactId>

  <build>
  <!-- Set the name of the war, used as the context root when the app
           is deployed -->
  <finalName>{project.artifactId}&lt;/<span>finalName</span>&gt; &nbsp;&nbsp;&lt;<span>plugins</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&lt;<span>plugin</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&lt;<span>artifactId</span>&gt;maven-war-plugin&lt;/<span>artifactId</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&lt;<span>version</span>&gt;{version.war.plugin}</version>
           <configuration>
                   <!-- Java EE 7 doesn’t require web.xml, Maven needs to catch up! -->
                   <failOnMissingWebXml>false</failOnMissingWebXml>                   
           </configuration>
           </plugin>
           <!-- WildFly plugin to deploy war -->
           <plugin>
           <groupId>org.wildfly.plugins</groupId>
           <artifactId>wildfly-maven-plugin</artifactId>
           <version>{version.wildfly.maven.plugin}&lt;/<span>version</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&lt;/<span>plugin</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;<span>&lt;!-- Compiler plugin enforces Java 1.6 compatibility and activates </span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;annotation processors --&gt; </span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span>&lt;<span>plugin</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&lt;<span>artifactId</span>&gt;maven-compiler-plugin&lt;/<span>artifactId</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&lt;<span>version</span>&gt;{version.compiler.plugin}</version>
           <configuration>
                   <source>{maven.compiler.source}&lt;/<span>source</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&lt;<span>target</span>&gt;{maven.compiler.target}</target>
           </configuration>
           </plugin>
  </plugins>
  </build>

  <dependencies>
  …
  <dependency>
           <groupId>no.logiq.babel</groupId>
           <artifactId>imlink-babel-admin-kotlin</artifactId>
           <version>${project.parent.version}</version>
           <scope>compile</scope>
  </dependency>
  …
  </dependencies>
</project>


As you can see above, I’ve separated the Kotlin kode in a separate module since the kotlin compilator was complaining about some of my java dependencies was not compatible with Kotlin (or something like that).

I’m struggling to know how to setup a traditonal webapp with Kotlin using Maven.

When I compile the project using IntelliJ I get the following error:
Error:Kotlin: Path '…/myapp-admin/lib/kotlin-jslib.jar’does not exist

However, maven builds successfully:

[INFO] ------------------------------------------------------------------------
[INFO] Building myapp-admin-kotlin 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for org.slf4j:slf4j-api:jar:1.7.2.jbossorg-1 is missing, no dependency information available
[INFO]
[INFO] — maven-resources-plugin:2.5:resources (default-resources) @ imlink-babel-admin-kotlin —
[debug] execute contextualize
[INFO] Using ‘UTF-8’ encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/joerunds/Code/myapp-project/trunk/myapp-admin-kotlin/src/main/resources
[INFO]
[INFO] — maven-compiler-plugin:3.1:compile (default-compile) @ imlink-babel-admin-kotlin —
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] — kotlin-maven-plugin:0.11.91.1:js (js) @ imlink-babel-admin-kotlin —
[info] Kotlin Compiler version 0.11.91.1
[info] Compiling Kotlin sources from [/Users/joerunds/Code/myapp-project/trunk/myapp-admin-kotlin/src/main/kotlin]
[warn] artifact org.jetbrains.kotlin:kotlin-stdlib:jar:0.11.91.1:compile is not a Kotlin Javascript Library
[warn] artifact org.jetbrains.kotlin:kotlin-runtime:jar:0.11.91.1:compile is not a Kotlin Javascript Library
[info] libraryFiles: [/Users/joerunds/.m2/repository/org/jetbrains/kotlin/kotlin-js-library/0.11.91.1/kotlin-js-library-0.11.91.1.jar]
[debug] Output:
/Users/joerunds/Code/imlink-babel-project/trunk/myapp-admin-kotlin/target/js/imlink-babel-admin-kotlin.js
Sources:
/Users/joerunds/Code/imlink-babel-project/trunk/myapp-admin-kotlin/src/main/kotlin/Hello.kt
[INFO]
[INFO] — maven-resources-plugin:2.5:testResources (default-testResources) @ imlink-babel-admin-kotlin —
[debug] execute contextualize
[INFO] Using ‘UTF-8’ encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/joerunds/Code/myapp-project/trunk/myapp-admin-kotlin/src/test/resources
[INFO]
[INFO] — maven-compiler-plugin:3.1:testCompile (default-testCompile) @ myapp-admin-kotlin —
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] — maven-surefire-plugin:2.18.1:test (default-test) @ myapp-admin-kotlin —
[INFO] Tests are skipped.
[INFO]
[INFO] — maven-jar-plugin:2.3.2:jar (default-jar) @ imlink-babel-admin-kotlin —
[INFO] Building jar: /Users/joerunds/Code/myapp-project/trunk/myapp-admin-kotlin/target/myapp-admin-kotlin-1.0-SNAPSHOT.jar


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.726s
[INFO] Finished at: Mon Apr 27 14:59:17 CEST 2015
[INFO] Final Memory: 16M/312M
[INFO] ------------------------------------------------------------------------

I’m also wondering which files from the kotlin compilation but I include in the webapp war and finally reference from the index.html?

Some kind of help or direction is most appreciated.
Thanks!

Jørund


#2

I've finally managed after some guidance from Hadi Hariri: https://devnet.jetbrains.com/thread/462657?tstart=0

And, see example project here:
https://github.com/jvskriubakken/kotlin-js