【发布时间】:2015-06-02 12:43:14
【问题描述】:
我正在尝试编译具有引用 Java 类的 Kotlin 类的 maven 项目。这是我父 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的相关部分:
<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 的模块时,它是这样的:
[error] C:\somepath\service\DummyServiceImpl.kt: (14, 31) Unresolved reference: DummyService
[error] C:\somepath\service\DummyServiceImpl.kt: (16, 35) Unresolved reference: Consts
如果我将 Kotlin 插件执行 phase 切换到 compile,那么如果有从 Java 到 Kotlin 类的引用,它会失败:
[ERROR] /C:/somepath/service/impl/DummyServiceClientImpl.java:[5,27] cannot find symbol
[ERROR] symbol: class DummyServiceImpl
那么,该怎么办呢?请注意,使用 IDEA 的 make 构建非常好。
【问题讨论】:
-
您的 Java 代码是否在不同的模块中?
-
@SergeyMashkov 不,同一个模块。
-
我在 gradle 中遇到了同样的问题。你找到解决办法了吗?
-
@DANGFan 不,对不起。
-
如果我有一个仅 Kotlin 的项目并想使用 maven 来实现它(以便可以在 pom 中指定依赖项),我是否只使用 pom.xml ?