Die Einführung von Kotlin in ein in Java geschriebenes Maven-Projekt ist einfach. Schreiben Sie einfach pom.xml um, um den Kotlin -Quellcode hinzuzufügen und ihn mit dem Java-Code zu erstellen.
Wie erwartet * 100% interoperabel mit Java ™ *! (\ * 'E` \ *)
Wenn ich pom.xml gemäß dem offiziellen Dokument ändere, funktioniert es irgendwie, aber ich habe nicht verstanden, warum Maven überhaupt funktioniert, also habe ich es untersucht.
Nehmen wir als Beispiel ein einfaches pom.xml.
Durch Umschreiben von pom.xml können Sie den Code von Kotlin zum Maven-Projekt hinzufügen, das ursprünglich nur mit * .java funktioniert hat.
<?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>
<groupId>megmogmog1965</groupId>
<artifactId>JavaJarToKotlin</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
+ <!-- Kotlin -->
+ <kotlin.version>1.1.2-5</kotlin.version>
</properties>
<dependencies>
+ <!-- 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>
+ <!-- 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>
+ </plugin>
<!-- for .java -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
+ <executions>
+ <!-- Replacing default-compile as it is treated specially by maven -->
+ <execution>
+ <id>default-compile</id>
+ <phase>none</phase>
+ </execution>
+ <!-- Replacing default-testCompile as it is treated specially by maven -->
+ <execution>
+ <id>default-testCompile</id>
+ <phase>none</phase>
+ </execution>
+ <execution>
+ <id>compile</id>
+ <phase>compile</phase>
+ <goals>
+ <goal>compile</goal>
+ </goals>
+ </execution>
+ <execution>
+ <id>testCompile</id>
+ <phase>test-compile</phase>
+ <goals>
+ <goal>testCompile</goal>
+ </goals>
+ </execution>
+ </executions>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Zunächst einmal verstehe ich die Bedeutung von Phase oder Ziel nicht, daher werde ich versuchen, ein Diagramm zu erstellen. Das Folgende ist das Build Lifecycle -Diagramm der obigen pom.xml.

Kurz gesagt, ich möchte zuerst den Code für Kotlin und später * .java erstellen. Andernfalls kann der Verweis von * .java auf Kotlin nicht aufgelöst werden.
properties
<properties>
<!-- Kotlin -->
<kotlin.version>1.1.2-5</kotlin.version>
</properties>
dependencies
<dependencies>
<!-- 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>
println, listOf usw.plugins --> kotlin-maven-plugin
<plugins>
<!-- 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>
</plugin>
...
kotlin-maven-plugin compile Goal Execution (= Plugin-Ausführung) zur compile Phase von Build Lifecycle hinzuWie
test-compile
plugins --> maven-compiler-plugin
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<executions>
<!-- Replacing default-compile as it is treated specially by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially by maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
...
compile Goal Execution von maven-compiler-plugin zur compile Phase von Build LifecycleMit anderen Worten, um kotlin-maven-plugin: compile zuerst auszuführen, habe ich es gelöscht und erneut hinzugefügt.
Wie
test-compile