Skip to content

[Build] Add support for publishing to maven central #128659

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 15 commits into from
Jun 6, 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
123 changes: 62 additions & 61 deletions build-conventions/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,88 +17,89 @@ buildscript {
}

plugins {
id 'java-gradle-plugin'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

applying some default formatting here that doesn't change logic

id 'java-test-fixtures'
id 'eclipse'
id 'java-gradle-plugin'
id 'java-test-fixtures'
id 'eclipse'
}

group = "org.elasticsearch"

// This project contains Checkstyle rule implementations used by IDEs which use a Java 11 runtime
java {
targetCompatibility = 11
sourceCompatibility = 11
targetCompatibility = 17
Copy link
Contributor Author

Choose a reason for hiding this comment

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

nmcp plugin does not support java 11, so we can bump

sourceCompatibility = 17
}

gradlePlugin {
// We already configure publication and we don't need or want the one that comes
// with the java-gradle-plugin
automatedPublishing = false
plugins {
internalLicenseheaders {
id = 'elasticsearch.internal-licenseheaders'
implementationClass = 'org.elasticsearch.gradle.internal.conventions.precommit.LicenseHeadersPrecommitPlugin'
}
eclipse {
id = 'elasticsearch.eclipse'
implementationClass = 'org.elasticsearch.gradle.internal.conventions.EclipseConventionPlugin'
}
publish {
id = 'elasticsearch.publish'
implementationClass = 'org.elasticsearch.gradle.internal.conventions.PublishPlugin'
}
licensing {
id = 'elasticsearch.licensing'
implementationClass = 'org.elasticsearch.gradle.internal.conventions.LicensingPlugin'
}
buildTools {
id = 'elasticsearch.build-tools'
implementationClass = 'org.elasticsearch.gradle.internal.conventions.BuildToolsConventionsPlugin'
}
versions {
id = 'elasticsearch.versions'
implementationClass = 'org.elasticsearch.gradle.internal.conventions.VersionPropertiesPlugin'
}
formatting {
id = 'elasticsearch.formatting'
implementationClass = 'org.elasticsearch.gradle.internal.conventions.precommit.FormattingPrecommitPlugin'
}
// We already configure publication and we don't need or want the one that comes
// with the java-gradle-plugin
automatedPublishing = false
plugins {
internalLicenseheaders {
id = 'elasticsearch.internal-licenseheaders'
implementationClass = 'org.elasticsearch.gradle.internal.conventions.precommit.LicenseHeadersPrecommitPlugin'
}
eclipse {
id = 'elasticsearch.eclipse'
implementationClass = 'org.elasticsearch.gradle.internal.conventions.EclipseConventionPlugin'
}
publish {
id = 'elasticsearch.publish'
implementationClass = 'org.elasticsearch.gradle.internal.conventions.PublishPlugin'
}
licensing {
id = 'elasticsearch.licensing'
implementationClass = 'org.elasticsearch.gradle.internal.conventions.LicensingPlugin'
}
buildTools {
id = 'elasticsearch.build-tools'
implementationClass = 'org.elasticsearch.gradle.internal.conventions.BuildToolsConventionsPlugin'
}
versions {
id = 'elasticsearch.versions'
implementationClass = 'org.elasticsearch.gradle.internal.conventions.VersionPropertiesPlugin'
}
formatting {
id = 'elasticsearch.formatting'
implementationClass = 'org.elasticsearch.gradle.internal.conventions.precommit.FormattingPrecommitPlugin'
}
}
}

repositories {
mavenCentral()
gradlePluginPortal()
mavenCentral()
gradlePluginPortal()
}

dependencies {
api buildLibs.maven.model
api buildLibs.shadow.plugin
api buildLibs.apache.rat
compileOnly buildLibs.checkstyle
constraints {
api("org.eclipse.platform:org.eclipse.osgi:3.18.300") {
because("Use the same version as we do in spotless gradle plugin at runtime")
}
}
api(buildLibs.spotless.plugin) {
exclude module: "groovy-xml"
api buildLibs.maven.model
api buildLibs.shadow.plugin
api buildLibs.apache.rat
api buildLibs.nmcp
Copy link
Contributor Author

Choose a reason for hiding this comment

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

required by the PublishPlugin

compileOnly buildLibs.checkstyle
constraints {
api("org.eclipse.platform:org.eclipse.osgi:3.18.300") {
because("Use the same version as we do in spotless gradle plugin at runtime")
}
}
api(buildLibs.spotless.plugin) {
exclude module: "groovy-xml"
}
}

project.getPlugins().withType(JavaBasePlugin.class) {
java.getModularity().getInferModulePath().set(false);
eclipse.getClasspath().getFile().whenMerged { classpath ->
/*
* give each source folder a unique corresponding output folder
* outside of the usual `build` folder. We can't put the build
* in the usual build folder because eclipse becomes *very* sad
* if we delete it. Which `gradlew clean` does all the time.
*/
classpath.getEntries().findAll{ s -> s instanceof SourceFolder }.eachWithIndex { s, i ->
s.setOutput("out/eclipse" + i)
}
java.getModularity().getInferModulePath().set(false);
eclipse.getClasspath().getFile().whenMerged { classpath ->
Copy link
Contributor Author

Choose a reason for hiding this comment

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

unrelated: we might want to remove all the leftover eclipse related configuration to our build. we do not support that anymore.

/*
* give each source folder a unique corresponding output folder
* outside of the usual `build` folder. We can't put the build
* in the usual build folder because eclipse becomes *very* sad
* if we delete it. Which `gradlew clean` does all the time.
*/
classpath.getEntries().findAll { s -> s instanceof SourceFolder }.eachWithIndex { s, i ->
s.setOutput("out/eclipse" + i)
}
}
}

tasks.withType(JavaCompile).configureEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import com.github.jengelman.gradle.plugins.shadow.ShadowExtension;
import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin;

import nmcp.NmcpPlugin;

import org.elasticsearch.gradle.internal.conventions.info.GitInfo;
import org.elasticsearch.gradle.internal.conventions.precommit.PomValidationPrecommitPlugin;
import org.elasticsearch.gradle.internal.conventions.util.Util;
Expand All @@ -27,6 +29,7 @@
import org.gradle.api.plugins.ExtensionContainer;
import org.gradle.api.plugins.JavaLibraryPlugin;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Provider;
import org.gradle.api.provider.ProviderFactory;
Expand Down Expand Up @@ -65,6 +68,7 @@ public void apply(Project project) {
project.getPluginManager().apply(MavenPublishPlugin.class);
project.getPluginManager().apply(PomValidationPrecommitPlugin.class);
project.getPluginManager().apply(LicensingPlugin.class);
project.getPluginManager().apply(NmcpPlugin.class);
configureJavadocJar(project);
configureSourcesJar(project);
configurePomGeneration(project);
Expand All @@ -82,6 +86,11 @@ private void configurePublications(Project project) {
publication.from(project.getComponents().getByName("java"));
}
});
project.getPlugins().withType(JavaPlugin.class, plugin -> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ensure we include sources and javadoc jars as maven artifacts

var javaPluginExtension = project.getExtensions().getByType(JavaPluginExtension.class);
javaPluginExtension.withJavadocJar();
javaPluginExtension.withSourcesJar();
});
@SuppressWarnings("unchecked")
var projectLicenses = (MapProperty<String, Provider<String>>) project.getExtensions().getExtraProperties().get("projectLicenses");
publication.getPom().withXml(xml -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ class BuildPluginFuncTest extends AbstractGradleFuncTest {
then:
result.task(":assemble").outcome == TaskOutcome.SUCCESS
file("build/distributions/hello-world.jar").exists()
file("build/distributions/hello-world-javadoc.jar").exists()
file("build/distributions/hello-world-sources.jar").exists()
Comment on lines -126 to -127
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are these files no longer produced? I couldn't pinpoint the change which disable that.
I can only see, there is a change which adds tasks for packaging javadoc and sources in the PublishPlugin.java.
Or is it removed just because this check is unrelated to this specific test?

Copy link
Contributor Author

@breskeby breskeby Jun 6, 2025

Choose a reason for hiding this comment

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

Its related to removing the publish plugin from the build plugin. see #128659 (comment)

And there's now better PublishPlugin test coverage for this in PublishPluginFunc

assertValidJar(file("build/distributions/hello-world.jar"))
}

Expand Down Expand Up @@ -162,7 +160,6 @@ class BuildPluginFuncTest extends AbstractGradleFuncTest {
result.task(":forbiddenPatterns").outcome == TaskOutcome.SUCCESS
result.task(":validateModule").outcome == TaskOutcome.SUCCESS
result.task(":splitPackagesAudit").outcome == TaskOutcome.SUCCESS
result.task(":validateElasticPom").outcome == TaskOutcome.SUCCESS
// disabled but check for being on the task graph
result.task(":forbiddenApisMain").outcome == TaskOutcome.SKIPPED
result.task(":checkstyleMain").outcome == TaskOutcome.SKIPPED
Expand Down
Loading