Maven 尝试从Java实例化Kotlin类时出错
我试图从Java实例化Kotlin类,但每次尝试使用Maven编译时,都会出现错误Maven 尝试从Java实例化Kotlin类时出错,maven,kotlin,Maven,Kotlin,我试图从Java实例化Kotlin类,但每次尝试使用Maven编译时,都会出现错误找不到符号: class ConfigCommand(private val game: Game) : Command("config", "") { init { addAliases("cfg") } override fun getRequiredRank(): Rank? { return null } override fun getDescription():
找不到符号:
class ConfigCommand(private val game: Game) : Command("config", "") {
init {
addAliases("cfg")
}
override fun getRequiredRank(): Rank? {
return null
}
override fun getDescription(): String {
return "Shows the config for the game"
}
@Throws(CommandException::class)
override fun execute(sender: CommandSender, args: Array<String>): Boolean {
if (args.isEmpty()) {
if (sender !is Player)
throw NoConsoleAccessException()
sender.openInventory(ConfigGUI(game).build())
return true
}
return false
}
}
在多次浏览页面并意识到自己没有做错什么之后,我开始胡乱处理pom,并最终通过将Kotlin编译的阶段更改为过程源代码来使其正常工作。在多次浏览页面并意识到自己没有做错什么之后,我开始胡乱处理围绕我的pom,通过将Kotlin编译的阶段更改为过程源代码
我仍在了解Kotlin,但我尝试根据您的示例进行一些排列,最终使其工作。我很容易就能根据你的Hastebin pom创建你的问题
将
更改为过程源代码
可以让我的测试代码在Maven中编译,这是对的,但是我(老实说)我无法始终记住Maven的所有阶段,因此我个人不愿意在没有更多研究的情况下更改为处理源代码——特别是因为IntelliJ工具依赖于编译阶段
在讨论了我自己的互操作示例之后,关键的(也是工具默认值中缺少的)部分似乎是
元素,作为
的顶级子元素,如:
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
src/main/java
org.jetbrains.kotlin
kotlin maven插件
${kotlin.version}
编译
在运行mvncile
终端命令时,将
作为顶级元素添加到
下,使我的Java对Kotlin interop代码进行编译。当我在“java”目录中混合源文件以同时包含java和Kotlin文件时,情况就是这样
作为旁注(我写这篇文章时不明白为什么),当我在Kotlin源代码中添加“Kotlin”作为类名的一部分时,我不需要添加
元素…我仍然在理解Kotlin,但我尝试根据您的示例进行一些排列。我很容易就能根据你的Hastebin pom创建你的问题
将
更改为过程源代码
可以让我的测试代码在Maven中编译,这是对的,但是我(老实说)我无法始终记住Maven的所有阶段,因此我个人不愿意在没有更多研究的情况下更改为处理源代码——特别是因为IntelliJ工具依赖于编译阶段
在讨论了我自己的互操作示例之后,关键的(也是工具默认值中缺少的)部分似乎是
元素,作为
的顶级子元素,如:
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
src/main/java
org.jetbrains.kotlin
kotlin maven插件
${kotlin.version}
编译
在运行mvncile
终端命令时,将
作为顶级元素添加到
下,使我的Java对Kotlin interop代码进行编译。当我在“java”目录中混合源文件以同时包含java和Kotlin文件时,情况就是这样
作为旁注(我写这篇文章时不明白为什么),当我加上“Kotlin”作为我的Kotlin源代码类名的一部分,我不需要添加
元素…maven编译器插件添加默认执行default compile
和default testCompile
,它们首先在相应的阶段运行
为了在Java代码中使用Kotlin类,您需要在Java编译器运行之前运行Kotlin编译器。一种方法是将其执行安排到处理源
阶段。另一种方法是“不计划”Java插件的默认执行,并在执行Kotlin插件之后计划新的执行
使用pom.xml的此部分关闭Java插件的默认执行:
<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>
...
</executions>
</plugin>
org.apache.maven.plugins
maven编译器插件添加了在相应阶段首先运行的默认执行default compile
和default testCompile
为了在Java代码中使用Kotlin类,您需要在Java编译器运行之前运行Kotlin编译器。一种方法是将其执行安排到处理源
阶段。另一种方法是“不计划”Java插件的默认执行,并在执行Kotlin插件之后计划新的执行
使用pom.xml的此部分关闭Java插件的默认执行:
<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>
...
</executions>
</plugin>
org.apache.maven.plugins
我遇到了类似的问题,我试图这样解决它:
原因是java编译器在kotlin编译器之前运行,java类在编译时找不到kotlin类
因此,在pom.xml(根pom.xml)中指定kotlin编译器插件
maven antrun插件
${maven antrun.version}
淘宝网潘多拉
pandora引导maven插件
${pandora boot maven plugin.version}
com.alibaba.citrus.tool
自动配置maven插件
${autoconfig maven plugin.version}
org.jetbrains.kotlin
kotlin maven插件
${kotlin.version}
编译
编译
编译
测试编译
测试编译
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>${maven-antrun.version}</version>
</plugin>
<plugin>
<groupId>com.taobao.pandora</groupId>
<artifactId>pandora-boot-maven-plugin</artifactId>
<version>${pandora-boot-maven-plugin.version}</version>
</plugin>
<plugin>
<groupId>com.alibaba.citrus.tool</groupId>
<artifactId>autoconfig-maven-plugin</artifactId>
<version>${autoconfig-maven-plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<target>${maven.compiler.target}</target>
<source>${maven.compiler.source}</source>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attatch-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>