Configuring the plugin

After installing the plugin, I get an entry in the project view for "New Kotlin file". Selecting this, and pasting some example code, still causes the calls to system libraries to be unresolved.

In the comments of the Kotlin blog, it’s mentioned that the plugin will set up the required dependencies automatically.
However, for me this does not happen. Perhaps this is caused by the current project being a Maven-based one?

First thing to check is your IntelliJ IDEA version. It should be the latest EAP of 11.1. Is it?

BTW: do you have anything like the “Configuring Kotlin Runtime…” bar appear at the top of your editor when opening a Kotlin file?
See this video:
http://www.youtube.com/watch?feature=player_embedded&v=0jBPcIPVihI

It works when adding a Kotlin file to a blank Java module, created from scratch.

It does not work when adding a Kotlin file to an existing Maven module.
While the file gets created, the editor popup does not appear.

Not a big deal at this point in time though :slight_smile:

I'm interested in this question, too.  

I start with a Maven project, I add Evgeny Goldin’s plugin

http://evgeny-goldin.com/wiki/Kotlin-maven-plugin

to the existing POM, I create manually src/[main,test]/kotlin, and add a Kotlin file to src/main/kotlin that uses stdlib (say, println()).  I’m including my POM as a reference.  It seems like we should add the kotlin runtime as at least a Maven “provided” scope dependency.  So I did this by doing a manual install-jar operation within maven to install the kotlin runtime to the local repository.  Then add the runtime as a dependency – I used version 1.0 for the runtime because it was easy to type :slight_smile:

I also have a Java main class defined that instantiates a Kotlin class which contains a function that prints “hello”.  This much works for me when I run the app from within the IDE using App#main as an entry point.

I do notice that once in a while, IJ will unmark src/[main,test]/kotlin as source roots.  Not sure what is going on there.  

Here is my POM:

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

  <groupId>org.petrovic</groupId>
  <artifactId>mailchimp-kotlin</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>mailchimp-kotlin</name>
  <url>http://maven.apache.org</url&gt;

  <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencyManagement>
  <dependencies>
           <dependency>
           <groupId>net.mailjimp</groupId>
           <artifactId>mailjimp-core</artifactId>
           <version>0.4-SNAPSHOT</version>
           </dependency>
           <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.8.2</version>
           <scope>test</scope>
           </dependency>
  </dependencies>
  </dependencyManagement>

  <build>
  <pluginManagement>
           <plugins>
           <plugin>
                   <groupId>com.goldin.plugins</groupId>
                   <artifactId>kotlin-maven-plugin</artifactId>
                   <version>0.2.3.8-beta-6</version>
                   <executions>
                   <execution>
                           <id>compile-kotlin-sources</id>
                           <goals>
                           <goal>compile</goal>
                           <goal>testCompile</goal>
                           </goals>
                           <configuration>
                           <src>${project.basedir}/src/main/kotlin</src>
                           <output>${project.build.outputDirectory}</output>
                           </configuration>
                   </execution>
                   </executions>
           </plugin>
           </plugins>
  </pluginManagement>
  <plugins>
           <plugin>
           <groupId>com.goldin.plugins</groupId>
           <artifactId>kotlin-maven-plugin</artifactId>
           </plugin>
  </plugins>
  </build>

  <dependencies>
  <dependency>
           <groupId>net.mailjimp</groupId>
           <artifactId>mailjimp-core</artifactId>
  </dependency>
  <dependency>
           <!-- manually added the runtime to mvn repo using rt jar from another newly created kt project –>
           <groupId>com.jetbrains</groupId>
           <artifactId>kotlin-runtime</artifactId>
           <version>1.0</version>
  </dependency>
  <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <scope>test</scope>
  </dependency>
  </dependencies>

  <pluginRepositories>
  <pluginRepository>
           <id>kotlin plugin repo</id>
           <url>http://evgeny-goldin.org/artifactory/repo/</url&gt;
  </pluginRepository>
  </pluginRepositories>
</project>


Here is my main Java class:

package org.petrovic.mailchimp;

import mailjimp.dom.response.list.MailingList;
import mailjimp.service.MailJimpException;
import mailjimp.service.impl.MailJimpJsonService;

import java.util.List;

public class App {
  public static void main(String args) throws MailJimpException {
  App app = new App();
  app.run();
  }

  private void run() throws MailJimpException {
  String userName = “blah”;
  String password = “foo”;
  String apiKey = “thekey”;
  String apiVersion = “1.3”;
  boolean ssl = false;
  MailJimpJsonService mailJimpJsonService = new MailJimpJsonService(userName, password, apiKey, apiVersion, ssl);
  mailJimpJsonService.init();
  List<MailingList> lists = mailJimpJsonService.lists();
  for (MailingList list : lists) {
           System.out.println(list);
  }

  &nbsp;&nbsp;ChimpUtils c = new ChimpUtils(1);
  &nbsp;&nbsp;c.foo();

  }
}

