Skip to content

Commit

Permalink
[Backport 2.x] ZIP publication groupId value is configurable (#4156) …
Browse files Browse the repository at this point in the history
…and Further simplification of the ZIP publication implementation (#4360) (#4733)

* This is a backport of #4156 and #4360 to 2.x
* #4156 was cherry-picked from 7fe5830
  -  Resolving conflicts in CHANGELOG.md
* #4360 was cherry-picked from ab6849f
  -  Resolving conflicts in CHANGELOG.md

There is one noticeable difference, the `project.group` property can be empty in which case it will fall back to default value: `org.opensearch.plugin`. See <#4156 (comment)> for more details.

Signed-off-by: Lukáš Vlček <lukas.vlcek@aiven.io>

Signed-off-by: Lukáš Vlček <lukas.vlcek@aiven.io>
  • Loading branch information
lukas-vlcek committed Oct 11, 2022
1 parent 747aa97 commit f4a3369
Show file tree
Hide file tree
Showing 10 changed files with 749 additions and 178 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Change the version to remove deprecated code of adding node name into log pattern of log4j property file ([#4569](https://github.com/opensearch-project/OpenSearch/pull/4569))
- Update to Apache Lucene 9.4.0 ([#4661](https://github.com/opensearch-project/OpenSearch/pull/4661))
- Load the deprecated master role in a dedicated method instead of in setAdditionalRoles() ([#4582](https://github.com/opensearch-project/OpenSearch/pull/4582))
- Plugin ZIP publication groupId value is configurable ([#4156](https://github.com/opensearch-project/OpenSearch/pull/4156))
- Further simplification of the ZIP publication implementation ([#4360](https://github.com/opensearch-project/OpenSearch/pull/4360))

### Deprecated

Expand Down
109 changes: 63 additions & 46 deletions buildSrc/src/main/java/org/opensearch/gradle/pluginzip/Publish.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,81 +9,98 @@

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.publish.Publication;
import org.gradle.api.publish.PublishingExtension;
import org.gradle.api.publish.maven.MavenPublication;
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin;

import java.nio.file.Path;
import org.gradle.api.Task;
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin;

public class Publish implements Plugin<Project> {
public final static String EXTENSION_NAME = "zipmavensettings";
private final static String DEFAULT_GROUP_ID = "org.opensearch.plugin";

// public final static String PLUGIN_ZIP_PUBLISH_POM_TASK = "generatePomFileForPluginZipPublication";
public final static String PUBLICATION_NAME = "pluginZip";
public final static String STAGING_REPO = "zipStaging";
public final static String PLUGIN_ZIP_PUBLISH_POM_TASK = "generatePomFileForPluginZipPublication";
public final static String LOCALMAVEN = "publishToMavenLocal";
public final static String LOCAL_STAGING_REPO_PATH = "/build/local-staging-repo";
public String zipDistributionLocation = "/build/distributions/";
// TODO: Does the path ^^ need to use platform dependant file separators ?

public static void configMaven(Project project) {
/**
* This method returns a "default" groupId value ("{@link #DEFAULT_GROUP_ID}").
* It is possible to have the `group` property unspecified in which case the default value is used instead.
* See <a href="https://github.com/opensearch-project/OpenSearch/pull/4156#issuecomment-1230397082">GitHub discussion</a>
* for details.
*
* @deprecated This method will be removed in OpenSearch 3.x and `group` property will be required
* @return The default groupId value
*/
@Deprecated
public static String getDefaultGroupId(Project project) {
project.getLogger()
.warn(
String.format(
"The 'project.group' property is empty, a default value '%s' will be used instead. "
+ "Please notice that in OpenSearch 3.x the 'project.group' property will be required.",
DEFAULT_GROUP_ID
)
);
return DEFAULT_GROUP_ID;
}

private boolean isZipPublicationPresent(Project project) {
PublishingExtension pe = project.getExtensions().findByType(PublishingExtension.class);
if (pe == null) {
return false;
}
return pe.getPublications().findByName(PUBLICATION_NAME) != null;
}

private void addLocalMavenRepo(Project project) {
final Path buildDirectory = project.getRootDir().toPath();
project.getPluginManager().apply(MavenPublishPlugin.class);
project.getExtensions().configure(PublishingExtension.class, publishing -> {
publishing.repositories(repositories -> {
repositories.maven(maven -> {
maven.setName(STAGING_REPO);
maven.setUrl(buildDirectory.toString() + LOCAL_STAGING_REPO_PATH);
});
});
});
}

private void addZipArtifact(Project project) {
project.getExtensions().configure(PublishingExtension.class, publishing -> {
publishing.publications(publications -> {
final Publication publication = publications.findByName(PUBLICATION_NAME);
if (publication == null) {
publications.create(PUBLICATION_NAME, MavenPublication.class, mavenZip -> {
String zipGroup = "org.opensearch.plugin";
String zipArtifact = project.getName();
String zipVersion = getProperty("version", project);
mavenZip.artifact(project.getTasks().named("bundlePlugin"));
mavenZip.setGroupId(zipGroup);
mavenZip.setArtifactId(zipArtifact);
mavenZip.setVersion(zipVersion);
});
} else {
final MavenPublication mavenZip = (MavenPublication) publication;
String zipGroup = "org.opensearch.plugin";
String zipArtifact = project.getName();
String zipVersion = getProperty("version", project);
MavenPublication mavenZip = (MavenPublication) publications.findByName(PUBLICATION_NAME);
if (mavenZip != null) {
mavenZip.artifact(project.getTasks().named("bundlePlugin"));
mavenZip.setGroupId(zipGroup);
mavenZip.setArtifactId(zipArtifact);
mavenZip.setVersion(zipVersion);
if (mavenZip.getGroupId().isEmpty()) {
mavenZip.setGroupId(getDefaultGroupId(project));
}
}
});
});
}

static String getProperty(String name, Project project) {
if (project.hasProperty(name)) {
Object property = project.property(name);
if (property != null) {
return property.toString();
}
}
return null;
}

@Override
public void apply(Project project) {
project.getPluginManager().apply("nebula.maven-base-publish");
project.getPluginManager().apply(MavenPublishPlugin.class);
project.afterEvaluate(evaluatedProject -> {
configMaven(project);
Task validatePluginZipPom = project.getTasks().findByName("validatePluginZipPom");
if (validatePluginZipPom != null) {
project.getTasks().getByName("validatePluginZipPom").dependsOn("generatePomFileForNebulaPublication");
}
Task publishPluginZipPublicationToZipStagingRepository = project.getTasks()
.findByName("publishPluginZipPublicationToZipStagingRepository");
if (publishPluginZipPublicationToZipStagingRepository != null) {
publishPluginZipPublicationToZipStagingRepository.dependsOn("generatePomFileForNebulaPublication");
if (isZipPublicationPresent(project)) {
addLocalMavenRepo(project);
addZipArtifact(project);
Task validatePluginZipPom = project.getTasks().findByName("validatePluginZipPom");
if (validatePluginZipPom != null) {
validatePluginZipPom.dependsOn("generatePomFileForNebulaPublication");
}
Task publishPluginZipPublicationToZipStagingRepository = project.getTasks()
.findByName("publishPluginZipPublicationToZipStagingRepository");
if (publishPluginZipPublicationToZipStagingRepository != null) {
publishPluginZipPublicationToZipStagingRepository.dependsOn("generatePomFileForNebulaPublication");
}
} else {
project.getLogger()
.warn(String.format("Plugin 'opensearch.pluginzip' is applied but no '%s' publication is defined.", PUBLICATION_NAME));
}
});
}
Expand Down
Loading

0 comments on commit f4a3369

Please sign in to comment.