I’ve got a conventions plugin, that has an extensi...
# codereview
b
I’ve got a conventions plugin, that has an extension containing a field:
val artifactName: Property<String> = project.objects.property(String::class.java)
that is consumed by maven-publish to set the name of the artifact (jar) to be published. a couple of times, due to copy paste errors, folks have complained about failure to publish to artifactory when in fact they’ve just used the same
artifactName
in multiple modules of a project. Does this approach make sense for identifying this case and proactively failing the build and alerting the user? Am i missing something much simpler?
Copy code
// Add a plugin to detect duplicate artifact names
plugins.withId("maven-publish") {
    // Create a simple task that will run on every project to check for duplicate artifact names
    val checkDuplicateArtifactNames =
        tasks.register("checkDuplicateArtifactNames") {
            group = "verification"
            description = "Checks for duplicate artifact names across all projects"

            doLast {
                // Map to store artifact names and their corresponding projects
                val artifactMap = mutableMapOf<String, MutableList<String>>()

                // Collect all artifact names from all projects
                rootProject.allprojects.forEach { proj ->
                    val projExtension = proj.extensions.findByType(GDConventionsExtension::class.java)
                    if (projExtension != null && projExtension.artifactName.isPresent) {
                        val artifactName = projExtension.artifactName.get()
                        if (!artifactMap.containsKey(artifactName)) {
                            artifactMap[artifactName] = mutableListOf()
                        }
                        artifactMap[artifactName]?.add(proj.path)
                    }
                }

                // Find duplicates (artifact names used by more than one project)
                val duplicates = artifactMap.filter { it.value.size > 1 }

                // If duplicates are found, throw an error with details
                if (duplicates.isNotEmpty()) {
                    val errorMessage = StringBuilder("\nDuplicate artifact names detected:\n")

                    duplicates.forEach { (artifactName, projects) ->
                        errorMessage.append("\n  Artifact name '$artifactName' is used by multiple projects:\n")
                        projects.forEach { projectPath ->
                            errorMessage.append("    - $projectPath\n")
                        }
                    }

                    errorMessage.append("\nPlease ensure each module has a unique artifactName in its fooConventions block.\n")
                    errorMessage.append("For example:\n")
                    errorMessage.append("gdConventions {\n")
                    errorMessage.append("    artifactName.set(\"unique-name\")\n")
                    errorMessage.append("}\n")

                    throw GradleException(errorMessage.toString())
                }
            }
        }

    // Make sure the check runs as part of the build
    tasks.named("build").configure {
        dependsOn(checkDuplicateArtifactNames)
    }
}
ended up using a buildService to avoid having to walk the all sub-projects to create the map in each task invocation.
ugh, 8 days later i realize i posted this in the wrong slack, meant to put it in the gradle slack 😄 sorry