📜 ⬆️ ⬇️

Getting to know Kotlin and building a jar with maven

This is a post about how I met Kotlin, what resources I used for this, tools, and how I gathered it in a jar and launched it in the console.

In general, I am a PHP programmer, a little bit of Java. The other day I decided to see Kotlin. This is a less strongly typed language from JetBrains to the point that it is, in fact, statically typed. This is its main fundamental difference from Java, plus Kotlin has its nice syntax. Kotlin can be compiled into JVM or JavaScript.

Let's get to know each other. First you need IDE Idea.

http://www.jetbrains.com/idea/
')
Installation instructions on the site. Version 13+.

Inside the IDE, I created a simple Java project. Next you need to install a plugin from JetBrains for IDE.
http://confluence.jetbrains.com/display/Kotlin/Getting+Started

The plugin adds autofill and the ability to create Kotlin projects.
Also added tools for compiling Kotlin.

The peculiarity of Kotlin is that it is fully compatible with Java. Those. code written in Kotlin can be called in Java and vice versa. Consequently, all the tools and libraries that are used to work with Java are applicable to Kotlin.

If the project in the IDE was not created by Kotlin initially, then after installing the plug-in, you can enable Kotlin support. After being included in the project, the Kotlin jar libraries will be added to the / lib directory.

I turned on Kotlin and wrote the following code for the test in the / src / testing folder:

//first.kt package testing /** * Created by seyfer on 26.03.14. */ //fun main(args : Array<String>) { // println("Hello, world!") //} //fun main(args : Array<String>) { // if (args.size == 0) { // println("Please provide a name as a command-line argument") // return // } // println("Hello, ${args[0]}!") //} /*fun main(args : Array<String>) { for (name in args) println("Hello, $name!") }*/ fun main(args: Array<String>) { val language = if (args.size == 0) "EN" else args[0] println(when (language) { "EN" -> "Hello!" "FR" -> "Salut!" "IT" -> "Ciao!" "seyfer" -> "seed!" else -> "Sorry, I can't greet you in $language yet" }) } 


In the cotlin file I left comments on purpose. These are examples from the page.
http://confluence.jetbrains.com/display/Kotlin/Hello%2C+world%21

To begin with, I executed the cauldron file in the IDE separately to make sure that the plugin works and the code compiles.

Then I created a java class:

 //first.java package testing; /** * Created with IntelliJ IDEA. * User: seyfer * Date: 24.07.13 * Time: 21:02 * To change this template use File | Settings | File Templates. */ public class first { public static void main(String[] args) { System.out.print("Hello " + args[0] + "\n"); TestingPackage.main(args); } } 


Here it is clear that I want to take the argument and pass it directly to the cotlin. In Kotlin, I used the example with languages ​​and added another one of mine, which depends on the argument - my nickname. In Java, you can call methods and get access to variables from the cotlin by calling - PackageNamePackage.methome ().

If you correctly configure the IDE in the execution of the project, you can specify with what argument to call the program. But this is not very convenient, I would like to change the argument on the fly to catch errors for testing. I needed to build a jar to run from the console.
The project was not maven initially, and I added it there.

This page helped me in this.
http://confluence.jetbrains.com/display/Kotlin/Kotlin+Build+Tools
The documentation of the cotlin is informative enough to learn the language on it.
Also, regarding the construction using maven and gradle, you can see the following examples.
https://github.com/JetBrains/kotlin-examples

Here is my pom.xml file
 <?xml version="1.0" encoding="UTF-8"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>org.jetbrains.kotlin.examples</groupId> <artifactId>idea_test</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>idea_test</name> <url>http://maven.apache.org</url> <properties> <kotlin.version>0.1-SNAPSHOT</kotlin.version> <junit.version>4.10</junit.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> <version>${kotlin.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <!-- Build an executable JAR --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>testing.first</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <artifactId>kotlin-maven-plugin</artifactId> <groupId>org.jetbrains.kotlin</groupId> <version>${kotlin.version}</version> <configuration><sourceDirs> < source > src/ < / s ource > </sourceDirs></configuration> <executions> <execution> <id>compile</id> <phase>process-sources</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>test-compile</id> <phase>process-test-sources</phase> <goals> <goal>test-compile</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <repositories> <repository> <id>sonatype.oss.snapshots</id> <name>Sonatype OSS Snapshot Repository</name> <url>http://oss.sonatype.org/content/repositories/snapshots</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>sonatype.oss.snapshots</id> <name>Sonatype OSS Snapshot Repository</name> <url>http://oss.sonatype.org/content/repositories/snapshots</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project> 




It uses the setting for the kotlin-maven-plugin, which indicates in which directory to search for files.
Also, maven-jar-plugin is used. Here it is very important to correctly register < manifest >, otherwise it will take a long time to figure out why the jar does not start with the error " No Main Manifest Attribute ".
After the build in the directory / target will be the project jar. Idea has a separate window for maven projects, from where it is convenient to manage the construction.

To start the jar, the command is usually executed

 java -jar /path/to/file.jar argument1 argument2 


In my case it did not work, because I didn't use the maven plugin, which would add the jotkin jar library to the build.
Therefore I get an error

 java -jar target/idea_test-1.0-SNAPSHOT.jar 1 Hello 1 Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics at testing.TestingPackage-first-39a20e2.main(first.kt:27) at testing.TestingPackage.main(first.kt:1) at testing.first.main(first.java:16) Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics at java.net.URLClassLoader$1.run(URLClassLoader.java:366) 


Hence it is clear that the Kotlin library is not located.
The correct call with the indication of the jar dependency

 java -cp target/idea_test-1.0-SNAPSHOT.jar:lib/kotlin-runtime.jar testing.first seyfer Hello seyfer seed! 


Now the code works correctly. As you can see, I got the argument in java, brought it out, then I passed it to kotlin, and based on it worked out the when construct (similar in meaning to the switch-case).

In order to build jar with the inclusion of dependent jar in the build, you can use the maven plugin - maven-dependency-plugin. Or onejar-maven-plugin to the one-jar project.

I decided to use one-jar.
Here is what you need to add to pom.xml
 <plugin> <groupId>org.dstovall</groupId> <artifactId>onejar-maven-plugin</artifactId> <version>1.4.5</version> <executions> <execution> <configuration> <binlibs> <fileSet> <directory>${project.build.directory}/../lib/</directory> <includes> <include>kotlin-runtime.jar</include> </includes> </fileSet> </binlibs> <!-- Optional, default is false --> <attachToBuild>true</attachToBuild> <!-- Optional, default is "onejar" --> <classifier>onejar</classifier> </configuration> <goals> <goal>one-jar</goal> </goals> </execution> </executions> </plugin> <pluginRepository> <id>onejar-maven-plugin.googlecode.com</id> <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url> </pluginRepository> 



Next, rebuild the project.
 mvn package 


The project.one-jar.jar file will appear in the / target directory. It can be called by the usual method.

 java -jar target/idea_test-1.0-SNAPSHOT.one-jar.jar seyfer Hello seyfer seed! 


This is where the project setup is complete for me Further it is possible to study Kotlin under the links to documentation given above.
Personally, I really liked the language. He will undoubtedly occupy his niche, as well as the hack language that Facebook is developing as its version of PHP.

Official page Kotlin.
http://kotlin.jetbrains.org/

Browser Editor with examples.
http://kotlin-demo.jetbrains.com/

By the way, the web framework has already been written on Kotlin.
http://karaframework.com/

Source: https://habr.com/ru/post/221269/


All Articles