And here is the Kotlin class:

package org.petrovic.mailchimp

class ChimpUtils(param : Int) {
  val property = param

  fun foo() {
  println(“hello”)
  }
}

I have no idea if I’m using the “package” statement correctly in the Kotlin file.  It feels like I may be lugging some Java paradigms into a realm where I may not need them?  Maybe not.  I need to read the docs.  Seems like I should know something about Kotlin modules before going much further.

Anyway, within a couple hours of downloading IJ EAP 11.1 and reading E. Goldin’s maven plugin post, I got ‘hello’ working from the IDE.

But I still cannot compile the app from the command line using mvn - I need to study the plugin config a bit more:  the compiler cannot resolve the Kotlin class from within the Java class.  Maybe it’s a maven compile-phase issue.

> mvn clean compile [INFO] Scanning for projects... [INFO]                                                                    [INFO] ------------------------------------------------------------------------ [INFO] Building mailchimp-kotlin 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ mailchimp-kotlin --- [INFO] Deleting /Users/petrovic/Projects/mailchimp-kotlin/target [INFO] [INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ mailchimp-kotlin --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/petrovic/Projects/mailchimp-kotlin/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ mailchimp-kotlin --- [INFO] Compiling 1 source file to /Users/petrovic/Projects/mailchimp-kotlin/target/classes [INFO] ------------------------------------------------------------- [ERROR] COMPILATION ERROR : [INFO] ------------------------------------------------------------- [ERROR] /Users/petrovic/Projects/mailchimp-kotlin/src/main/java/org/petrovic/mailchimp/App.java:[28,8] cannot find symbol symbol  : class ChimpUtils location: class org.petrovic.mailchimp.App [ERROR] /Users/petrovic/Projects/mailchimp-kotlin/src/main/java/org/petrovic/mailchimp/App.java:[28,27] cannot find symbol symbol  : class ChimpUtils location: class org.petrovic.mailchimp.App [INFO] 2 errors [INFO] ------------------------------------------------------------- [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.432s [INFO] Finished at: Mon Feb 27 07:51:51 PST 2012 [INFO] Final Memory: 9M/81M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project mailchimp-kotlin: Compilation failure: Compilation failure: [ERROR] /Users/petrovic/Projects/mailchimp-kotlin/src/main/java/org/petrovic/mailchimp/App.java:[28,8] cannot find symbol [ERROR] symbol  : class ChimpUtils [ERROR] location: class org.petrovic.mailchimp.App [ERROR] /Users/petrovic/Projects/mailchimp-kotlin/src/main/java/org/petrovic/mailchimp/App.java:[28,27] cannot find symbol [ERROR] symbol  : class ChimpUtils [ERROR] location: class org.petrovic.mailchimp.App [ERROR] -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
hth

Mark

Hi Mark, In order to compile Java sources using Kotlin libraries you need to use an "ivy-maven-plugin" that downloads Kotlin dependencies from TeamCity and adds them to Maven's "compile" scope. An example to that can be found in Kotlin GitHub repo, "kdoc" sub-project.  Can you try doing the same and let us know? I see this question is becoming common so I'll add this scenario to plugin's Wiki. Also, the work is being done on deploying Kotlin artifacts to Maven repository, this will eliminate the need in using "ivy-maven-plugin" and will allow to use Kotlin runtime as a regular Maven <dependency>.

I hope this answers the question about adding Kotlin dependencies to the compilation classpath. Btw, if you follow "kdoc" example linked above it means you need to remove the Kotlin <dependency> you have added:

<dependency>   <groupId>com.jetbrains</groupId>   <artifactId>kotlin-runtime</artifactId>   <version>1.0</version> </dependency>

As about compiling Java sources with Kotlin-compiled classes I'll try to provide a sample example later today.

Ah, that could be easier than I thought :) In your case Maven attempts to compile Java sources before Kotlin sources are compiled. Can you try setting Kotlin's plugin phase to, say, "process-resources" ?

<id>compile-kotlin-sources</id>
<goals>
  <goal>compile</goal>
  <goal>testCompile</goal>
</goals>
<phase>process-resources</phase>


This will ensure that Kotlin compilation runs before “compile” phase.

Yep, running the Kotlin compiler plugin in an earlier phase did the trick:

<pre>

  &nbsp;&nbsp;&lt;pluginManagement&gt;

           <plugins>
           <plugin>
                   <groupId>com.goldin.plugins</groupId>
                   <artifactId>kotlin-maven-plugin</artifactId>
                   <version>0.2.3.8-beta-6</version>
                   <executions>
                   <execution>
                           <id>compile-kotlin-sources</id>
                           <goals>
                           <goal>compile</goal>
                           <goal>testCompile</goal>
                           </goals>
                           <phase>process-resources</phase>
                           <configuration>
                           <src>${project.basedir}/src/main/kotlin</src>
                           <output>${project.build.outputDirectory}</output>
                           </configuration>
                   </execution>
                   </executions>
           </plugin>
           </plugins>
  </pluginManagement>

</pre>

Genie wrote:

Ah, that could be easier than I thought :slight_smile: In your case Maven attempts to compile Java sources before Kotlin sources are compiled. Can you try setting Kotlin’s plugin phase to, say, “process-resources” ?

<id>compile-kotlin-sources</id>
<goals>
  <goal>compile</goal>
  <goal>testCompile</goal>
</goals>
<phase>process-resources</phase>


This will ensure that Kotlin compilation runs before “compile” phase.

Excellent, glad it worked for you!

Hallo

Yep, running the Kotlin compiler plugin in an earlier phase did the trick:

But what if you have Kotlin classes that depend on Java classes and the other way around? Maybe even only two classes that depend on one another. Is that supported, too? Currently I get "Unresolved reference FooBar" whatever I try.

bye,

-christian-

Genie wrote:

Hi Mark,


In order to compile Java sources using Kotlin libraries you need to use an “ivy-maven-plugin” that downloads Kotlin dependencies from TeamCity and adds them to Maven’s “compile” scope. An example to that can be found in Kotlin GitHub repo, “kdoc” sub-project.  Can you try doing the same and let us know? I see this question is becoming common so I’ll add this scenario to plugin’s Wiki.


Also, the work is being done on deploying Kotlin artifacts to Maven repository, this will eliminate the need in using “ivy-maven-plugin” and will allow to use Kotlin runtime as a regular Maven <dependency>.


How do I keep up with the progress of the Kotlin runtime artifacts being deployed to a well-known Maven repository?

Thanks!

Genie wrote:

Hi Mark,


In order to compile Java sources using Kotlin libraries you need to use an “ivy-maven-plugin” that downloads Kotlin dependencies from TeamCity and adds them to Maven’s “compile” scope. An example to that can be found in Kotlin GitHub repo, “kdoc” sub-project.  Can you try doing the same and let us know? I see this question is becoming common so I’ll add this scenario to plugin’s Wiki.


Also, the work is being done on deploying Kotlin artifacts to Maven repository, this will eliminate the need in using “ivy-maven-plugin” and will allow to use Kotlin runtime as a regular Maven <dependency>.


I’m quite impressed – using the kdoc trick worked :slight_smile:   And I removed the Maven <dependency> on the kotlin-runtime in my POM.

One odd effect, however:  I can buld and run the project from the command line with something like

mvn clean compile exec:java -Dexec.mainClass=“namespace” -Dexec.args=“foolist”

But when I try to “Run namespace” within the IDE, the IDE fails to run with a “StackOverflow” error in the bottom border of the window.  Not good :slight_smile:

Anyway, my POM:

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

  <groupId>org.petrovic</groupId>
  <artifactId>mailchimp-kotlin</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>mailchimp-kotlin</name>
  <url>http://maven.apache.org</url&gt;

  <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <kotlin-maven-plugin.version>0.2.3.8-beta-6</kotlin-maven-plugin.version>
  <mailjimp.version>0.4-SNAPSHOT</mailjimp.version>
  </properties>

  <dependencyManagement>
  <dependencies>
           <dependency>
           <groupId>net.mailjimp</groupId>
           <artifactId>mailjimp-core</artifactId>
           <version>${mailjimp.version}</version>
           </dependency>
           <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.8.2</version>
           <scope>test</scope>
           </dependency>
  </dependencies>
  </dependencyManagement>

  <build>
  <sourceDirectory>src/main/kotlin</sourceDirectory>
  <testSourceDirectory>src/test/kotlin</testSourceDirectory>
  <pluginManagement>
           <plugins>
           <plugin>
                   <groupId>com.goldin.plugins</groupId>
                   <artifactId>kotlin-maven-plugin</artifactId>
                   <version>${kotlin-maven-plugin.version}</version>
                   <executions>
                   <execution>
                           <id>compile-kotlin-sources</id>
                           <goals>
                           <goal>compile</goal>
                           <goal>testCompile</goal>
                           </goals>
                   </execution>
                   </executions>
           </plugin>
           <plugin>
                   <groupId>com.goldin.plugins</groupId>
                   <artifactId>ivy-maven-plugin</artifactId>
                   <version>${kotlin-maven-plugin.version}</version>
                   <executions>
                   <execution>
                           <id>add-ivy-dependencies</id>
                           <goals>
                           <goal>ivy</goal>
                           </goals>
                           <phase>initialize</phase>
                           <configuration>
                           <ivyconf>${project.basedir}/src/main/resources/ivyconf.xml</ivyconf>
                           <dependencies>
                                   <dependency>
                                   <groupId>ivy.org</groupId>
                                   <artifactId>bt343</artifactId>
                                   <version>latest.lastSuccessful</version>
                                   <classifier>core/intellij-core</classifier>
                                   </dependency>
                                   <dependency>
                                   <groupId>ivy.org</groupId>
                                   <artifactId>bt344</artifactId>
                                   <version>latest.lastSuccessful</version>
                                   <classifier>kotlin-runtime</classifier>
                                   </dependency>
                           </dependencies>
                           <scope>compile</scope>
                           <verbose>true</verbose>
                           </configuration>
                   </execution>
                   </executions>
           </plugin>
           </plugins>
  </pluginManagement>
  <plugins>
           <plugin>
           <groupId>com.goldin.plugins</groupId>
           <artifactId>ivy-maven-plugin</artifactId>
           <version>${kotlin-maven-plugin.version}</version>
           </plugin>
           <plugin>
           <groupId>com.goldin.plugins</groupId>
           <artifactId>kotlin-maven-plugin</artifactId>
           </plugin>
  </plugins>
  </build>

  <dependencies>
  <dependency>
           <groupId>net.mailjimp</groupId>
           <artifactId>mailjimp-core</artifactId>
  </dependency>
  <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <scope>test</scope>
  </dependency>
  </dependencies>

  <pluginRepositories>
  <pluginRepository>
           <id>kotlin plugin repo</id>
           <url>http://evgeny-goldin.org/artifactory/repo/</url&gt;
  </pluginRepository>
  </pluginRepositories>
</project>

Woops.  Using the kdoc trick, which includes removing the <dependency> on the Kotlin runtime in the POM, deprives the IDE of println resolution.  See the attached screenshot.



[kotlin-stdio.png|attachment](upload://ytQsbzhTrsh0E3yzWXWAn7i1BDh.png) (173 KB)

Hi Christian, No, joint Java/Kotlin compilation isn't supported right now, as far as I know.

http://youtrack.jetbrains.com/issue/KT-1472 is the issue to follow.

As long as Kotlin is only available in TeamCity Ivy repo, we can only fool Maven by adding it through "ivy-kotlin-plugin" <dependencies>, not a real Maven <dependencies>. This solves the Maven compilation issue but it makes IDEA unaware of the workaround made so it doesn't see the Kotlin library. To make it aware of Kotlin, I add Kotlin manually as project's library: File => Project Structure => Libraries => "+" => Java => point to .ivy2 cache in your home directory where Kotlin binaries are downloaded to (see the screenshot attached). Once we have the Maven repo available all those inconveniences should go away.



[ScreenShot001.png|attachment](upload://9eZT0eFujDUhiY2Jc09MzqJCNFI.png) (33.1 KB)

Just voted for it.

For fun, I added the maven shade plugin to the POM.  You have to keep an eye on the output in the uber jar, but the shade plugin can often be real handy.  When you add the shade plugin, you can do stuff like this to run the app:

$ java  -cp target/mailchimp-kotlin-1.0-SNAPSHOT-shaded-uber.jar namespace

Here’s the POM snippet:

  <plugins>            <plugin>            <groupId>com.goldin.plugins</groupId>            <artifactId>ivy-maven-plugin</artifactId>            <version>${kotlin-maven-plugin.version}</version>            </plugin>            <plugin>            <groupId>com.goldin.plugins</groupId>            <artifactId>kotlin-maven-plugin</artifactId>            </plugin>            <plugin>            <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-shade-plugin</artifactId>            <version>1.4</version>            <executions>                    <execution>                    <phase>package</phase>                    <goals>                            <goal>shade</goal>                    </goals>                    <configuration>                            <shadedArtifactAttached>true</shadedArtifactAttached>                            <shadedClassifierName>shaded-uber</shadedClassifierName>                            <transformers>                            <!-- Concatenate the META-INF/services entries for each input jar -->                            <transformer                                    implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"                                    />                            </transformers>                    </configuration>                    </execution>            </executions>            </plugin>   </plugins>

I created a github repo that holds this work in progress:

git@github.com:ae6rt/mailchimp-kotlin.git

I can’t run the app from within the IDE, and I don’t know why.  I based the creation of the IDEA project on the Maven POM.  Whenever I try to run it in IDEA, I get a StackOverflow message in the IDE bottom tray.  I may be doing something silly.

But you can run the app from the command line:

mvn compile exec:java -Dexec.mainClass=“namespace” -Dexec.args=“–list somelist --since ‘2013-12-12 23:21:33’”

To run the app for full effect, you need a MailChimp account, at least one mailing list, and at least one user on that list who has unsubscribed since some known date.  Add your MailChimp credentials to src/main/resources/mailchimp.properties before you run.

http://mailchimp.com/