Skip to content

Commit 9d2dac9

Browse files
authored
[Build] Support publishing maven artifacts directly to maven central (#2397)
* Adjusted nmcp compatibility with scala variants * Fixed dist maven publishing * Fix pom generation to ignore embedded dependencies * Minor cleanup * Build maven aggregation zip as part of dra build
1 parent aae4528 commit 9d2dac9

File tree

8 files changed

+97
-54
lines changed

8 files changed

+97
-54
lines changed

.buildkite/dra.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ mkdir localRepo
4545
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" \
4646
-O "localRepo/build-tools-${HADOOP_VERSION}${VERSION_SUFFIX}.jar"
4747

48-
./gradlew -S -PlocalRepo=true "${BUILD_ARGS[@]}" -Dorg.gradle.warning.mode=summary -Dcsv="$WORKSPACE/build/distributions/dependencies-${HADOOP_VERSION}${VERSION_SUFFIX}.csv" :dist:generateDependenciesReport distribution
48+
./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
4949

5050
# Allow other users access to read the artifacts so they are readable in the container
5151
find "$WORKSPACE" -type f -path "*/build/distributions/*" -exec chmod a+r {} \;

build.gradle

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import org.elasticsearch.hadoop.gradle.buildtools.ConcatFilesTask
22
import java.lang.management.ManagementFactory;
33
import java.time.LocalDateTime;
4+
import org.elasticsearch.gradle.VersionProperties
45

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

1314
apply plugin: 'es.hadoop.build.root'
15+
apply plugin: 'com.gradleup.nmcp.aggregation'
1416

1517
defaultTasks 'build'
1618

19+
dependencies {
20+
nmcpAggregation(project(":dist"))
21+
nmcpAggregation(project(":elasticsearch-hadoop-mr"))
22+
nmcpAggregation(project(":elasticsearch-hadoop-hive"))
23+
nmcpAggregation(project(":elasticsearch-spark-30"))
24+
}
25+
26+
tasks.named('zipAggregation').configure {
27+
archiveFileName.unset();
28+
archiveBaseName.set("elasticsearch-maven-aggregration")
29+
archiveVersion.set(VersionProperties.elasticsearch)
30+
}
31+
1732
allprojects {
1833
group = "org.elasticsearch"
1934
tasks.withType(AbstractCopyTask) {

buildSrc/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ dependencies {
7272
// Required for dependency licenses task
7373
implementation 'org.apache.rat:apache-rat:0.11'
7474
implementation 'commons-codec:commons-codec:1.12'
75+
implementation 'com.gradleup.nmcp:nmcp:0.1.4'
7576

7677
if (localRepo) {
7778
implementation name: "build-tools-${buildToolsVersion}"

buildSrc/src/main/groovy/org/elasticsearch/hadoop/gradle/BuildPlugin.groovy

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@ class BuildPlugin implements Plugin<Project> {
567567

568568
private void configureMaven(Project project) {
569569
project.getPluginManager().apply("maven-publish")
570+
project.getPluginManager().apply("com.gradleup.nmcp")
570571

571572
// Configure Maven publication
572573
project.publishing {
@@ -587,13 +588,6 @@ class BuildPlugin implements Plugin<Project> {
587588
// Configure Maven Pom
588589
configurePom(project, project.publishing.publications.main)
589590

590-
// Disable the publishing tasks since we only need the pom generation tasks.
591-
// If we are working with a project that has a scala variant (see below), we need to modify the pom's
592-
// artifact id which the publish task does not like (it fails validation when run).
593-
project.getTasks().withType(PublishToMavenRepository) { PublishToMavenRepository m ->
594-
m.enabled = false
595-
}
596-
597591
// Configure Scala Variants if present
598592
project.getPlugins().withType(SparkVariantPlugin).whenPluginAdded {
599593
// Publishing gets weird when you introduce variants into the project.
@@ -608,7 +602,8 @@ class BuildPlugin implements Plugin<Project> {
608602

609603
// Main variant needs the least configuration on its own, since it is the default publication created above.
610604
sparkVariants.defaultVariant { SparkVariant variant ->
611-
updateVariantPomLocationAndArtifactId(project, project.publishing.publications.main, variant)
605+
project.publishing.publications.main.setAlias(true)
606+
updateVariantArtifactId(project, project.publishing.publications.main, variant)
612607
}
613608

614609
// For each spark variant added, we need to do a few things:
@@ -659,7 +654,7 @@ class BuildPlugin implements Plugin<Project> {
659654
suppressAllPomMetadataWarnings() // We get it. Gradle metadata is better than Maven Poms
660655
}
661656
configurePom(project, variantPublication)
662-
updateVariantPomLocationAndArtifactId(project, variantPublication, variant)
657+
updateVariantArtifactId(project, variantPublication, variant)
663658
}
664659
}
665660
}
@@ -672,14 +667,6 @@ class BuildPlugin implements Plugin<Project> {
672667
}
673668

674669
private static void configurePom(Project project, MavenPublication publication) {
675-
// Set the pom's destination to the distribution directory
676-
project.tasks.withType(GenerateMavenPom).all { GenerateMavenPom pom ->
677-
if (pom.name == "generatePomFileFor${publication.name.capitalize()}Publication") {
678-
BasePluginExtension baseExtension = project.getExtensions().getByType(BasePluginExtension.class);
679-
pom.destination = project.provider({"${project.buildDir}/distributions/${baseExtension.archivesName.get()}-${project.getVersion()}.pom"})
680-
}
681-
}
682-
683670
// add all items necessary for publication
684671
Provider<String> descriptionProvider = project.provider({ project.getDescription() })
685672
MavenPom pom = publication.getPom()
@@ -722,7 +709,8 @@ class BuildPlugin implements Plugin<Project> {
722709
while (dependenciesIterator.hasNext()) {
723710
Node dependencyNode = dependenciesIterator.next()
724711
String artifact = dependencyNode.get("artifactId").text()
725-
if (artifact == dependency.getName()) {
712+
// handle scala variants by splitting via "_" and checking the first part
713+
if (artifact =~ dependency.getName().split('_')[0]) {
726714
dependenciesIterator.remove()
727715
break
728716
}
@@ -732,23 +720,11 @@ class BuildPlugin implements Plugin<Project> {
732720
}
733721
}
734722

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

754730
/**

dist/build.gradle

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import jdk.internal.foreign.abi.Binding
12
import org.elasticsearch.hadoop.gradle.buildtools.ConcatFilesTask
23
import org.elasticsearch.hadoop.gradle.buildtools.DependenciesInfoTask
34
import org.elasticsearch.hadoop.gradle.buildtools.DependencyLicensesTask
@@ -132,26 +133,6 @@ javadoc {
132133
}
133134
}
134135

135-
publishing {
136-
publications {
137-
main {
138-
getPom().withXml { XmlProvider xml ->
139-
Node root = xml.asNode()
140-
141-
// add clojars repo to pom
142-
Node repositories = root.appendNode('repositories')
143-
Node repository = repositories.appendNode('repository')
144-
repository.appendNode('id', 'clojars.org')
145-
repository.appendNode('url', 'https://clojars.org/repo')
146-
BasePluginExtension baseExtension = project.getExtensions().getByType(BasePluginExtension.class)
147-
148-
// Correct the artifact Id, otherwise it is listed as 'dist'
149-
root.get('artifactId').get(0).setValue(baseExtension.archivesName.get())
150-
}
151-
}
152-
}
153-
}
154-
155136
// Name of the directory under the root of the zip file that will contain the zip contents
156137
String zipContentDir = "elasticsearch-hadoop-${project.version}"
157138

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

180161
distribution {
181162
dependsOn(distZip)
163+
164+
}
165+
166+
167+
publishing {
168+
publications {
169+
main {
170+
artifact tasks.named('distZip')
171+
getPom().withXml { XmlProvider xml ->
172+
Node root = xml.asNode()
173+
174+
// add clojars repo to pom
175+
Node repositories = root.appendNode('repositories')
176+
Node repository = repositories.appendNode('repository')
177+
repository.appendNode('id', 'clojars.org')
178+
repository.appendNode('url', 'https://clojars.org/repo')
179+
}
180+
}
181+
}
182182
}
183183

184+
184185
// Add a task in the root project that collects all the dependencyReport data for each project
185186
// Concatenates the dependencies CSV files into a single file
186187
task generateDependenciesReport(type: ConcatFilesTask) { concatDepsTask ->
@@ -197,3 +198,15 @@ task generateDependenciesReport(type: ConcatFilesTask) { concatDepsTask ->
197198
project.tasks.named('dependencyLicenses', DependencyLicensesTask) {
198199
it.dependencies = project.configurations.licenseChecks
199200
}
201+
202+
203+
tasks.register('copyPoms', Copy) {
204+
BasePluginExtension baseExtension = project.getExtensions().getByType(BasePluginExtension.class);
205+
from(tasks.named('generatePomFileForMainPublication'))
206+
into(new File(project.buildDir, 'distributions'))
207+
rename 'pom-default.xml', "${baseExtension.archivesName.get()}-${project.getVersion()}.pom"
208+
}
209+
210+
tasks.named('distribution').configure {
211+
dependsOn 'copyPoms'
212+
}

hive/build.gradle

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,15 @@ itestJar {
8888
include "META-INF/services/*"
8989
}
9090
}
91+
92+
93+
tasks.register('copyPoms', Copy) {
94+
BasePluginExtension baseExtension = project.getExtensions().getByType(BasePluginExtension.class);
95+
from(tasks.named('generatePomFileForMainPublication'))
96+
into(new File(project.buildDir, 'distributions'))
97+
rename 'pom-default.xml', "${baseExtension.archivesName.get()}-${project.getVersion()}.pom"
98+
}
99+
100+
tasks.named('distribution').configure {
101+
dependsOn 'copyPoms'
102+
}

mr/build.gradle

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,14 @@ eclipse.classpath.file {
137137
}
138138
}
139139
}
140+
141+
tasks.register('copyPoms', Copy) {
142+
BasePluginExtension baseExtension = project.getExtensions().getByType(BasePluginExtension.class);
143+
from(tasks.named('generatePomFileForMainPublication'))
144+
into(new File(project.buildDir, 'distributions'))
145+
rename 'pom-default.xml', "${baseExtension.archivesName.get()}-${project.getVersion()}.pom"
146+
}
147+
148+
tasks.named('distribution').configure {
149+
dependsOn 'copyPoms'
150+
}

spark/sql-30/build.gradle

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,18 @@ sparkVariants {
189189
}
190190
}
191191
}
192+
193+
194+
tasks.register('copyPoms', Copy) {
195+
from(tasks.named('generatePomFileForMainPublication')) {
196+
rename 'pom-default.xml', "elasticsearch-spark-30_2.13-${project.getVersion()}.pom"
197+
}
198+
from(tasks.named('generatePomFileForSpark30scala212Publication')) {
199+
rename 'pom-default.xml', "elasticsearch-spark-30_2.12-${project.getVersion()}.pom"
200+
}
201+
into(new File(project.buildDir, 'distributions'))
202+
}
203+
204+
tasks.named('distribution').configure {
205+
dependsOn 'copyPoms'
206+
}

0 commit comments

Comments
 (0)