Here is my build.gradle
dependencies {
api 'org.springframework.boot:spring-boot-starter-web'
api 'org.mapstruct:mapstruct'
api 'org.projectlombok:lombok-mapstruct-binding'
annotationProcessor 'org.mapstruct:mapstruct-processor'
}
Here is my Plugin code
project.configurations.forEach(new Consumer<Configuration>() {
@Override
void accept(Configuration files) {
println files.name
println files.dependencies.name
}
})
But the output result is an empty array (as follows)
I would like to know why this is happening and what do I need to do to get the dependency name
Thank you for your help
annotationProcessor
[]
api
[]
apiElements
[]
archives
[]
compileClasspath
[]
compileOnly
[]
compileOnlyApi
[]
default
[]
implementation
[]
mainSourceElements
[]
runtimeClasspath
[]
runtimeElements
[]
runtimeOnly
[]
testAnnotationProcessor
[]
testCompileClasspath
[]
testCompileOnly
[]
testImplementation
[]
testResultsElementsForTest
[]
testRuntimeClasspath
[]
testRuntimeOnly
[]
files.dependencies
returns a DependencySet
, which does not have a name
property. I’m not sure exactly why an error does not occur.
Using Groovy’s spread-dot operator should give you the result you want.
void accept(Configuration files) {
println files.name
println files.dependencies*.name
}
It’s really confusing me that my test project is working with both dependencies.name
and dependencies*.name
. ¯_(ツ)_/¯
Sorry, I should have asked, when is your plugin code executing? Is it in the plugin’s apply
method? If yes, and assuming the plugin is applied before the dependencies are configured, then that’s why the configuration dependency sets are always empty. The apply
method executes as part of the buildscript calling apply plugin: 'abc'
(or the newer plugins { ... }
block).
That’s nothing,I haven’t learned gradle for a long time, and some concepts can’t be understood
My project is an aggregate project, and I configure it in the parent project’s (build.gradle)
The configuration is as follows
subprojects {
apply plugin: 'livk.resources.plugin' //This is my custom plugin
}
Thanks for the reminder, I found a way to deal with it
I should fetch dependencies after Java has executed compilation(If there is a better way, hopefully can guide the following)
Thanks again for your patient guidance
javaCompile.doLast {
project.configurations.forEach {
println it.dependencies*.name
}
}
What would you like to achieve? Why do you want to print out the dependencies?
Aside; I did some more research on Groovy and why files.dependencies.name
works. Basically, when accessing a property on a collection, Groovy behaves like an implicit spread-dot operator was used.
It’s part of Groovy’s GPath expressions. Here’s some additional information I found on null behaviour.