Skip to content

Commit d61aa40

Browse files
Gradle custom java zippublish plugin (#2988)
* Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> Added custom plugin Signed-off-by: pgodithi <[email protected]> * fixed failure checks Signed-off-by: pgodithi <[email protected]> * fixed failure checks Signed-off-by: pgodithi <[email protected]> * ref String to null Signed-off-by: pgodithi <[email protected]> * Added gradlePlugin config Signed-off-by: pgodithi <[email protected]> * Added gradlePlugin config Signed-off-by: pgodithi <[email protected]> * Added gradlePlugin config Signed-off-by: pgodithi <[email protected]> * Added zipFilePath injection Signed-off-by: pgodithi <[email protected]> * Added custom plugin eval logic Signed-off-by: pgodithi <[email protected]> * Added system property for version check Signed-off-by: pgodithi <[email protected]> * spotlessApply check Signed-off-by: pgodithi <[email protected]> * Added custom plugin eval logic Signed-off-by: pgodithi <[email protected]> * spotlessJavaCheck fix Signed-off-by: pgodithi <[email protected]> * Reverted back existing publish plugin Signed-off-by: pgodithi <[email protected]> * Added feedback code fix Signed-off-by: pgodithi <[email protected]> * modified BUILD_DISTRIBUTIONS_LOCATION to zipDistributionLocation Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * changed the zip artifcat identify logic Signed-off-by: pgodithi <[email protected]> * Changed the zip file identify logic Signed-off-by: pgodithi <[email protected]> * Changed the zip file identify logic Signed-off-by: pgodithi <[email protected]> * Changed the zip file identify logic Signed-off-by: pgodithi <[email protected]> * Changed the zip file identify logic Signed-off-by: pgodithi <[email protected]> * Changed the zip file identify logic Signed-off-by: pgodithi <[email protected]> * Changed the zip file identify logic Signed-off-by: pgodithi <[email protected]> * ran spotlessApply Signed-off-by: pgodithi <[email protected]> * ran spotlessApply Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * fix lint issues Signed-off-by: pgodithi <[email protected]> * Code cleanup Signed-off-by: pgodithi <[email protected]> * Code cleanup Signed-off-by: pgodithi <[email protected]> * Code cleanup Signed-off-by: pgodithi <[email protected]> * Code cleanup typos Signed-off-by: pgodithi <[email protected]> * code cleanup and extended unit tests Signed-off-by: pgodithi <[email protected]> * code cleanup and extended unit tests Signed-off-by: pgodithi <[email protected]> * Naming convention fix Signed-off-by: pgodithi <[email protected]> * Naming convention fix Signed-off-by: pgodithi <[email protected]>
1 parent 1716963 commit d61aa40

File tree

4 files changed

+192
-10
lines changed

4 files changed

+192
-10
lines changed

buildSrc/src/main/java/org/opensearch/gradle/PublishPlugin.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,21 @@ public String call() throws Exception {
125125
// Add git origin info to generated POM files
126126
publication.getPom().withXml(PublishPlugin::addScmInfo);
127127

128-
// have to defer this until archivesBaseName is set
129-
project.afterEvaluate(p -> publication.setArtifactId(getArchivesBaseName(project)));
128+
if (!publication.getName().toLowerCase().contains("zip")) {
130129

131-
// publish sources and javadoc for Java projects.
132-
if (project.getPluginManager().hasPlugin("opensearch.java")) {
133-
publication.artifact(project.getTasks().getByName("sourcesJar"));
134-
publication.artifact(project.getTasks().getByName("javadocJar"));
135-
}
130+
// have to defer this until archivesBaseName is set
131+
project.afterEvaluate(p -> publication.setArtifactId(getArchivesBaseName(project)));
132+
133+
// publish sources and javadoc for Java projects.
134+
if (project.getPluginManager().hasPlugin("opensearch.java")) {
135+
publication.artifact(project.getTasks().getByName("sourcesJar"));
136+
publication.artifact(project.getTasks().getByName("javadocJar"));
137+
}
136138

137-
generatePomTask.configure(
138-
t -> t.dependsOn(String.format("generatePomFileFor%sPublication", Util.capitalize(publication.getName())))
139-
);
139+
generatePomTask.configure(
140+
t -> t.dependsOn(String.format("generatePomFileFor%sPublication", Util.capitalize(publication.getName())))
141+
);
142+
}
140143
});
141144

