This message was deleted.
# community-support
s
This message was deleted.
👀 1
j
The issue is this
schema.map { project.zipTree(it) }
The map here is not (!!) the Gradle provider map {}. But the normal Kotlin Collections map {}. Because a configuration can be treated as a set of files (Gradle then sneakily resolves the Configuration…) this method is offered here. This is unfortunately very easy to get wrong. 😞 There is a lot of “legacy” in the Configuration API that would be nice if it could be removed. When you call this map {}, Gradle looses all task dependency information. Instead, you can use
schema.elements
to get a provider for files/folder. On this you can use Gradle’s map{}. I think something like:
schema.elements.map { zipTree(it) }
could work.
👏 2
One note/question. You should not need:
project.artifacts.add("schema-artifact", schemaZip)
Is that used for something? You already “add the artifact” by
Copy code
outgoing {
   artifact(schemaZip)
}
l
Thank you,
schema.elements
brought me on the right track, just needed to tweak the
from
of my copy/unzip task a bit, bc.
zipTree
didn't like collections of files. Ended up with this:
Copy code
val unpackSchemaDependencies by project.tasks.registering(Copy::class) {
    group = "build setup"
    description = "Extrahiert die Schema-Abhängigkeiten"
    from(schema.elements.map {
        var fc = project.files()
        it.forEach { fc.from(project.zipTree(it)) }
        return@map fc
    })
    into(schemaArtifactSpec._dependenciesDir)
}
regarding
project.artifacts.add
: I need the artifact reference to add it to a maven publication, which in turn is tweaked to reference the potential schema dependencies
Copy code
val artifact = project.artifacts.add("schema-artifact", schemaZip)
project.configure<PublishingExtension> {
    publications {
        maybeCreate<MavenPublication>(KONS_PUBLICATION).apply {
            artifact(artifact)

            pom.withXml {

                if (schema.dependencies.isEmpty()) {
                    return@withXml
                }

                val depsNode = (asNode().get("dependencies") as NodeList).firstOrNull() as Node?
                    ?: asNode().appendNode("dependencies")
                schema.resolvedConfiguration.resolvedArtifacts.distinct().forEach {
                    depsNode.appendNode("dependency").apply {
                        appendNode("groupId", it.moduleVersion.id.group)
                        appendNode("artifactId", it.moduleVersion.id.name)
                        appendNode("version", it.moduleVersion.id.version)
                        appendNode("type", it.type)
                    }
                }
            }
        }
    }
}
There is possibly a better way to achieve this, but I haven't figured that out yet 🙂 Thanks again
j
Glad that it helped. The publishing could be “better” modelled using a component (
project.components
). So that the publication is only configured by
from(myComponent)
. And no direct reference of artifacts or modification of pom files. Have a look at: https://docs.gradle.org/current/userguide/publishing_customization.html#sec:publishing-custom-components
🙌 1
l
Thank you, that finally forced me to wrap my head around the components concept. 😀 I almost got it working, the only thing missing is that I can't seem to convince it to produce the "type" attribute in the POM dependencies (I want
<type>zip</type>
). Explicitly setting the type on the outgoing artifact doesn't seem to do the trick and I found no API to map a type for the Maven dependency (e.g. similar to
mapToMavenScope
)
Copy code
val `schema-artifact` by project.configurations.creating {
    isTransitive = true
    isCanBeConsumed = true
    isCanBeResolved = false
    extendsFrom(schema)
    attributes {
        attribute(Category.CATEGORY_ATTRIBUTE, project.objects.named(Category.DOCUMENTATION))
        attribute(DocsType.DOCS_TYPE_ATTRIBUTE, project.objects.named("schema"))
        attribute(Bundling.BUNDLING_ATTRIBUTE, project.objects.named(Bundling.EXTERNAL))
        attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named("schema-artifact"))
    }
    outgoing {
        artifact(schemaZip) {
            type = Zip.ZIP_EXTENSION
        }
    }
}

val schemaComponent = softwareComponentFactory.adhoc("schema")
project.components.add(schemaComponent)
schemaComponent.addVariantsFromConfiguration(`schema-artifact`) {
}

project.configure<PublishingExtension> {
    publications {
        maybeCreate<MavenPublication>(KONS_PUBLICATION).apply {
            from(schemaComponent)
        }
    }
}
well, thinking about it it seems logical that the outgoing artifact type doesn't alter the type attribute for its dependencies in the POM. So in a way I would need to express that the dependencies of the
schema
configuration get type=zip when writing the POM.
j
Why can’t you rely on the Gradle Metadata? There your variant/attributes are published and things should just work.
I think there is no API to do what you want in the POM. You would need to use
pom.withXml
and iterate though the dependencies there to add the “type” attribute. There is an issue for that. And I actually wrote about the workaround there… https://github.com/gradle/gradle/issues/3170#issuecomment-544248344
l
For gradle consumers of those artifacts it will certainly do, but we'll likely have some inhouse teams using Maven. The suggested workaround will do nicely 🙂 Thanks again for all the help 👏
👍 1