Hello, I have a dependency like ```val otelApiJar:...
# community-support
s
Hello, I have a dependency like
Copy code
val otelApiJar: Configuration by configurations.creating {
    isCanBeConsumed = false
    isCanBeResolved = true
}
dependencies {
    otelApiJar(libs.otelInstrumentationAnnotations)
}
in libs.versions.toml
Copy code
[versions]
opentelemetryJavaagent = "2.10.0"

[libraries]
otelInstrumentationAnnotations = { group = "io.opentelemetry.instrumentation", name = "opentelemetry-instrumentation-annotations", version.ref = "opentelemetryJavaagent" }
I want to generate a text file with a list of the files in otelApiJar configuration that will be available as resource file at runtime. I did this
Copy code
tasks {

    val listOtelFiles = register("listOtelFiles"){
        doLast{
            val file = File(sourceSets.main.get().output.resourcesDir,"otel-jars.txt")
            val writer = PrintWriter(file)
            otelApiJar.files.forEach {
                writer.println(it.name)
            }
            writer.close()
        }
    }

    processResources{
        finalizedBy(listOtelFiles)
    }
}
it works, i have a file containing these lines
Copy code
opentelemetry-instrumentation-annotations-2.10.0.jar
opentelemetry-api-1.44.1.jar
opentelemetry-context-1.44.1.jar
is it the right way to do it? can anyone suggest a better way?
t
Your task should output to a directory specific to that task (and also properly declare its inputs and outputs), and then that output directory be added as a resource directory of the sourceSet (that will then be used as input to processResources)
☝️ 1
s
I get it. Thank you!
t
Copy code
abstract class ListFiles : DefaultTask() {
  @get:InputFiles @get:PathSensitive(PathSensitivity.NAME_ONLY) abstract val files: ConfigurableFileCollection
  @get:OutputDirectory abstract DirectoryProperty outputDir
  @get:Input abstract val filename: Property<String>

  @TaskAction
  fun listFiles() {
    outputDir.file(filename).get().asFile.printWriter().use { out ->
      files.forEach {
        out.println(it.name)
      }
    }
  }
}

val listOtelFiles by tasks.registering(ListFiles::class) {
  files.from(otelApiJar)
  outputFile = layout.buildDirectory.dir("otel-files")
  filename = "otel-files.txt"
}

sourceSets {
  main {
    resources {
      srcDir(listOtelFiles)
    }
  }
}
(untested)
s
Thank you! I will use it
e
in this case it's doable with an ad-hoc task too
Copy code
val listOtelFiles by tasks.registering {
    inputs.files(otelApiJar)
    val outputDirectory = layout.buildDirectory.dir("reports/listOtelFiles")
    outputs.dir(outputDirectory)
    doLast {
        val outputDir = outputDirectory.get()
        val outputFile = outputDir.file("otel-jars.txt")
        outputDir.asFile.mkdirs()
        outputFile.asFile.bufferedWriter().use { writer ->
            for (file in inputs.files) writer.appendLine(file.name)
        }
    }
}
sourceSets.main {
    resources.srcDir(listOtelFiles)
}
but a custom task class allows for other Gradle features to be used, such as annotating the input with
@PathSensitive
🙏 1
v
You can also define the path sensitivity with ad-hoc api. Afair the only thing you cannot do ad-hoc is nested input and options.
e
yes,
.withPathSensitivity(...)
but at some point extracting the logic out is probably a better idea
v
Definitely 🙂
t
nit: edited above to add
@get:PathSensitive(PathSensitivity.NAME_ONLY)