142145
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
package org.opensearch.gradle.pluginzip;
9+
10+
import java.util.*;
11+
import org.gradle.api.Plugin;
12+
import org.gradle.api.Project;
13+
import org.gradle.api.publish.PublishingExtension;
14+
import org.gradle.api.publish.maven.MavenPublication;
15+
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin;
16+
import java.nio.file.Path;
17+
18+
public class Publish implements Plugin<Project> {
19+
private Project project;
20+
21+
public final static String EXTENSION_NAME = "zipmavensettings";
22+
public final static String PUBLICATION_NAME = "pluginZip";
23+
public final static String STAGING_REPO = "zipStaging";
24+
public final static String PLUGIN_ZIP_PUBLISH_POM_TASK = "generatePomFileForPluginZipPublication";
25+
public final static String LOCALMAVEN = "publishToMavenLocal";
26+
public final static String LOCAL_STAGING_REPO_PATH = "/build/local-staging-repo";
27+
public String zipDistributionLocation = "/build/distributions/";
28+
29+
public static void configMaven(Project project) {
30+
final Path buildDirectory = project.getRootDir().toPath();
31+
project.getPluginManager().apply(MavenPublishPlugin.class);
32+
project.getExtensions().configure(PublishingExtension.class, publishing -> {
33+
publishing.repositories(repositories -> {
34+
repositories.maven(maven -> {
35+
maven.setName(STAGING_REPO);
36+
maven.setUrl(buildDirectory.toString() + LOCAL_STAGING_REPO_PATH);
37+
});
38+
});
39+
publishing.publications(publications -> {
40+
publications.create(PUBLICATION_NAME, MavenPublication.class, mavenZip -> {
41+
String zipGroup = "org.opensearch.plugin";
42+
String zipArtifact = project.getName();
43+
String zipVersion = getProperty("version", project);
44+
mavenZip.artifact(project.getTasks().named("bundlePlugin"));
45+
mavenZip.setGroupId(zipGroup);
46+
mavenZip.setArtifactId(zipArtifact);
47+
mavenZip.setVersion(zipVersion);
48+
});
49+
});
50+
});
51+
}
52+
53+
static String getProperty(String name, Project project) {
54+
if (project.hasProperty(name)) {
55+
Object property = project.property(name);
56+
if (property != null) {
57+
return property.toString();
58+
}
59+
}
60+
return null;
61+
}
62+
63+
@Override
64+
public void apply(Project project) {
65+
this.project = project;
66+
project.afterEvaluate(evaluatedProject -> { configMaven(project); });
67+
project.getGradle().getTaskGraph().whenReady(graph -> {
68+
if (graph.hasTask(LOCALMAVEN)) {
69+
project.getTasks().getByName(PLUGIN_ZIP_PUBLISH_POM_TASK).setEnabled(false);
70+
}
71+
72+
});
73+
}
74+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
implementation-class=org.opensearch.gradle.pluginzip.Publish
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.gradle.pluginzip;
10+
11+
import org.gradle.testkit.runner.BuildResult;
12+
import org.gradle.testkit.runner.GradleRunner;
13+
import org.gradle.testfixtures.ProjectBuilder;
14+
import org.gradle.api.Project;
15+
import org.opensearch.gradle.test.GradleUnitTestCase;
16+
import org.junit.Test;
17+
import java.io.IOException;
18+
import org.gradle.api.publish.maven.tasks.PublishToMavenRepository;
19+
import java.io.File;
20+
import org.gradle.testkit.runner.BuildResult;
21+
import java.io.FileWriter;
22+
import java.io.Writer;
23+
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS;
24+
import static org.junit.Assert.assertEquals;
25+
import java.nio.file.Files;
26+
import org.apache.maven.model.Model;
27+
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
28+
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
29+
import java.io.FileReader;
30+
import org.gradle.api.tasks.bundling.Zip;
31+
32+
public class PublishTests extends GradleUnitTestCase {
33+
34+
@Test
35+
public void testZipPublish() throws IOException, XmlPullParserException {
36+
Project project = ProjectBuilder.builder().build();
37+
String zipPublishTask = "publishPluginZipPublicationToZipStagingRepository";
38+
// Apply the opensearch.pluginzip plugin
39+
project.getPluginManager().apply("opensearch.pluginzip");
40+
// Check if the plugin has been applied to the project
41+
assertTrue(project.getPluginManager().hasPlugin("opensearch.pluginzip"));
42+
// Check if the project has the task from class PublishToMavenRepository after plugin apply
43+
assertNotNull(project.getTasks().withType(PublishToMavenRepository.class));
44+
// Create a mock bundlePlugin task
45+
Zip task = project.getTasks().create("bundlePlugin", Zip.class);
46+
Publish.configMaven(project);
47+
// Check if the main task publishPluginZipPublicationToZipStagingRepository exists after plugin apply
48+
assertTrue(project.getTasks().getNames().contains(zipPublishTask));
49+
assertNotNull("Task to generate: ", project.getTasks().getByName(zipPublishTask));
50+
// Run Gradle functional tests, but calling a build.gradle file, that resembles the plugin publish behavior
51+
File projectDir = new File("build/functionalTest");
52+
// Create a sample plugin zip file
53+
File sampleZip = new File("build/functionalTest/sample-plugin.zip");
54+
Files.createDirectories(projectDir.toPath());
55+
Files.createFile(sampleZip.toPath());
56+
writeString(new File(projectDir, "settings.gradle"), "");
57+
// Generate the build.gradle file
58+
String buildFileContent = "apply plugin: 'maven-publish' \n"
59+
+ "publishing {\n"
60+
+ " repositories {\n"
61+
+ " maven {\n"
62+
+ " url = 'local-staging-repo/'\n"
63+
+ " name = 'zipStaging'\n"
64+
+ " }\n"
65+
+ " }\n"
66+
+ " publications {\n"
67+
+ " pluginZip(MavenPublication) {\n"
68+
+ " groupId = 'org.opensearch.plugin' \n"
69+
+ " artifactId = 'sample-plugin' \n"
70+
+ " version = '2.0.0.0' \n"
71+
+ " artifact('sample-plugin.zip') \n"
72+
+ " }\n"
73+
+ " }\n"
74+
+ "}";
75+
writeString(new File(projectDir, "build.gradle"), buildFileContent);
76+
// Execute the task publishPluginZipPublicationToZipStagingRepository
77+
GradleRunner runner = GradleRunner.create();
78+
runner.forwardOutput();
79+
runner.withPluginClasspath();
80+
runner.withArguments(zipPublishTask);
81+
runner.withProjectDir(projectDir);
82+
BuildResult result = runner.build();
83+
// Check if task publishMavenzipPublicationToZipstagingRepository has ran well
84+
assertEquals(SUCCESS, result.task(":" + zipPublishTask).getOutcome());
85+
// check if the zip has been published to local staging repo
86+
assertTrue(
87+
new File("build/functionalTest/local-staging-repo/org/opensearch/plugin/sample-plugin/2.0.0.0/sample-plugin-2.0.0.0.zip")
88+
.exists()
89+
);
90+
// Parse the maven file and validate the groupID to org.opensearch.plugin
91+
MavenXpp3Reader reader = new MavenXpp3Reader();
92+
Model model = reader.read(
93+
new FileReader("build/functionalTest/local-staging-repo/org/opensearch/plugin/sample-plugin/2.0.0.0/sample-plugin-2.0.0.0.pom")
94+
);
95+
assertEquals(model.getGroupId(), "org.opensearch.plugin");
96+
}
97+
98+
private void writeString(File file, String string) throws IOException {
99+
try (Writer writer = new FileWriter(file)) {
100+
writer.write(string);
101+
}
102+
}
103+
104+
}

0 commit comments

Comments
 (0)