我在理解Gradle中api
和implementation
配置之间的区别时遇到了麻烦。我知道这已经被问过了,但是它们的行为似乎与每个人的描述完全相反。我有三个Gradle项目:使用者-> libraryA-> libraryB。 libraryA在libraryB上具有api
依赖性,而consumer在libraryA上具有implementation
依赖性,但是libraryB在消费者中的任何类路径上均不可用。当我将libraryA更改为对libraryB具有implementation
依赖性时,libraryB在使用者的runtimeClasspath上可用。这似乎与所有人所说的不符。我想念什么?
图书馆
* 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")
}
库A:
/*
* 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 executed
注意我正在使用版本6.2.2
最佳答案
所以我的问题是我没有启用java-library
插件。将其添加到libraryA之后,libraryB成功出现在使用者的compileClasspath上。
因此,libraryA的正确build.gradle.kts是:
/*
* 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")
}