Skip to content

[Build] Support publishing maven artifacts directly to maven central #2397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .buildkite/dra.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ mkdir localRepo
wget --quiet "https://artifacts-$DRA_WORKFLOW.elastic.co/elasticsearch/${ES_BUILD_ID}/maven/org/elasticsearch/gradle/build-tools/${HADOOP_VERSION}${VERSION_SUFFIX}/build-tools-${HADOOP_VERSION}${VERSION_SUFFIX}.jar" \
-O "localRepo/build-tools-${HADOOP_VERSION}${VERSION_SUFFIX}.jar"

./gradlew -S -PlocalRepo=true "${BUILD_ARGS[@]}" -Dorg.gradle.warning.mode=summary -Dcsv="$WORKSPACE/build/distributions/dependencies-${HADOOP_VERSION}${VERSION_SUFFIX}.csv" :dist:generateDependenciesReport distribution
./gradlew -S -PlocalRepo=true "${BUILD_ARGS[@]}" -Dorg.gradle.warning.mode=summary -Dcsv="$WORKSPACE/build/distributions/dependencies-${HADOOP_VERSION}${VERSION_SUFFIX}.csv" :dist:generateDependenciesReport distribution zipAggregation

# Allow other users access to read the artifacts so they are readable in the container
find "$WORKSPACE" -type f -path "*/build/distributions/*" -exec chmod a+r {} \;
Expand Down
15 changes: 15 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import org.elasticsearch.hadoop.gradle.buildtools.ConcatFilesTask
import java.lang.management.ManagementFactory;
import java.time.LocalDateTime;
import org.elasticsearch.gradle.VersionProperties

import org.elasticsearch.gradle.Architecture
import org.elasticsearch.gradle.OS
Expand All @@ -11,9 +12,23 @@ import java.time.LocalDateTime
description = 'Elasticsearch for Apache Hadoop'

apply plugin: 'es.hadoop.build.root'
apply plugin: 'com.gradleup.nmcp.aggregation'

defaultTasks 'build'

dependencies {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we explicitly list the artifacts we want to publish here to avoid any accidental publication

nmcpAggregation(project(":dist"))
nmcpAggregation(project(":elasticsearch-hadoop-mr"))
nmcpAggregation(project(":elasticsearch-hadoop-hive"))
nmcpAggregation(project(":elasticsearch-spark-30"))
}

tasks.named('zipAggregation').configure {
archiveFileName.unset();
archiveBaseName.set("elasticsearch-maven-aggregration")
archiveVersion.set(VersionProperties.elasticsearch)
}

