Understanding the transitive dependencies of the Kotlin Standard Library with Maven

huangapple go评论84阅读模式
英文:

Understanding the the transitive dependencies of the Kotlin Standard Library with Maven

问题

我正在尝试理解不同版本的Kotlin库与Maven Kotlin插件之间的关系。如下面的配置所示,我将Kotlin版本设置为1.8.20-RC2,但出于我不明白的原因,它获取了Kotlin stdlib的版本1.3.72。我想要stdlib版本1.5中的某些功能,但我不明白为什么只得到了版本1.3

我在我的pom.xml中有这个配置:

  1. <properties>
  2. <kotlin.version>1.8.20-RC2</kotlin.version>
  3. </properties>

<dependencies>下,还有以下配置:

  1. <dependency>
  2. <groupId>org.jetbrains.kotlin</groupId>
  3. <artifactId>kotlin-stdlib-jdk8</artifactId>
  4. <version>${kotlin.version}</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.jetbrains.kotlin</groupId>
  8. <artifactId>kotlin-test</artifactId>
  9. <version>${kotlin.version}</version>
  10. <scope>test</scope>
  11. </dependency>

<build><plugins>...</plugins></build>部分下,有以下配置:

  1. <plugin>
  2. <groupId>org.jetbrains.kotlin</groupId>
  3. <artifactId>kotlin-maven-plugin</artifactId>
  4. <version>${kotlin.version}</version>
  5. // 还有很多其他配置
  6. </plugin>

但是,然后我运行了mvn dependency:tree,看看它显示了什么:

  1. // 列出了许多其他依赖项
  2. [INFO] +- org.jetbrains.kotlin:kotlin-stdlib-jdk8:jar:1.8.20-RC2:compile
  3. [INFO] | +- org.jetbrains.kotlin:kotlin-stdlib:jar:1.3.72:compile
  4. [INFO] | | +- org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.3.72:compile
  5. [INFO] | | \- org.jetbrains:annotations:jar:13.0:compile
  6. [INFO] | \- org.jetbrains.kotlin:kotlin-stdlib-jdk7:jar:1.3.72:compile
  7. [INFO] \- org.jetbrains.kotlin:kotlin-test:jar:1.8.20-RC2:test

我不是专家,但如果我理解正确的话,版本1.8.20-RC21.3.72作为传递依赖项引入了。我确认我无法在我的代码中使用一个在1.3中不可用但在版本1.5或更高版本可用的函数firstNotNullOfOrNull

有人能解释一下这里发生了什么,以及我如何获取stdlib的较新版本吗?

英文:

I'm trying to understand the relationship between different versions of Kotlin libraries the Maven Kotlin plugin. As the configuration below shows, I'm setting my Kotlin version to 1.8.20-RC2, but for reasons I don't understand, it's getting version 1.3.72 of the Kotlin stdlib. There are features in stdlib version 1.5 that I want and I can't understand why I'm only getting version 1.3.

I've got this in my pom.xml:

  1. &lt;properties&gt;
  2. &lt;kotlin.version&gt;1.8.20-RC2&lt;/kotlin.version&gt;
  3. &lt;/properties&gt;

and under &lt;depenencies&gt;,

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;org.jetbrains.kotlin&lt;/groupId&gt;
  3. &lt;artifactId&gt;kotlin-stdlib-jdk8&lt;/artifactId&gt;
  4. &lt;version&gt;${kotlin.version}&lt;/version&gt;
  5. &lt;/dependency&gt;
  6. &lt;dependency&gt;
  7. &lt;groupId&gt;org.jetbrains.kotlin&lt;/groupId&gt;
  8. &lt;artifactId&gt;kotlin-test&lt;/artifactId&gt;
  9. &lt;version&gt;${kotlin.version}&lt;/version&gt;
  10. &lt;scope&gt;test&lt;/scope&gt;
  11. &lt;/dependency&gt;

and under &lt;build&gt;&lt;plugins&gt;...&lt;/plugins&gt;&lt;/build&gt; section:

  1. &lt;plugin&gt;
  2. &lt;groupId&gt;org.jetbrains.kotlin&lt;/groupId&gt;
  3. &lt;artifactId&gt;kotlin-maven-plugin&lt;/artifactId&gt;
  4. &lt;version&gt;${kotlin.version}&lt;/version&gt;
  5. // lots more stuff here
  6. &lt;/plugin&gt;

But then, I ran mvn dependency:tree, and look what it shows:

  1. // lots of other dependencies listed
  2. [INFO] +- org.jetbrains.kotlin:kotlin-stdlib-jdk8:jar:1.8.20-RC2:compile
  3. [INFO] | +- org.jetbrains.kotlin:kotlin-stdlib:jar:1.3.72:compile
  4. [INFO] | | +- org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.3.72:compile
  5. [INFO] | | \- org.jetbrains:annotations:jar:13.0:compile
  6. [INFO] | \- org.jetbrains.kotlin:kotlin-stdlib-jdk7:jar:1.3.72:compile
  7. [INFO] \- org.jetbrains.kotlin:kotlin-test:jar:1.8.20-RC2:test

