简体   繁体   English

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

[英]Mixed Kotlin + Java with Maven, unresolved reference

I have a Maven project with Kotlin code hello.kt which calls Java code JavaFoo.java which calls Kotlin code KotlinFoo.kt . 我有一个带有Kotlin代码hello.kt的Maven项目,该代码调用Java代码JavaFoo.java ,该代码调用Kotlin代码KotlinFoo.kt hello.kt also calls KotlinFoo.kt directly. hello.kt也直接调用KotlinFoo.kt I'm trying to build this with mvn clean install using exactly the Maven settings described in kotlinlang's Maven docs . 我正在尝试使用mvn clean install使用kotlinlang的Maven文档中描述的Maven设置来构建它。

If hello.kt doesn't call JavaFoo (but I leave JavaFoo in the project) then this builds just fine. 如果hello.kt没有调用JavaFoo (但我将JavaFoo留在项目中),那么这个构建就好了。

The docs say that the Kotlin compiler should be invoked before the Java compiler, which suggests to me that all Kotlin code needs to compile before any Java code, ie with this setup you can call Kotlin from Java but not vice versa. 文档说应该在Java编译器之前调用Kotlin编译器,这告诉我所有Kotlin代码都需要在任何Java代码之前编译,即使用此设置可以从Java调用Kotlin但反之亦然。 However, the docs describe this setup as just "mixed code applications", not "calling Kotlin from Java". 但是,文档将此设置描述为“混合代码应用程序”,而不是“从Java调用Kotlin”。

In other words, this failure seems consistent with what the docs seem to imply but not with what they directly say -- or I'm just misunderstanding something. 换句话说,这种失败似乎与文档似乎暗示的内容一致,但与他们直接说的内容不一致 - 或者我只是误解某些东西。

I want to call each language from the other. 我想从另一个语言中调用每种语言。 Is there a Maven configuration that will do this, please? 有没有Maven配置可以做到这一点,好吗?

(I looked at various StackExchange questions on mixed code settings and none of the solutions works for me.) (我查看了有关混合代码设置的各种StackExchange问​​题,但没有一个解决方案适用于我。)

Adding the code as requested: pom.xml: 按要求添加代码: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/kotlin/hello.kt : src/main/kotlin/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/kotlin/KotlinFoo.kt : src/main/kotlin/KotlinFoo.kt

package com.example.kotlindemo

class KotlinFoo {
    var fooString = "init"

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

src/main/java/JavaFoo.java : 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: 错误:

[ERROR] .../src/main/kotlin/hello.kt: (12, 14) Unresolved reference: JavaFoo

The compilation fails because your Java class is not in a directory that matches its package statement. 编译失败,因为您的Java类不在与其package语句匹配的目录中。 While Kotlin allows you to put classes in any directories regardless of the package they're in, Java requires you to put each file in a package that corresponds to its directory. 虽然Kotlin允许您将类放在任何目录中而不管它们所在的包中,但Java要求您将每个文件放在与其目录对应的包中。 This requirement applies to mixed-language projects as well. 此要求也适用于混合语言项目。

To fix the error, move JavaFoo.java to src/main/java/com/example/kotlindemo . 要修复错误,请将JavaFoo.java移动到src/main/java/com/example/kotlindemo

If you are using maven, you need to add the goal: <goal>addSources</goal> 如果您使用的是maven,则需要添加目标: <goal>addSources</goal>

Like this: 像这样:

<executions>
    <execution>
        <goals>
            <goal>addSources</goal>
            <goal>addTestSources</goal>
            <goal>generateStubs</goal>
            <goal>compile</goal>
            <goal>testGenerateStubs</goal>
            <goal>testCompile</goal>
            <goal>removeStubs</goal>
            <goal>removeTestStubs</goal>
        </goals>
    </execution>
</executions>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM