将Kotlin库用作Java项目中的依赖项

时间:2018-08-29 05:40:46

标签: java maven kotlin shared-libraries

我有一个图书馆项目,该项目为其他项目提供共享功能。该实现是用Kotlin编写的。但是,我确保该库的公共接口不使用Kotlin提供的任何类。

如果我们将此库作为依赖项添加到纯Java项目中,它将可传递地提取kotlin-stdlibkotlin-runtime。现在,可以在仅Java的使用者项目中潜在地(但错误地)使用Kotlin-stdlib中的类。

有没有办法防止这种类型的泄漏?我可以想到的一些选择:

  • 在Maven中将kotlin-stdlib-*标记为provided :这会在运行时引起任何问题吗? (ClassNotFoundException

  • 阴影+重新定位kotlin-stdlib-*和/或kotlin-runtime通过阴影,如果我们依赖多个Kotlin库项目,我可以预见问题。另外,我不确定这是否会隐藏Kotlin类型。

注意:问题Using a kotlin library in java code在这里不适用(标题误导)。我特别避免这种情况。

1 个答案:

答案 0 :(得分:0)

使用implementation关键字来声明您的库依赖项是否足够不传递它们?如here We use the implementation dependency configuration so that we don’t expose any of the Kotlin standard library through our API. This has architectural and performance benefits.