allprojects {
group = "org.elasticsearch"
tasks.withType(AbstractCopyTask) {
Expand Down
1 change: 1 addition & 0 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ dependencies {
// Required for dependency licenses task
implementation 'org.apache.rat:apache-rat:0.11'
implementation 'commons-codec:commons-codec:1.12'
implementation 'com.gradleup.nmcp:nmcp:0.1.4'

if (localRepo) {
implementation name: "build-tools-${buildToolsVersion}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ class BuildPlugin implements Plugin<Project> {

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

// Configure Maven publication
project.publishing {
Expand All @@ -587,13 +588,6 @@ class BuildPlugin implements Plugin<Project> {
// 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.
Expand All @@ -608,7 +602,8 @@ class BuildPlugin implements Plugin<Project> {

// 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)
project.publishing.publications.main.setAlias(true)
updateVariantArtifactId(project, project.publishing.publications.main, variant)
}

// For each spark variant added, we need to do a few things:
Expand Down Expand Up @@ -659,7 +654,7 @@ class BuildPlugin implements Plugin<Project> {
suppressAllPomMetadataWarnings() // We get it. Gradle metadata is better than Maven Poms
}
configurePom(project, variantPublication)
updateVariantPomLocationAndArtifactId(project, variantPublication, variant)
updateVariantArtifactId(project, variantPublication, variant)
}
}
}
Expand All @@ -672,14 +667,6 @@ class BuildPlugin implements Plugin<Project> {
}

private static void configurePom(Project project, MavenPublication publication) {
// Set the pom's destination to the distribution directory
project.tasks.withType(GenerateMavenPom).all { GenerateMavenPom pom ->
if (pom.name == "generatePomFileFor${publication.name.capitalize()}Publication") {
BasePluginExtension baseExtension = project.getExtensions().getByType(BasePluginExtension.class);
pom.destination = project.provider({"${project.buildDir}/distributions/${baseExtension.archivesName.get()}-${project.getVersion()}.pom"})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this results in a deprecation warning and we would not want to rely on that workaround anymore.

}
}

// add all items necessary for publication
Provider<String> descriptionProvider = project.provider({ project.getDescription() })
MavenPom pom = publication.getPom()
Expand Down Expand Up @@ -722,7 +709,8 @@ class BuildPlugin implements Plugin<Project> {
while (dependenciesIterator.hasNext()) {
Node dependencyNode = dependenciesIterator.next()
String artifact = dependencyNode.get("artifactId").text()
if (artifact == dependency.getName()) {
// handle scala variants by splitting via "_" and checking the first part
if (artifact =~ dependency.getName().split('_')[0]) {
dependenciesIterator.remove()
break
}
Expand All @@ -732,23 +720,11 @@ class BuildPlugin implements Plugin<Project> {
}
}

private static void updateVariantPomLocationAndArtifactId(Project project, MavenPublication publication, SparkVariant variant) {
private static void updateVariantArtifactId(Project project, MavenPublication publication, SparkVariant variant) {
// Add variant classifier to the pom file name if required
String classifier = variant.shouldClassifySparkVersion() && variant.isDefaultVariant() == false ? "-${variant.getName()}" : ''
BasePluginExtension baseExtension = project.getExtensions().getByType(BasePluginExtension.class);
String filename = "${baseExtension.archivesName.get()}_${variant.scalaMajorVersion}-${project.getVersion()}${classifier}"
// Fix the pom name
project.tasks.withType(GenerateMavenPom).all { GenerateMavenPom pom ->
if (pom.name == "generatePomFileFor${publication.name.capitalize()}Publication") {
pom.destination = project.provider({"${project.buildDir}/distributions/${filename}.pom"})
}
}
// Fix the artifactId. Note: The publishing task does not like this happening. Hence it is disabled.
publication.getPom().withXml { XmlProvider xml ->
Node root = xml.asNode()
Node artifactId = (root.get('artifactId') as NodeList).get(0) as Node
artifactId.setValue("${baseExtension.archivesName.get()}_${variant.scalaMajorVersion}")
}
// Fix the artifact id
publication.setArtifactId("${baseExtension.archivesName.get()}_${variant.scalaMajorVersion}")
}

/**
Expand Down
53 changes: 33 additions & 20 deletions dist/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import jdk.internal.foreign.abi.Binding
import org.elasticsearch.hadoop.gradle.buildtools.ConcatFilesTask
import org.elasticsearch.hadoop.gradle.buildtools.DependenciesInfoTask
import org.elasticsearch.hadoop.gradle.buildtools.DependencyLicensesTask
Expand Down Expand Up @@ -132,26 +133,6 @@ javadoc {
}
}

publishing {
publications {
main {
getPom().withXml { XmlProvider xml ->
Node root = xml.asNode()

// add clojars repo to pom
Node repositories = root.appendNode('repositories')
Node repository = repositories.appendNode('repository')
repository.appendNode('id', 'clojars.org')
repository.appendNode('url', 'https://clojars.org/repo')
BasePluginExtension baseExtension = project.getExtensions().getByType(BasePluginExtension.class)

// Correct the artifact Id, otherwise it is listed as 'dist'
root.get('artifactId').get(0).setValue(baseExtension.archivesName.get())
}
}
}
}

// Name of the directory under the root of the zip file that will contain the zip contents
String zipContentDir = "elasticsearch-hadoop-${project.version}"

Expand Down Expand Up @@ -179,8 +160,28 @@ task('distZip', type: Zip) {

distribution {
dependsOn(distZip)

}


publishing {
publications {
main {
artifact tasks.named('distZip')
getPom().withXml { XmlProvider xml ->
Node root = xml.asNode()

// add clojars repo to pom
Node repositories = root.appendNode('repositories')
Node repository = repositories.appendNode('repository')
repository.appendNode('id', 'clojars.org')
repository.appendNode('url', 'https://clojars.org/repo')
}
}
}
}


// Add a task in the root project that collects all the dependencyReport data for each project
// Concatenates the dependencies CSV files into a single file
task generateDependenciesReport(type: ConcatFilesTask) { concatDepsTask ->
Expand All @@ -197,3 +198,15 @@ task generateDependenciesReport(type: ConcatFilesTask) { concatDepsTask ->
project.tasks.named('dependencyLicenses', DependencyLicensesTask) {
it.dependencies = project.configurations.licenseChecks
}


tasks.register('copyPoms', Copy) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

temporally we want to keep the build compatible with the existing DRA build. We can remove this workaround once we rely our releases on the new mechanism

BasePluginExtension baseExtension = project.getExtensions().getByType(BasePluginExtension.class);
from(tasks.named('generatePomFileForMainPublication'))
into(new File(project.buildDir, 'distributions'))
rename 'pom-default.xml', "${baseExtension.archivesName.get()}-${project.getVersion()}.pom"
}

tasks.named('distribution').configure {
dependsOn 'copyPoms'
}
12 changes: 12 additions & 0 deletions hive/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,15 @@ itestJar {
include "META-INF/services/*"
}
}


tasks.register('copyPoms', Copy) {
BasePluginExtension baseExtension = project.getExtensions().getByType(BasePluginExtension.class);
from(tasks.named('generatePomFileForMainPublication'))
into(new File(project.buildDir, 'distributions'))
rename 'pom-default.xml', "${baseExtension.archivesName.get()}-${project.getVersion()}.pom"
}

tasks.named('distribution').configure {
dependsOn 'copyPoms'
}
11 changes: 11 additions & 0 deletions mr/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,14 @@ eclipse.classpath.file {
}
}
}

tasks.register('copyPoms', Copy) {
BasePluginExtension baseExtension = project.getExtensions().getByType(BasePluginExtension.class);
from(tasks.named('generatePomFileForMainPublication'))
into(new File(project.buildDir, 'distributions'))
rename 'pom-default.xml', "${baseExtension.archivesName.get()}-${project.getVersion()}.pom"
}

tasks.named('distribution').configure {
dependsOn 'copyPoms'
}
15 changes: 15 additions & 0 deletions spark/sql-30/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,18 @@ sparkVariants {
}
}
}


tasks.register('copyPoms', Copy) {
from(tasks.named('generatePomFileForMainPublication')) {
rename 'pom-default.xml', "elasticsearch-spark-30_2.13-${project.getVersion()}.pom"
}
from(tasks.named('generatePomFileForSpark30scala212Publication')) {
rename 'pom-default.xml', "elasticsearch-spark-30_2.12-${project.getVersion()}.pom"
}
into(new File(project.buildDir, 'distributions'))
}

tasks.named('distribution').configure {
dependsOn 'copyPoms'
}