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

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

问题描述

我正在尝试编译maven项目,该项目包含引用Java类的Kotlin类。这是我父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类:

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!\"."
    }
}

DummyService Consts 类是与 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:\somepath\service\DummyServiceImpl.kt: (14, 31) Unresolved reference: DummyService
[error] C:\somepath\service\DummyServiceImpl.kt: (16, 35) Unresolved reference: Consts

如果我切换Kotlin插件执行阶段编译然后如果有从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的< build> 中有这个声明

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中为3x在配置实际中提到的src / test),即使它们不包含任何类/资源。一旦它适合你,你仍然可以微调配置。

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天全站免登陆