Thanks for responding quickly,
I do understand that artifact jar task packages the output of driver sourceset and makes it available to the consumer via implementation project(":DatabaseDriver")
and doesn’t isolate it from main as I am trying to do so.
I can do create a custom configuration and associate the artifact with the configuration so that the consumer can consume from that configuration.
DatabaseDriver → build.gradle
plugins {
id 'java-library'
}
repositories {
mavenCentral()
}
configurations {
driverApiJar
}
sourceSets{
driver
}
dependencies {
driverImplementation 'org.postgresql:postgresql:42.6.0'
driverImplementation 'org.xerial:sqlite-jdbc:3.41.2.0'
driverApiJar 'org.postgresql:postgresql:42.6.0'
driverApiJar 'org.xerial:sqlite-jdbc:3.41.2.0'
}
jar{
enabled = false
}
task driverJar(type: Jar){
from sourceSets.driver.output
}
artifacts {
driverApiJar driverJar
}
Consumer → build.gradle
plugins {
id 'java' //used to support java features
id 'application' //used to create java CLI application
}
dependencies {
implementation project(path: ":DatabaseDriver", configuration: "driverApiJar")
}
application {
mainClass = 'org.example.consumer.Main'
}
sourceSets{ main }
I can rename the custom configuration to be driverApi
instead of driverApiJar
and that almost resolves my issue since the dependencies are available on the custom configuration classpath.
Thus when a consumer requests only the specific configuration then they can get the dependency implicitly.
As I had mentioned I just wanted to know why it was done this way, ie only ‘api’ dependency handler is available for java-library
plugin.
if it was available then I could get away without creating a custom configuration and associating artifact with the config etc as shown below.
DatabaseDriver → build.gradle
plugins {
id 'java-library'
}
repositories {
mavenCentral()
}
sourceSets{
driver
}
dependencies {
driverImplementation 'org.postgresql:postgresql:42.6.0'
driverImplementation 'org.xerial:sqlite-jdbc:3.41.2.0'
driverApi 'org.postgresql:postgresql:42.6.0'
driverApi 'org.xerial:sqlite-jdbc:3.41.2.0'
}
jar{
from sourceSets.driver.output
}
Consumer → build.gradle
plugins {
id 'java' //used to support java features
id 'application' //used to create java CLI application
}
dependencies {
implementation project(":DatabaseDriver")
}
application {
mainClass = 'org.example.consumer.Main'
}
sourceSets{ main }
How this helps: Consumer need not know about any custom configurations and can rely on default configuration and default artifact from jar task.
I can merge all the outputs in the jar task so that consumer just needs a single jar whereas I can have multiple sourceSets in my library config.
If I get away with declaring the available “api” dependency handler then it still works, but doesn’t distinguish which transitive api dependency is targeted for which sourceSet (association becomes unclear).
What works now:
DatabaseDriver → build.gradle
plugins {
id 'java-library'
}
repositories {
mavenCentral()
}
sourceSets{
xxxx
yyyy
zzzz
...
}
dependencies {
// xxxx sourceSet internally consumed implementation and externally exported api dependency
xxxxImplementation 'aaaa.bbbb.xxxx:1.0'
xxxxImplementation 'aaaa.bbbb.yyyy:1.0'
api 'aaaa.bbbb.xxxx:1.0'
api 'aaaa.bbbb.yyyy:1.0'
// yyyy sourceSet internally consumed implementation and externally exported api dependency
yyyyImplementation 'cccc.bbbb.xxxx:1.0'
yyyyImplementation 'cccc.bbbb.yyyy:1.0'
api 'cccc.bbbb.xxxx:1.0'
api 'cccc.bbbb.yyyy:1.0'
// zzzz sourceSet internally consumed implementation and externally exported api dependency
zzzzImplementation 'dddd.bbbb.xxxx:1.0'
zzzzImplementation 'dddd.bbbb.yyyy:1.0'
api 'dddd.bbbb.xxxx:1.0'
api 'dddd.bbbb.yyyy:1.0'
...
}
jar{
from sourceSets.xxxx.output
from sourceSets.yyyy.output
from sourceSets.zzzz.output
...
}
What I am requesting for
DatabaseDriver → build.gradle
plugins {
id 'java-library'
}
repositories {
mavenCentral()
}
sourceSets{
xxxx
yyyy
zzzz
...
}
dependencies {
// xxxx sourceSet internally consumed implementation and externally exported api dependency
xxxxImplementation 'aaaa.bbbb.xxxx:1.0'
xxxxImplementation 'aaaa.bbbb.yyyy:1.0'
xxxxApi 'aaaa.bbbb.xxxx:1.0'
xxxxApi 'aaaa.bbbb.yyyy:1.0'
// yyyy sourceSet internally consumed implementation and externally exported api dependency
yyyyImplementation 'cccc.bbbb.xxxx:1.0'
yyyyImplementation 'cccc.bbbb.yyyy:1.0'
yyyyApi 'cccc.bbbb.xxxx:1.0'
yyyyApi 'cccc.bbbb.yyyy:1.0'
// zzzz sourceSet internally consumed implementation and externally exported api dependency
zzzzImplementation 'dddd.bbbb.xxxx:1.0'
zzzzImplementation 'dddd.bbbb.yyyy:1.0'
zzzzApi 'dddd.bbbb.xxxx:1.0'
zzzzApi 'dddd.bbbb.yyyy:1.0'
...
}
jar{
from sourceSets.xxxx.output
from sourceSets.yyyy.output
from sourceSets.zzzz.output
...
}
In both the cases consumer should simply depend on the project alone. ie, output just from jar
task.
Consumer → build.gradle
plugins {
id 'java' //used to support java features
id 'application' //used to create java CLI application
}
dependencies {
implementation project(":DatabaseDriver")
}
application {
mainClass = 'org.example.consumer.Main'
}
sourceSets{ main }
NOTE: The question is still about the “api” dependencyHandler that is unavailable from gradle for custom soureSet.