Understanding the transitive dependencies of the Kotlin Standard Library with Maven

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

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中有这个配置:

<properties>
    <kotlin.version>1.8.20-RC2</kotlin.version>
</properties>

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

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

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

<plugin>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-maven-plugin</artifactId>
    <version>${kotlin.version}</version>

    // 还有很多其他配置
</plugin>

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

// 列出了许多其他依赖项
[INFO] +- org.jetbrains.kotlin:kotlin-stdlib-jdk8:jar:1.8.20-RC2:compile
[INFO] |  +- org.jetbrains.kotlin:kotlin-stdlib:jar:1.3.72:compile
[INFO] |  |  +- org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.3.72:compile
[INFO] |  |  \- org.jetbrains:annotations:jar:13.0:compile
[INFO] |  \- org.jetbrains.kotlin:kotlin-stdlib-jdk7:jar:1.3.72:compile
[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:

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

and under &lt;depenencies&gt;,

    &lt;dependency&gt;
        &lt;groupId&gt;org.jetbrains.kotlin&lt;/groupId&gt;
        &lt;artifactId&gt;kotlin-stdlib-jdk8&lt;/artifactId&gt;
        &lt;version&gt;${kotlin.version}&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.jetbrains.kotlin&lt;/groupId&gt;
        &lt;artifactId&gt;kotlin-test&lt;/artifactId&gt;
        &lt;version&gt;${kotlin.version}&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;

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

&lt;plugin&gt;
    &lt;groupId&gt;org.jetbrains.kotlin&lt;/groupId&gt;
    &lt;artifactId&gt;kotlin-maven-plugin&lt;/artifactId&gt;
    &lt;version&gt;${kotlin.version}&lt;/version&gt;

    // lots more stuff here
&lt;/plugin&gt;

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

// lots of other dependencies listed
[INFO] +- org.jetbrains.kotlin:kotlin-stdlib-jdk8:jar:1.8.20-RC2:compile
[INFO] |  +- org.jetbrains.kotlin:kotlin-stdlib:jar:1.3.72:compile
[INFO] |  |  +- org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.3.72:compile
[INFO] |  |  \- org.jetbrains:annotations:jar:13.0:compile
[INFO] |  \- org.jetbrains.kotlin:kotlin-stdlib-jdk7:jar:1.3.72:compile
[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:

我的pom文件包含以下内容
    &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>


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

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


我猜测正在发生的情况是,某个其他的依赖项正在强制降低版本。

你使用的是哪个IDE?如果你使用的是IntelliJ,它有一个Maven分析器,可以帮助你找出哪个传递性依赖项被覆盖。

显然,你应该找出这个问题的根本原因,但如果你找不到,你可以像这样在顶层显式重新声明`kotlin-stdlib-common`:

    &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

        &lt;kotlin.version&gt;1.8.10&lt;/kotlin.version&gt;
...    
        &lt;dependency&gt;
            &lt;groupId&gt;org.jetbrains.kotlin&lt;/groupId&gt;
            &lt;artifactId&gt;kotlin-stdlib&lt;/artifactId&gt;
            &lt;version&gt;${kotlin.version}&lt;/version&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;

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

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

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:

        &lt;kotlin.version&gt;1.8.10&lt;/kotlin.version&gt;
...    
        &lt;dependency&gt;
            &lt;groupId&gt;org.jetbrains.kotlin&lt;/groupId&gt;
            &lt;artifactId&gt;kotlin-stdlib&lt;/artifactId&gt;
            &lt;version&gt;${kotlin.version}&lt;/version&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.jetbrains.kotlin&lt;/groupId&gt;
            &lt;artifactId&gt;kotlin-stdlib-common&lt;/artifactId&gt;
            &lt;version&gt;${kotlin.version}&lt;/version&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &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:

确定