混合Kotlin Java与Maven,未解决的参考

我有一个带有Kotlin代码hello.kt的Maven项目,该代码调用Java代码JavaFoo.java,该代码调用Kotlin代码KotlinFoo.kt. hello.kt也直接调用KotlinFoo.kt.我正在尝试使用mvn clean install使用kotlinlang’s Maven docs中描述的Maven设置来构建它.

如果hello.kt没有调用JavaFoo(但我将JavaFoo留在项目中),那么这个构建就好了.

文档说应该在Java编译器之前调用Kotlin编译器,这告诉我所有Kotlin代码都需要在任何Java代码之前编译,即使用这种设置你可以从Java调用Kotlin但反之亦然.但是,文档将此设置描述为“混合代码应用程序”,而不是“从Java调用Kotlin”.

换句话说,这种失败似乎与文档似乎暗示的内容一致,但与他们直接说的内容不一致 – 或者我只是误解某些东西.

我想从另一个语言中调用每种语言.有没有Maven配置可以做到这一点,好吗?

(我查看了有关混合代码设置的各种StackExchange问​​题,但没有一个解决方案适用于我.)

按要求添加代码:
pom.xml中:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.kotlindemo</groupId>
    <artifactId>kotlin-demo</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>kotlin-demo</name>

    <properties>
        <kotlin.version>1.1.2-2</kotlin.version> 
        <kotlin.compiler.incremental>true</kotlin.compiler.incremental>
        <main.class>com.example.kotlindemo.HelloKt</main.class>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
    </dependencies>

    <build>
        <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>
                        <configuration>
                            <sourceDirs>
                                <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
                                <sourceDir>${project.basedir}/src/main/java</sourceDir>
                            </sourceDirs>
                        </configuration>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <goals> <goal>test-compile</goal> </goals>
                        <configuration>
                            <sourceDirs>
                                <sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
                                <sourceDir>${project.basedir}/src/test/java</sourceDir>
                            </sourceDirs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.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>java-compile</id>
                        <phase>compile</phase>
                        <goals> <goal>compile</goal> </goals>
                    </execution>
                    <execution>
                        <id>java-test-compile</id>
                        <phase>test-compile</phase>
                        <goals> <goal>testCompile</goal> </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals> <goal>single</goal> </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>${main.class}</mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

的src / main /科特林/ hello.kt:

package com.example.kotlindemo

fun main(args : Array<String>) { 
  println("Hello, world!") 

  var kfoo = KotlinFoo()
  kfoo.printFooString()

  kfoo.fooString = "init2"
  kfoo.printFooString()

  var foo2 = JavaFoo("abcd")
  foo2.printString()
}

的src / main /科特林/ KotlinFoo.kt:

package com.example.kotlindemo

class KotlinFoo {
    var fooString = "init"

    fun printFooString() {
        println(this.fooString) 
    }
}

的src / main / JAVA / JavaFoo.java:

package com.example.kotlindemo;

class JavaFoo {
    private KotlinFoo k;

    JavaFoo(String initializer) {
        k = new KotlinFoo();
        k.setFooString(initializer);
    }

    void printString() {
        this.k.printFooString();
    }
}

错误:

[ERROR] .../src/main/kotlin/hello.kt: (12, 14) Unresolved reference: JavaFoo
最佳答案
编译失败,因为您的Java类不在与其package语句匹配的目录中.虽然Kotlin允许您将类放在任何目录中而不管它们所在的包中,但Java要求您将每个文件放在与其目录对应的包中.此要求也适用于混合语言项目.

要修复错误,请将JavaFoo.java移动到src / main / java / com / example / kotlindemo.

转载注明原文:混合Kotlin Java与Maven,未解决的参考 - 代码日志