Gradle:compileOnly和runtimeOnly [英] Gradle: compileOnly and runtimeOnly

查看:316
本文介绍了Gradle:compileOnly和runtimeOnly的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会阅读文档,但我我无法理解如何创建一个有效的示例来更好地理解它们之间的差异.

I'd read the documentation but I'm not able to understand how to create a working example to understand better their differences.

我当然还创建了一个游乐场项目,以检查当我使用一个或另一个项目时会发生什么情况.

And ofc I've created a playground project to check what happens when I use one or another.

app.gradle

app.gradle

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$rootProject.kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    compileOnly project(":compileonlylibrary")
    runtimeOnly project(":runtimeonlylibrary")
}

MainActivity.kt

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        FooCompile() // this crash in runtime
        BarRuntime() // this doesn't compiles obviously
    }
}
// FooCompile belongs to compileonlylibrary
// BarRuntime belongs to runtimeonlylibrary

就是这样,我被困在这里,我无法创建适当的示例来提高对Gradle配置的了解.

And that's it, I'm stuck here, I'm not able to create a proper example in order to improve my knowledge of Gradle configurations.

有人可以帮我吗?如果需要,我可以提供更多详细信息.

Could someone give me a hand? I can provide more details if needed.

推荐答案

CompileOnly依赖项在编译时可用,但在运行它们时不可用.

CompileOnly dependencies are available while compiling but not when running them.

这等效于maven中的provided范围.

This is equivalent to the provided scope in maven.

这意味着每个想要执行它的人都需要为库提供CompileOnly库的所有类.

It means that everyone who wants to execute it needs to supply a library with all classes of the CompileOnly library.

例如,您可以创建一个使用SLF4J API的库,并将其设置为CompileOnly.

For example, you could create a library that uses the SLF4J API and you could set it to CompileOnly.

使用该库的任何人都需要(明确地)导入某些版本的SLF4J API才能使用它.

Anyone using the library needs to (explicitely) import some version of the SLF4J API in order to use it.

RuntimeOnly库与此相反,它们在运行时可用,但在编译时不可用.

RuntimeOnly libraries are the opposite, they are available at runtime but not at compile-time.

例如,您在编译时不需要具体的SLF4J记录器(例如logback)(因为您使用SLF4J类来访问它),但是在运行时需要使用它.

For example, you don't need the concrete SLF4J logger(e.g. logback) at compile time (as you use the SLF4J classes in order to access it) but you need it at runtime as you want to use it.

让我们看下面的示例:

您有一个使用SLF4J的库:

You have a library that uses the SLF4J:

compileOnly org.slf4j:slf4j-api:1.7.30

您可以使用该库创建一个项目:

and you could have a project using the library:

implementation project(":yourlibrary")
implementation org.slf4j:slf4j-api:2.0.0-alpha1
runtimeOnly ch.qos.logback:logback:0.5

SLF4J在运行时检测到具体的记录器,它不需要在运行时知道日志库(例如logback).这就是为什么您可以将runtimeOnly用于具体记录器的原因.

SLF4J detects the concrete logger at runtime, it does not need to know the logging library (like logback) at runtime. This is why you can use runtimeOnly for the concrete logger.

请注意,compileOnly广泛用于Jakarta EE,因为博客.

Note that compileOnly is broadly used with Jakarta EE as lots of dependencies are provided by the JEE container/application server as shown in the blog the OP found.

这篇关于Gradle:compileOnly和runtimeOnly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