private void configureMaven()

in buildSrc/src/main/groovy/org/elasticsearch/hadoop/gradle/BuildPlugin.groovy [568:672]


    private void configureMaven(Project project) {
        project.getPluginManager().apply("maven-publish")

        // Configure Maven publication
        project.publishing {
            publications {
                main(MavenPublication) {
                    from project.components.java
                    suppressAllPomMetadataWarnings() // We get it. Gradle metadata is better than Maven Poms
                }
            }
            repositories {
                maven {
                    name = 'build'
                    url = "file://${project.buildDir}/repo"
                }
            }
        }

        // Configure Maven Pom
        configurePom(project, project.publishing.publications.main)

        // Disable the publishing tasks since we only need the pom generation tasks.
        // If we are working with a project that has a scala variant (see below), we need to modify the pom's
        // artifact id which the publish task does not like (it fails validation when run).
        project.getTasks().withType(PublishToMavenRepository) { PublishToMavenRepository m ->
            m.enabled = false
        }

        // Configure Scala Variants if present
        project.getPlugins().withType(SparkVariantPlugin).whenPluginAdded {
            // Publishing gets weird when you introduce variants into the project.
            // By default, when adding a spark/scala variant, its outgoing configurations are added to the main java components.
            // The maven publish plugin will take all these variants, smoosh them and their dependencies together, and create
            // one big pom file full of version conflicts. Since spark variants are mutually exclusive, we need to perform a
            // workaround to materialize multiple poms for the different scala variants.
            // TODO: Should this adhoc component configuration work be done in the SparkVariantPlugin?

            SparkVariantPluginExtension sparkVariants = project.getExtensions().getByType(SparkVariantPluginExtension.class)
            def javaComponent = project.components.java

            // Main variant needs the least configuration on its own, since it is the default publication created above.
            sparkVariants.defaultVariant { SparkVariant variant ->
                updateVariantPomLocationAndArtifactId(project, project.publishing.publications.main, variant)
            }

            // For each spark variant added, we need to do a few things:
            sparkVariants.featureVariants { SparkVariant variant ->
                // Collect all the outgoing configurations that are compatible with publication
                def variantConfigurationsToExcludeFromMain = [
                        variant.configuration("apiElements"),
                        variant.configuration("runtimeElements"),
                        variant.configuration('javadocElements'),
                        variant.configuration('sourcesElements'),
                        variant.configuration('test', 'apiElements'),
                        variant.configuration('test', 'runtimeElements'),
                        variant.configuration('itest', 'apiElements'),
                        variant.configuration('itest', 'runtimeElements')
                ]

                // Remove each of those outgoing configurations from the default java component.
                // This will keep the default variant from being smooshed together with conflicting artifacts/dependencies.
                variantConfigurationsToExcludeFromMain.each {
                    javaComponent.withVariantsFromConfiguration(project.configurations.getByName(it)) {
                        skip()
                    }
                }

                // Create an adhoc component for the variant
                def variantComponent = softwareComponentFactory.adhoc("${variant.getName()}Component")
                // Add it to the list of components that this project declares
                project.components.add(variantComponent)
                // Register the variant's outgoing configurations for publication
                variantComponent.addVariantsFromConfiguration(project.configurations.getByName(variant.configuration("apiElements"))) {
                    it.mapToMavenScope("compile")
                }
                variantComponent.addVariantsFromConfiguration(project.configurations.getByName(variant.configuration("runtimeElements"))) {
                    it.mapToMavenScope("runtime")
                }
                variantComponent.addVariantsFromConfiguration(project.configurations.getByName(variant.configuration("javadocElements"))) {
                    it.mapToMavenScope("runtime")
                }
                variantComponent.addVariantsFromConfiguration(project.configurations.getByName(variant.configuration("sourcesElements"))) {
                    it.mapToMavenScope("runtime")
                }

                // Create a publication for this adhoc component to create pom generation and publishing tasks
                project.publishing {
                    publications {
                        MavenPublication variantPublication = create(variant.getName(), MavenPublication) {
                            from variantComponent
                            suppressAllPomMetadataWarnings() // We get it. Gradle metadata is better than Maven Poms
                        }
                        configurePom(project, variantPublication)
                        updateVariantPomLocationAndArtifactId(project, variantPublication, variant)
                    }
                }
            }
        }

        // Set the pom generation tasks as required for the distribution task.
        project.tasks.withType(GenerateMavenPom).all { GenerateMavenPom pom ->
            project.getTasks().getByName('distribution').dependsOn(pom)
        }
    }