I'm not an expert at this, but if I understand this right, version 1.8.20-RC2 is bringing in 1.3.72 as a transitive dependency. I confirmed that I am unable to use a function in my code, firstNotNullOfOrNull that is not available in 1.3 but is available on versions 1.5 or later.

Can someone explain what is going on here, and how I can get a later version of the stdlib?

答案1

得分: 1

我不确定你在这里的具体问题,因为该工件确实依赖于正确的stdlib和stdlib-jdk7,如我们可以在Maven Central上的pom中看到的那样:
https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.8.20-RC2/kotlin-stdlib-jdk8-1.8.20-RC2.pom

但无论如何,请注意,自从Kotlin 1.8以来,那些kotlin-stdlib-jdk7kotlin-stdlib-jdk8工件已经与常规的kotlin-stdlib合并,所以你不应该再使用它们。

而是直接使用kotlin-stdlib,可能会彻底解决问题。

英文:

I'm not sure about your specific issue here, because the artifact does depend on the correct stdlib and stdlib-jdk7, as we can see in the pom on Maven Central:
https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.8.20-RC2/kotlin-stdlib-jdk8-1.8.20-RC2.pom

But anyway, note that those kotlin-stdlib-jdk7 and kotlin-stdlib-jdk8 artifacts have been merged with the regular kotlin-stdlib since Kotlin 1.8, so you shouldn't use them anymore.

Instead, just use kotlin-stdlib directly, it might solve the problem altogether.

答案2

得分: 0

Here is the translation of the code snippet you provided:

  1. 我的pom文件包含以下内容
  1. &lt;kotlin.version&gt;1.8.10&lt;/kotlin.version&gt;

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

  1. 我得到了你可能期望在依赖图中看到的内容

INFO] +- org.jetbrains.kotlin:kotlin-stdlib:jar:1.8.10:provided
[INFO] | +- org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.8.10:provided
[INFO] | - org.jetbrains:annotations:jar:13.0:provided

  1. 我猜测正在发生的情况是,某个其他的依赖项正在强制降低版本。
  2. 你使用的是哪个IDE?如果你使用的是IntelliJ,它有一个Maven分析器,可以帮助你找出哪个传递性依赖项被覆盖。
  3. 显然,你应该找出这个问题的根本原因,但如果你找不到,你可以像这样在顶层显式重新声明`kotlin-stdlib-common`
  1. &lt;kotlin.version&gt;1.8.10&lt;/kotlin.version&gt;

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

Please note that I've provided the translation without the code part as per your request.

英文:

My pom has this

  1. &lt;kotlin.version&gt;1.8.10&lt;/kotlin.version&gt;
  2. ...
  3. &lt;dependency&gt;
  4. &lt;groupId&gt;org.jetbrains.kotlin&lt;/groupId&gt;
  5. &lt;artifactId&gt;kotlin-stdlib&lt;/artifactId&gt;
  6. &lt;version&gt;${kotlin.version}&lt;/version&gt;
  7. &lt;scope&gt;provided&lt;/scope&gt;
  8. &lt;/dependency&gt;

and I get what you might be expecting on the dependency graph

  1. INFO] +- org.jetbrains.kotlin:kotlin-stdlib:jar:1.8.10:provided
  2. [INFO] | +- org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.8.10:provided
  3. [INFO] | \- org.jetbrains:annotations:jar:13.0:provided

My guess about what is happening is that some other dependency is forcing the version down.

What IDE are you using. If you are using IntelliJ is has a Maven analyser which will help you find that a transitive dependency is being overriden.

Obviously you should find the source of that, but if you cannot you can just re-declare the kotlin-stdlib-common explicitly at the top level like this:

  1. &lt;kotlin.version&gt;1.8.10&lt;/kotlin.version&gt;
  2. ...
  3. &lt;dependency&gt;
  4. &lt;groupId&gt;org.jetbrains.kotlin&lt;/groupId&gt;
  5. &lt;artifactId&gt;kotlin-stdlib&lt;/artifactId&gt;
  6. &lt;version&gt;${kotlin.version}&lt;/version&gt;
  7. &lt;scope&gt;provided&lt;/scope&gt;
  8. &lt;/dependency&gt;
  9. &lt;dependency&gt;
  10. &lt;groupId&gt;org.jetbrains.kotlin&lt;/groupId&gt;
  11. &lt;artifactId&gt;kotlin-stdlib-common&lt;/artifactId&gt;
  12. &lt;version&gt;${kotlin.version}&lt;/version&gt;
  13. &lt;scope&gt;provided&lt;/scope&gt;
  14. &lt;/dependency&gt;

huangapple
  • 本文由 发表于 2023年6月12日 23:00:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定