官术网_书友最值得收藏!

Kotlin with Maven

If you still prefer to stick with good old Maven, there is no problem. There is a plugin for it to support Kotlin as well. If you don't have Maven on your machine, you can follow the instructions at https://maven.apache.org/install.html to get it installed on your local machine.

Just as we did with Gradle, let's use the built-in templates to generate the project folder and file structure. From the Terminal, run the following command in an empty directory:

$ mvn archetype:generate -DgroupId=com.programming.kotlin 
-DartifactId=chapter01 -DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false
maven-archetype- quickstart => maven-archetype-quickstart

This will generate the pom.xml file and the src folder for Maven. But before we add the file containing the kotlin code, we need to enable the plugin. Just as before, start by deleting App.java and AppTest.java from src/main/java/com/programming/kotlin and src/test/java/com/programming/kotlin, and create the src/main/kotlin directory (the subdirectory structure matches the namespace name), as shown in the following code:

$ mkdir -p src/main/kotlin/com/programming/kotlin/chapter01
$ mkdir -p src/test/kotlin/com/programming/kotlin/chapter01

In an editor of your choice, open up the generated pom.xml file and add the following:

<properties> 
<kotlin.version>1.3.31</kotlin.version>
<kotlin.test.version>3.3.2</kotlin.test.version>
</properties>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<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>

All we have done so far is enable the Kotlin plugin and make it run in the process-stages phase to allow the mixing of Java code as well. There are cases where you might have part of the source code written in good old Java. I am sure you also noticed the addition of source directory tags, allowing for the kotlin files to be included in the build.

The only thing left to do now is to add the library dependencies for the Kotlin runtime, as well as the unit tests. We are not going to touch upon the testing framework until later in the book. Replace the entire dependencies section with the following:

     <dependencies> 
       <dependency> 
          <groupId>org.jetbrains.kotlin</groupId> 
          <artifactId>kotlin-stdlib</artifactId> 
          <version>${kotlin.version}</version> 
        </dependency> 
   
        <dependency> 
          <groupId>io.kotlintest</groupId> 
          <artifactId>kotlintest-runner-junit5</artifactId> 
          <version>${kotlin.test.version}</version> 
          <scope>test</scope> 
        </dependency> 
      </dependencies>

It is now time to add the Hello World! code; this step is similar to the one we took earlier when we discussed Gradle, as you can see from the following code:

$ echo "" >> src/main/kotlin/com/programming/kotlin/chapter01/Program.kt
$cat <<EOF >> src/main/kotlin/com/programming/kotlin/chapter01/Program.kt
    package com.programming.kotlin.chapter01
    fun main(args: Array<String>) {
      println("Hello World!")
    }

We are now in a position to compile and build the JAR file for the sample program using the following code:

$ mvn package
$ mvn exec:java - Dexec.mainClass="com.programming.kotlin.chapter01.ProgramKt"

The last instruction should end up printing the Hello World! text to the console. Of course, we can run the program outside Maven by going back to executing Java, but we need to add the Kotlin runtime to the classpath, as follows:

$java -cp $KOTLIN_HOME/lib/kotlin-runtime.jar:target/chapter01-1.0- SNAPSHOT.jar "com.programming.kotlin.chapter01.ProgramKt"

If you want to avoid the classpath dependency setup when you run the application, there is an option to bundle all the dependencies in the result JAR and produce what is called a fat jar. For that, however, another plugin needs to be added, as shown in the following code:

    <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-shade-plugin</artifactId> 
      <version>2.4.3</version> 
      <executions> 
        <execution> 
          <phase>package</phase> 
          <goals> 
            <goal>shade</goal> 
          </goals> 
           <configuration> 
            <transformers> 
              <transformer  implementation="org.apache.maven.plugins.shade.resource.ManifestRe sourceTransformer"> 
                 <mainClass>com.programming.kotlin.chapter01.ProgramKt</mainClass> 
              </transformer> 
            </transformers> 
          </configuration> 
        </execution> 
      </executions> 
    </plugin> 

We can execute the command to run our JAR without having to worry about setting the classpath since this has been taken care of by the plugin, as follows:

$ java -jar target/chapter01-1.0-SNAPSHOT.jar

The result of executing this command is to launch the JAR and execute the program.

主站蜘蛛池模板: 丹阳市| 沾化县| 措勤县| 德兴市| 陇西县| 阿鲁科尔沁旗| 景泰县| 马公市| 镇平县| 兴安盟| 长垣县| 昭平县| 大竹县| 耿马| 呼和浩特市| 龙海市| 阜南县| 左贡县| 齐河县| 松原市| 安康市| 晋宁县| 湘乡市| 东平县| 大冶市| 乌拉特前旗| 淅川县| 突泉县| 雷州市| 禄劝| 湘潭市| 永修县| 渭南市| 澄迈县| 巩留县| 公主岭市| 曲水县| 定结县| 巴彦淖尔市| 罗江县| 陕西省|