Convert fileTree as artifact - move from Groovy to Kotlin

Hi,

I am moving from groovy to kotlin for my build files, but I am a little bit lost for this transformation:

groovy one which works:

...
def modelTree = fileTree(projectDir)
modelTree.include("**/*.json")
modelTree.exclude("package.json")
modelTree.exclude("package-lock.json")
modelTree.exclude("node_modules")

artifacts {
	modelConfig(modelTree.getFiles())
}
...

With Kotlin:

...
val modelTree = fileTree(projectDir)
modelTree.include("**/*.json")
modelTree.exclude("package.json")
modelTree.exclude("package-lock.json")
modelTree.exclude("node_modules")

artifacts.add("modelConfig", modelTree.files)

...

But this will generate:

Cannot convert the provided notation to an object of type ConfigurablePublishArtifact: [List-of-Files]

The following types/formats are supported:
  - Instances of ConfigurablePublishArtifact.
  - Instances of PublishArtifact.
  - Instances of AbstractArchiveTask, for example jar.
  - Instances of Provider<RegularFile>.
  - Instances of Provider<Directory>.
  - Instances of Provider<File>.
  - Instances of RegularFile.
  - Instances of Directory.
  - Instances of File.
  - Maps with 'file' key

Wrapping it with providers.provider {modelTree.files} does not help either.

I am sure this is an easy task to do, but I could need a hint in the right direction, thanks.

There is some evil dark magic in the Groovy DSL that iterates over the Collection<File> you give as argument and calls the method for each individually.

So the Kotlin DSL equivalent would be

modelTree.files.forEach {
    artifacts.add("modelConfig", it)
}

Thank you very much, that is really like Radagast once said: “Witchcraft. … A dark and powerful magic” :wink:

1 Like