Reputation: 551
I am having trouble understanding the difference between the api
and implementation
configurations in Gradle. I know this has been asked before, but they seem to behave exactly oppositely of how everyone describes. I have three Gradle projects: consumer -> libraryA -> libraryB. libraryA has an api
dependency on libraryB, and consumer has an implementation
dependency on libraryA, however libraryB is not available on any classpath in consumer. When I change libraryA to have an implementation
dependency on libraryB, libraryB becomes available on the runtimeClasspath of consumer. This does not seem to comport with what everyone is saying. What am I missing?
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")
}
consumer:
/*
* 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"
}
In consumer: 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
Now with libraryA having an implementation
dependency on 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
Note I am using version 6.2.2
Upvotes: 4
Views: 2168
Reputation: 551
So my problem was that I wasn't enabling the java-library
plugin. Once I added that to libraryA, libraryB successfully appeared on the compileClasspath of consumer.
So the correct build.gradle.kts for libraryA is:
/*
* 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")
}
Upvotes: 5