如何使用Maven编译Java+Kotlin项目? [英] How do you compile Java+Kotlin project using Maven?

查看:36
本文介绍了如何使用Maven编译Java+Kotlin项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编译具有引用 Java 类的 Kotlin 类的 maven 项目.这是我的父 POM 的一部分:

I'm trying to compile maven project which has Kotlin classes referencing Java classes. Here's a part of my parent POM:

...

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

...

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${compiler-plugin-version}</version>
    <configuration>
        <source>${java-version}</source>
        <target>${java-version}</target>
        <encoding>${project.build.sourceEncoding}</encoding>
    </configuration>
</plugin>

<plugin>
    <artifactId>kotlin-maven-plugin</artifactId>
    <groupId>org.jetbrains.kotlin</groupId>
    <version>${kotlin.plugin.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>

    <configuration>
        <scanForAnnotations>false</scanForAnnotations>
    </configuration>
</plugin>

以及子POM的相关部分:

And related parts of the child POM:

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-stdlib</artifactId>
</dependency>

...

<plugin>
    <artifactId>kotlin-maven-plugin</artifactId>
    <groupId>org.jetbrains.kotlin</groupId>
    <configuration>
        <sourceDirs>
            <source>${project.basedir}/src/main/kotlin</source>
        </sourceDirs>
    </configuration>
</plugin>

还有 Kotlin 类:

And the Kotlin class:

Stateless
open class DummyServiceImpl : DummyService {

    PersistenceContext(unitName = Consts.UNIT_NAME)
    private val em: EntityManager? = null

    override fun get(id: Long?): Dummy {
        return em!!.find<Dummy>(javaClass<Dummy>(), id)
    }

    override fun sayHi(): String {
        return "Dummy service says "Hi!"."
    }
}

DummyServiceConsts 类是与 DummyServiceImpl 位于同一模块中的 Java 类.因此,当我使用 Maven 编译包含 DummyServiceImpl 的模块时,它是这样的:

DummyService and Consts classes are Java classes residing in the same module as DummyServiceImpl. So when I compile the module containing DummyServiceImpl with Maven it goes like this:

[error] C:somepathserviceDummyServiceImpl.kt: (14, 31) Unresolved reference: DummyService
[error] C:somepathserviceDummyServiceImpl.kt: (16, 35) Unresolved reference: Consts

如果我将 Kotlin 插件执行 phase 切换到 compile 那么如果有从 Java 到 Kotlin 类的引用,它就会失败:

If I switch Kotlin plugin execution phase to compile then it predictably fails if there're references from Java to Kotlin classes:

[ERROR] /C:/somepath/service/impl/DummyServiceClientImpl.java:[5,27] cannot find symbol
[ERROR] symbol:   class DummyServiceImpl

那么,该怎么办呢?请注意,使用 IDEA 的 make 构建非常好.

So, what's to be done about this? Note that building with IDEA's make goes perfectly fine.

推荐答案

确保在你的 pom.xml

Make sure you have this declaration in <build> of your pom.xml

    <plugins>
        <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <version>${kotlin.version}</version>
            <executions>
                <execution>
                    <id>compile</id>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <source>src/main/java</source>
                            <source>src/main/kotlin</source>
                            <source>src/main/resources</source>
                        </sourceDirs>
                    </configuration>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <phase>process-test-sources</phase>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <source>src/test/java</source>
                            <source>src/test/kotlin</source>
                            <source>src/test/resources</source>
                        </sourceDirs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

确保配置中提到的所有文件夹(src/main 中的 3x & src/test 中的 3x)实际存在,即使它们不包含任何课程/资源.一旦配置适合您,您仍然可以对其进行微调.

Make sure that all folders (3x in src/main & 3x in src/test) mentioned in the configuration actually exist, even if they don’t contain any classes/resources. You can still fine-tune the configuration once it works for you.

还要注意使用我上面提到的完全相同的顺序让编译器先编译Java代码.

Also pay attention to use exactly the same order I mentioned above to let the compiler compile the Java code first.

这篇关于如何使用Maven编译Java+Kotlin项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