我很难理解api和implementation配置在Gradle中的区别。我知道以前有人问过这个问题,但他们的行为似乎与每个人所描述的完全相反。我有三个分级项目: consumer -> libraryA -> libraryB。libraryA有一个api依赖于libraryB,而使用者对libraryA有一个implementation依赖,但是libraryB在消费者的任何类路径上都是不可用的。当我将libraryA更改为implementation依赖于libraryB时,libraryB就可以在消费者的runtimeClasspath上使用。这似乎不符合每个人的说法。我遗漏了什么?
libraryB
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Kotlin library project to get you started.
*/
plugins {
maven
id("org.jetbrains.kotlin.jvm").version("1.3.31")
}
group = "libraryB"
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// Use the Kotlin JDK 8 standard library.
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
// Use the Kotlin test library.
testImplementation("org.jetbrains.kotlin:kotlin-test")
// Use the Kotlin JUnit integration.
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}libraryA:
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Kotlin library project to get you started.
*/
plugins {
maven
id("org.jetbrains.kotlin.jvm").version("1.3.31")
}
group = "libraryA"
repositories {
mavenLocal()
jcenter()
}
dependencies {
// if I change this to implementation, libraryB will be on the runtime classpath of consumer
api("libraryB:libraryB:unspecified")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
// Use the Kotlin test library.
testImplementation("org.jetbrains.kotlin:kotlin-test")
// Use the Kotlin JUnit integration.
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}消费者:
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Kotlin application project to get you started.
*/
plugins {
// Apply the Kotlin JVM plugin to add support for Kotlin on the JVM.
id("org.jetbrains.kotlin.jvm").version("1.3.31")
// Apply the application plugin to add support for building a CLI application.
application
}
group = "com.consumer"
repositories {
mavenLocal()
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
implementation("libraryA:libraryA:unspecified")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
// Use the Kotlin test library.
testImplementation("org.jetbrains.kotlin:kotlin-test")
// Use the Kotlin JUnit integration.
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}
application {
// Define the main class for the application
mainClassName = "com.consumer.AppKt"
}在消费者中:gradle dependencies
> Task :dependencies
------------------------------------------------------------
Root project
------------------------------------------------------------
-api (n)
No dependencies
-runtime (n)
+--- libraryA:libraryA:unspecified (n)
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31 (n)
annotationProcessor - Annotation processors and their dependencies for source set 'main'.
No dependencies
api - API dependencies for compilation 'main' (target (jvm)). (n)
No dependencies
apiDependenciesMetadata
No dependencies
apiElements - API elements for main. (n)
No dependencies
archives - Configuration for archive artifacts.
No dependencies
compileClasspath - Compile classpath for compilation 'main' (target (jvm)).
+--- libraryA:libraryA:unspecified
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31
+--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31
| +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| \--- org.jetbrains:annotations:13.0
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.31
\--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
compileOnly - Compile only dependencies for compilation 'main' (target (jvm)). (n)
No dependencies
compileOnlyDependenciesMetadata
No dependencies
default - Configuration for default artifacts.
+--- libraryA:libraryA:unspecified
| \--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31
| +--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31
| | +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| | \--- org.jetbrains:annotations:13.0
| \--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.31
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31 (*)
implementation - Implementation only dependencies for compilation 'main' (target (jvm)). (n)
+--- libraryA:libraryA:unspecified (n)
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31 (n)
implementationDependenciesMetadata
+--- libraryA:libraryA:unspecified
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31
+--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31
| +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| \--- org.jetbrains:annotations:13.0
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.31
\--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
kotlinCompilerClasspath
\--- org.jetbrains.kotlin:kotlin-compiler-embeddable:1.3.31
+--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31
| +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| \--- org.jetbrains:annotations:13.0
+--- org.jetbrains.kotlin:kotlin-script-runtime:1.3.31
+--- org.jetbrains.kotlin:kotlin-reflect:1.3.31
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
\--- org.jetbrains.intellij.deps:trove4j:1.0.20181211
kotlinCompilerPluginClasspath
\--- org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:1.3.31
kotlinNativeCompilerPluginClasspath
No dependencies
kotlinScriptDef - Script filename extensions discovery classpath configuration
No dependencies
kotlinScriptDefExtensions
No dependencies
runtimeClasspath - Runtime classpath of compilation 'main' (target (jvm)).
+--- libraryA:libraryA:unspecified
| \--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31
| +--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31
| | +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| | \--- org.jetbrains:annotations:13.0
| \--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.31
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31 (*)
runtimeElements - Elements of runtime for main. (n)
No dependencies
runtimeOnly - Runtime only dependencies for compilation 'main' (target (jvm)). (n)
No dependencies
runtimeOnlyDependenciesMetadata
No dependencies
sourceArtifacts (n)
No dependencies
testAnnotationProcessor - Annotation processors and their dependencies for source set 'test'.
No dependencies
testApi - API dependencies for compilation 'test' (target (jvm)). (n)
No dependencies
testApiDependenciesMetadata
No dependencies
testCompileClasspath - Compile classpath for compilation 'test' (target (jvm)).
+--- libraryA:libraryA:unspecified
+--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31
| +--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31
| | +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| | \--- org.jetbrains:annotations:13.0
| \--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.31
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
+--- org.jetbrains.kotlin:kotlin-test:1.3.31
| +--- org.jetbrains.kotlin:kotlin-test-common:1.3.31
| | \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
\--- org.jetbrains.kotlin:kotlin-test-junit:1.3.31
+--- org.jetbrains.kotlin:kotlin-test-annotations-common:1.3.31
| \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
+--- org.jetbrains.kotlin:kotlin-test:1.3.31 (*)
\--- junit:junit:4.12
\--- org.hamcrest:hamcrest-core:1.3
testCompileOnly - Compile only dependencies for compilation 'test' (target (jvm)). (n)
No dependencies
testCompileOnlyDependenciesMetadata
No dependencies
testImplementation - Implementation only dependencies for compilation 'test' (target (jvm)). (n)
+--- org.jetbrains.kotlin:kotlin-test:1.3.31 (n)
\--- org.jetbrains.kotlin:kotlin-test-junit:1.3.31 (n)
testImplementationDependenciesMetadata
+--- libraryA:libraryA:unspecified
+--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31
| +--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31
| | +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| | \--- org.jetbrains:annotations:13.0
| \--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.31
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
+--- org.jetbrains.kotlin:kotlin-test:1.3.31
| +--- org.jetbrains.kotlin:kotlin-test-common:1.3.31
| | \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
\--- org.jetbrains.kotlin:kotlin-test-junit:1.3.31
+--- org.jetbrains.kotlin:kotlin-test-annotations-common:1.3.31
| \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
+--- org.jetbrains.kotlin:kotlin-test:1.3.31 (*)
\--- junit:junit:4.12
\--- org.hamcrest:hamcrest-core:1.3
testKotlinScriptDef - Script filename extensions discovery classpath configuration
No dependencies
testKotlinScriptDefExtensions
No dependencies
testRuntimeClasspath - Runtime classpath of compilation 'test' (target (jvm)).
+--- libraryA:libraryA:unspecified
| \--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31
| +--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31
| | +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| | \--- org.jetbrains:annotations:13.0
| \--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.31
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
+--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31 (*)
+--- org.jetbrains.kotlin:kotlin-test:1.3.31
| +--- org.jetbrains.kotlin:kotlin-test-common:1.3.31
| | \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
\--- org.jetbrains.kotlin:kotlin-test-junit:1.3.31
+--- org.jetbrains.kotlin:kotlin-test-annotations-common:1.3.31
| \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
+--- org.jetbrains.kotlin:kotlin-test:1.3.31 (*)
\--- junit:junit:4.12
\--- org.hamcrest:hamcrest-core:1.3
testRuntimeOnly - Runtime only dependencies for compilation 'test' (target (jvm)). (n)
No dependencies
testRuntimeOnlyDependenciesMetadata
No dependencies
(*) - dependencies omitted (listed previously)
(n) - Not resolved (configuration is not meant to be resolved)
A web-based, searchable dependency report is available by adding the --scan option.
BUILD SUCCESSFUL in 604ms
1 actionable task: 1 executed现在libraryA有一个implementation依赖于libraryB
> Task :dependencies
------------------------------------------------------------
Root project
------------------------------------------------------------
-api (n)
No dependencies
-runtime (n)
+--- libraryA:libraryA:unspecified (n)
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31 (n)
annotationProcessor - Annotation processors and their dependencies for source set 'main'.
No dependencies
api - API dependencies for compilation 'main' (target (jvm)). (n)
No dependencies
apiDependenciesMetadata
No dependencies
apiElements - API elements for main. (n)
No dependencies
archives - Configuration for archive artifacts.
No dependencies
compileClasspath - Compile classpath for compilation 'main' (target (jvm)).
+--- libraryA:libraryA:unspecified
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31
+--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31
| +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| \--- org.jetbrains:annotations:13.0
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.31
\--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
compileOnly - Compile only dependencies for compilation 'main' (target (jvm)). (n)
No dependencies
compileOnlyDependenciesMetadata
No dependencies
default - Configuration for default artifacts.
+--- libraryA:libraryA:unspecified
| +--- libraryB:libraryB:unspecified
| | \--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31
| | +--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31
| | | +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| | | \--- org.jetbrains:annotations:13.0
| | \--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.31
| | \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
| \--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31 (*)
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31 (*)
implementation - Implementation only dependencies for compilation 'main' (target (jvm)). (n)
+--- libraryA:libraryA:unspecified (n)
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31 (n)
implementationDependenciesMetadata
+--- libraryA:libraryA:unspecified
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31
+--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31
| +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| \--- org.jetbrains:annotations:13.0
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.31
\--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
kotlinCompilerClasspath
\--- org.jetbrains.kotlin:kotlin-compiler-embeddable:1.3.31
+--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31
| +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| \--- org.jetbrains:annotations:13.0
+--- org.jetbrains.kotlin:kotlin-script-runtime:1.3.31
+--- org.jetbrains.kotlin:kotlin-reflect:1.3.31
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
\--- org.jetbrains.intellij.deps:trove4j:1.0.20181211
kotlinCompilerPluginClasspath
\--- org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:1.3.31
kotlinNativeCompilerPluginClasspath
No dependencies
kotlinScriptDef - Script filename extensions discovery classpath configuration
No dependencies
kotlinScriptDefExtensions
No dependencies
runtimeClasspath - Runtime classpath of compilation 'main' (target (jvm)).
+--- libraryA:libraryA:unspecified
| +--- libraryB:libraryB:unspecified
| | \--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31
| | +--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31
| | | +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| | | \--- org.jetbrains:annotations:13.0
| | \--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.31
| | \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
| \--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31 (*)
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31 (*)
runtimeElements - Elements of runtime for main. (n)
No dependencies
runtimeOnly - Runtime only dependencies for compilation 'main' (target (jvm)). (n)
No dependencies
runtimeOnlyDependenciesMetadata
No dependencies
sourceArtifacts (n)
No dependencies
testAnnotationProcessor - Annotation processors and their dependencies for source set 'test'.
No dependencies
testApi - API dependencies for compilation 'test' (target (jvm)). (n)
No dependencies
testApiDependenciesMetadata
No dependencies
testCompileClasspath - Compile classpath for compilation 'test' (target (jvm)).
+--- libraryA:libraryA:unspecified
+--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31
| +--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31
| | +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| | \--- org.jetbrains:annotations:13.0
| \--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.31
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
+--- org.jetbrains.kotlin:kotlin-test:1.3.31
| +--- org.jetbrains.kotlin:kotlin-test-common:1.3.31
| | \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
\--- org.jetbrains.kotlin:kotlin-test-junit:1.3.31
+--- org.jetbrains.kotlin:kotlin-test-annotations-common:1.3.31
| \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
+--- org.jetbrains.kotlin:kotlin-test:1.3.31 (*)
\--- junit:junit:4.12
\--- org.hamcrest:hamcrest-core:1.3
testCompileOnly - Compile only dependencies for compilation 'test' (target (jvm)). (n)
No dependencies
testCompileOnlyDependenciesMetadata
No dependencies
testImplementation - Implementation only dependencies for compilation 'test' (target (jvm)). (n)
+--- org.jetbrains.kotlin:kotlin-test:1.3.31 (n)
\--- org.jetbrains.kotlin:kotlin-test-junit:1.3.31 (n)
testImplementationDependenciesMetadata
+--- libraryA:libraryA:unspecified
+--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31
| +--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31
| | +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| | \--- org.jetbrains:annotations:13.0
| \--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.31
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
+--- org.jetbrains.kotlin:kotlin-test:1.3.31
| +--- org.jetbrains.kotlin:kotlin-test-common:1.3.31
| | \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
\--- org.jetbrains.kotlin:kotlin-test-junit:1.3.31
+--- org.jetbrains.kotlin:kotlin-test-annotations-common:1.3.31
| \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
+--- org.jetbrains.kotlin:kotlin-test:1.3.31 (*)
\--- junit:junit:4.12
\--- org.hamcrest:hamcrest-core:1.3
testKotlinScriptDef - Script filename extensions discovery classpath configuration
No dependencies
testKotlinScriptDefExtensions
No dependencies
testRuntimeClasspath - Runtime classpath of compilation 'test' (target (jvm)).
+--- libraryA:libraryA:unspecified
| +--- libraryB:libraryB:unspecified
| | \--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31
| | +--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31
| | | +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| | | \--- org.jetbrains:annotations:13.0
| | \--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.31
| | \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
| \--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31 (*)
+--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31 (*)
+--- org.jetbrains.kotlin:kotlin-test:1.3.31
| +--- org.jetbrains.kotlin:kotlin-test-common:1.3.31
| | \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.31 (*)
\--- org.jetbrains.kotlin:kotlin-test-junit:1.3.31
+--- org.jetbrains.kotlin:kotlin-test-annotations-common:1.3.31
| \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.31
+--- org.jetbrains.kotlin:kotlin-test:1.3.31 (*)
\--- junit:junit:4.12
\--- org.hamcrest:hamcrest-core:1.3
testRuntimeOnly - Runtime only dependencies for compilation 'test' (target (jvm)). (n)
No dependencies
testRuntimeOnlyDependenciesMetadata
No dependencies
(*) - dependencies omitted (listed previously)
(n) - Not resolved (configuration is not meant to be resolved)
A web-based, searchable dependency report is available by adding the --scan option.
BUILD SUCCESSFUL in 564ms
1 actionable task: 1 executedNote I正在使用6.2.2版本
发布于 2020-03-25 01:26:34
所以我的问题是我没有启用java-library插件。一旦我将它添加到libraryA中,libraryB就成功地出现在了消费者的compileClasspath上。
因此,正确的build.gradle.kts用于libraryA是:
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Kotlin library project to get you started.
*/
plugins {
maven
`java-library`
id("org.jetbrains.kotlin.jvm").version("1.3.31")
}
group = "libraryA"
repositories {
mavenLocal()
jcenter()
}
dependencies {
api("libraryB:libraryB:unspecified")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
// Use the Kotlin test library.
testImplementation("org.jetbrains.kotlin:kotlin-test")
// Use the Kotlin JUnit integration.
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}https://stackoverflow.com/questions/60831210
复制相似问题