Humphrey
08/30/2023, 10:52 AMHumphrey
08/30/2023, 10:56 AMspecDirName
from the filename (jar).
We use this for the extracted location and we use it to set it on the classpath (sourceSets).
I'm struggling to find a way to modify the sourceSets within the configurations loop in my kotlin script file.
tasks.register('extractApis') {
configurations.spec.each { file ->
def specDirName = file.toString().split("\\" + file.separator).find { it.matches("^[a-z\\-]*-(specs|api)\$") }
copy {
from zipTree(file)
into "$buildDir/extracted/$specDirName"
}
sourceSets.main.java.srcDirs += "$buildDir/generated/$specDirName/src/main/java"
sourceSets.main.resources.srcDirs += "$buildDir/generated/$specDirName/src/main/resources"
}
}
Vampire
08/30/2023, 11:12 AMHumphrey
08/30/2023, 11:13 AMHumphrey
08/30/2023, 11:14 AMVampire
08/30/2023, 11:21 AMsrcDir
, then you also automatically get the needed task dependencies in all tasks that try to access the sources or resources like compilation tasks, source jar tasks, static code analysis tasks, and so on, without the need to declare manual `dependsOn`s which are practically always bad practice unless a lifecycle task is on the left-hand side.Vampire
08/30/2023, 11:22 AMextractApis
in its configuration phase, instead of its execution phase. That means whenever the task is configured, the work is right away done, whether the task is going to executed or not, and in the configuration phase.Vampire
08/30/2023, 11:23 AMsrcDir
also do not match the path where you extract into.Humphrey
08/30/2023, 11:35 AMconfigurations.create("openApiSpec") {
isCanBeResolved = true
isCanBeConsumed = false
}
tasks {
register("extractApis") {
configurations.getByName("openApiSpec").forEach {
val specDirName = Regex("^[a-z\\-]*-(specs|api)").find(it.name)?.value
copy {
from(zipTree(it))
into(layout.buildDirectory.dir("extracted/$specDirName"))
}
}
}
}
Didn't manage to get that regex completely as the groovy one (missing that last part \$
ending part, but seems to give the same result for the dirName. There might be more Jars on the classpath (openApiSpec) which needs to be extracted in a separate (own dir). And have those directory on the source path.
I'm not sure how to break this in separate tasks as we can add multiple jar files in the openApiSpec configuration.Vampire
08/30/2023, 11:42 AM(missing that last partJust useending part\$
$
should be the same then.Vampire
08/30/2023, 11:42 AMAdam S
08/30/2023, 11:42 AMforEach {}
means that Gradle will eagerly fetch the dependencies
• using zipTree
and copy {}
actually uses Project.zipTree
, which will break configuration cache (which isn't hugely important if you don't have it enabled, but it sets off alarm bells for me)
• doing copy operations immediately, instead of in a task action (e.g. in a doLast {}
or doFirst {}
block) is 'allowed', but it means that the task you've written actually doesn't do anything.
Did you try adapting the task I shared yesterday? It should work better https://kotlinlang.slack.com/archives/C19FD9681/p1693314919620089?thread_ts=1693308712.410899&cid=C19FD9681Vampire
08/30/2023, 11:43 AMwhich will break configuration cacheIt will not, because he does this during configuration time, which is of course bad in itself as desribed above 🙂
Humphrey
08/30/2023, 11:43 AM$
at the end I get null for the value.Vampire
08/30/2023, 11:46 AMFile.name
should include the extension if I'm not wrong, but should be the same for GroovyHumphrey
08/30/2023, 11:49 AMAdam S
08/30/2023, 11:49 AMHumphrey
08/30/2023, 11:50 AMxxxx-specs-1.2.3.jar
Humphrey
08/30/2023, 11:51 AMval specDirName = Regex("^[a-z\\-]*-(specs|api)$").find("xxx-specs-1.2.3.jar")?.value
Humphrey
08/30/2023, 12:19 PMVampire
08/30/2023, 12:32 PM$
means end of string, but your string continues with -1.2.3.jar
Vampire
08/30/2023, 12:34 PM