Skip to content

Commit 400a5b8

Browse files
lukas-vlceksashashura
authored andcommitted
Further simplification of the ZIP publication implementation (opensearch-project#4360)
A follow-up of PR opensearch-project#4156 that brings in further simplification of the ZIP publication implementation and new tests. It is possible to remove some of the initialization code because the work is already handled by other library under the hood (most likely by NebulaPublishPlugin). For instance the condition to test the `groupId` presence: `if (groupId == null)` was pointless. It was never `true`. If the `project.group` is not defined the publication task fails with an exception, if there is a custom `groupId` value setup in publication config then it overrides the `project.group` as well. Tests are provided to cover these use cases. As for the new tests they cover the following cases: - verify that the task "publishToMavenLocal" gets expected results. The inspiration for this comes from opensearch-project/opensearch-plugin-template-java#35 - verify that applying only the 'opensearch.pluginzip' is enough, because both the NebulaPublish and MavenPublishPlugin plugins are added explicitly and tasks are chained correctly - verify that if the plugin is applied but no publication is defined then a message is printed for the user Signed-off-by: Lukáš Vlček <[email protected]> Signed-off-by: Lukáš Vlček <[email protected]>
1 parent a2e18d3 commit 400a5b8

File tree

10 files changed

+330
-66
lines changed

10 files changed

+330
-66
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
4949
- PUT api for weighted shard routing ([#4272](https://github.com/opensearch-project/OpenSearch/pull/4272))
5050
- Unmute test RelocationIT.testRelocationWhileIndexingRandom ([#4580](https://github.com/opensearch-project/OpenSearch/pull/4580))
5151
- Add DecommissionService and helper to execute awareness attribute decommissioning ([#4084](https://github.com/opensearch-project/OpenSearch/pull/4084))
52+
- Further simplification of the ZIP publication implementation ([#4360](https://github.com/opensearch-project/OpenSearch/pull/4360))
5253

5354
### Deprecated
5455

buildSrc/src/main/java/org/opensearch/gradle/pluginzip/Publish.java

+36-47
Original file line numberDiff line numberDiff line change
@@ -9,83 +9,72 @@
99

1010
import org.gradle.api.Plugin;
1111
import org.gradle.api.Project;
12-
import org.gradle.api.logging.Logger;
13-
import org.gradle.api.logging.Logging;
1412
import org.gradle.api.publish.PublishingExtension;
1513
import org.gradle.api.publish.maven.MavenPublication;
16-
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin;
1714

1815
import java.nio.file.Path;
1916
import org.gradle.api.Task;
17+
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin;
2018

2119
public class Publish implements Plugin<Project> {
2220

23-
private static final Logger LOGGER = Logging.getLogger(Publish.class);
24-
25-
public final static String EXTENSION_NAME = "zipmavensettings";
21+
// public final static String PLUGIN_ZIP_PUBLISH_POM_TASK = "generatePomFileForPluginZipPublication";
2622
public final static String PUBLICATION_NAME = "pluginZip";
2723
public final static String STAGING_REPO = "zipStaging";
28-
public final static String PLUGIN_ZIP_PUBLISH_POM_TASK = "generatePomFileForPluginZipPublication";
29-
public final static String LOCALMAVEN = "publishToMavenLocal";
3024
public final static String LOCAL_STAGING_REPO_PATH = "/build/local-staging-repo";
31-
public String zipDistributionLocation = "/build/distributions/";
25+
// TODO: Does the path ^^ need to use platform dependant file separators ?
26+
27+
private boolean isZipPublicationPresent(Project project) {
28+
PublishingExtension pe = project.getExtensions().findByType(PublishingExtension.class);
29+
if (pe == null) {
30+
return false;
31+
}
32+
return pe.getPublications().findByName(PUBLICATION_NAME) != null;
33+
}
3234

33-
public static void configMaven(Project project) {
35+
private void addLocalMavenRepo(Project project) {
3436
final Path buildDirectory = project.getRootDir().toPath();
35-
project.getPluginManager().apply(MavenPublishPlugin.class);
3637
project.getExtensions().configure(PublishingExtension.class, publishing -> {
3738
publishing.repositories(repositories -> {
3839
repositories.maven(maven -> {
3940
maven.setName(STAGING_REPO);
4041
maven.setUrl(buildDirectory.toString() + LOCAL_STAGING_REPO_PATH);
4142
});
4243
});
44+
});
45+
}
46+
47+
private void addZipArtifact(Project project) {
48+
project.getExtensions().configure(PublishingExtension.class, publishing -> {
4349
publishing.publications(publications -> {
4450
MavenPublication mavenZip = (MavenPublication) publications.findByName(PUBLICATION_NAME);
45-
46-
if (mavenZip == null) {
47-
mavenZip = publications.create(PUBLICATION_NAME, MavenPublication.class);
51+
if (mavenZip != null) {
52+
mavenZip.artifact(project.getTasks().named("bundlePlugin"));
4853
}
49-
50-
String groupId = mavenZip.getGroupId();
51-
if (groupId == null) {
52-
// The groupId is not customized thus we get the value from "project.group".
53-
// See https://docs.gradle.org/current/userguide/publishing_maven.html#sec:identity_values_in_the_generated_pom
54-
groupId = getProperty("group", project);
55-
}
56-
57-
String artifactId = project.getName();
58-
String pluginVersion = getProperty("version", project);
59-
mavenZip.artifact(project.getTasks().named("bundlePlugin"));
60-
mavenZip.setGroupId(groupId);
61-
mavenZip.setArtifactId(artifactId);
62-
mavenZip.setVersion(pluginVersion);
6354
});
6455
});
6556
}
6657

67-
static String getProperty(String name, Project project) {
68-
if (project.hasProperty(name)) {
69-
Object property = project.property(name);
70-
if (property != null) {
71-
return property.toString();
72-
}
73-
}
74-
return null;
75-
}
76-
7758
@Override
7859
public void apply(Project project) {
60+
project.getPluginManager().apply("nebula.maven-base-publish");
61+
project.getPluginManager().apply(MavenPublishPlugin.class);
7962
project.afterEvaluate(evaluatedProject -> {
80-
configMaven(project);
81-
Task validatePluginZipPom = project.getTasks().findByName("validatePluginZipPom");
82-
if (validatePluginZipPom != null) {
83-
project.getTasks().getByName("validatePluginZipPom").dependsOn("generatePomFileForNebulaPublication");
84-
}
85-
Task publishPluginZipPublicationToZipStagingRepository = project.getTasks()
86-
.findByName("publishPluginZipPublicationToZipStagingRepository");
87-
if (publishPluginZipPublicationToZipStagingRepository != null) {
88-
publishPluginZipPublicationToZipStagingRepository.dependsOn("generatePomFileForNebulaPublication");
63+
if (isZipPublicationPresent(project)) {
64+
addLocalMavenRepo(project);
65+
addZipArtifact(project);
66+
Task validatePluginZipPom = project.getTasks().findByName("validatePluginZipPom");
67+
if (validatePluginZipPom != null) {
68+
validatePluginZipPom.dependsOn("generatePomFileForNebulaPublication");
69+
}
70+
Task publishPluginZipPublicationToZipStagingRepository = project.getTasks()
71+
.findByName("publishPluginZipPublicationToZipStagingRepository");
72+
if (publishPluginZipPublicationToZipStagingRepository != null) {
73+
publishPluginZipPublicationToZipStagingRepository.dependsOn("generatePomFileForNebulaPublication");
74+
}
75+
} else {
76+
project.getLogger()
77+
.warn(String.format("Plugin 'opensearch.pluginzip' is applied but no '%s' publication is defined.", PUBLICATION_NAME));
8978
}
9079
});
9180
}

0 commit comments

Comments
 (0)