-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Gradle custom java zippublish plugin #2988
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
dblock
merged 65 commits into
opensearch-project:2.0
from
prudhvigodithi:gradleplugin-2.0
Apr 27, 2022
Merged
Changes from all commits
Commits
Show all changes
65 commits
Select commit
Hold shift + click to select a range
d60eafe
Added custom plugin
prudhvigodithi 4a61852
fixed failure checks
prudhvigodithi 1e8c5f1
fixed failure checks
prudhvigodithi 5e87cde
ref String to null
prudhvigodithi 770c886
Added gradlePlugin config
prudhvigodithi 342bc64
Added gradlePlugin config
prudhvigodithi a8647ec
Added gradlePlugin config
prudhvigodithi 0865be2
Added zipFilePath injection
prudhvigodithi be37b91
Added custom plugin eval logic
prudhvigodithi 8a7c18e
Added system property for version check
prudhvigodithi a43e651
spotlessApply check
prudhvigodithi a523ab1
Added custom plugin eval logic
prudhvigodithi e8a11b7
spotlessJavaCheck fix
prudhvigodithi cf9e291
Reverted back existing publish plugin
prudhvigodithi 88d21d4
Added feedback code fix
prudhvigodithi 056d06e
modified BUILD_DISTRIBUTIONS_LOCATION to zipDistributionLocation
prudhvigodithi edf9028
fix lint issues
prudhvigodithi 6baf646
fix lint issues
prudhvigodithi f8a528c
changed the zip artifcat identify logic
prudhvigodithi ebd85a3
Changed the zip file identify logic
prudhvigodithi 26d83ee
Changed the zip file identify logic
prudhvigodithi cf9ee09
Changed the zip file identify logic
prudhvigodithi 1aeb73a
Changed the zip file identify logic
prudhvigodithi b98ab64
Changed the zip file identify logic
prudhvigodithi bdf32dd
Merge branch 'gradleplugin-2.2' of github.com:prudhvigodithi/OpenSear…
prudhvigodithi 321ec96
Changed the zip file identify logic
prudhvigodithi 5e97819
ran spotlessApply
prudhvigodithi 9ccbf81
ran spotlessApply
prudhvigodithi 8b85320
fix lint issues
prudhvigodithi 4d83dd1
fix lint issues
prudhvigodithi ac2f151
fix lint issues
prudhvigodithi daa06c3
fix lint issues
prudhvigodithi 88d4562
fix lint issues
prudhvigodithi dba1cac
fix lint issues
prudhvigodithi 36a98f9
fix lint issues
prudhvigodithi 5e3ca97
fix lint issues
prudhvigodithi f1da84e
fix lint issues
prudhvigodithi 71b1337
fix lint issues
prudhvigodithi 15cd29f
fix lint issues
prudhvigodithi 122d48f
fix lint issues
prudhvigodithi 30ca4c6
fix lint issues
prudhvigodithi c733586
fix lint issues
prudhvigodithi 89bf24a
fix lint issues
prudhvigodithi 416e74e
fix lint issues
prudhvigodithi 02a8234
fix lint issues
prudhvigodithi ad1e589
fix lint issues
prudhvigodithi 2f9bca8
fix lint issues
prudhvigodithi ed0d1b0
fix lint issues
prudhvigodithi 4445608
fix lint issues
prudhvigodithi b48cf2e
fix lint issues
prudhvigodithi 710611c
fix lint issues
prudhvigodithi b61104d
fix lint issues
prudhvigodithi eb17f5a
fix lint issues
prudhvigodithi bfb812d
fix lint issues
prudhvigodithi 740516e
fix lint issues
prudhvigodithi 3071a55
fix lint issues
prudhvigodithi c2b5121
Code cleanup
prudhvigodithi f44aa2a
Code cleanup
prudhvigodithi 303465f
Code cleanup
prudhvigodithi 8f4a8d3
Code cleanup typos
prudhvigodithi 7f87df9
Merge branch 'gradleplugin-2.2' of github.com:prudhvigodithi/OpenSear…
prudhvigodithi 9b1cb92
code cleanup and extended unit tests
prudhvigodithi c866641
code cleanup and extended unit tests
prudhvigodithi 9f32f4d
Naming convention fix
prudhvigodithi cdac61d
Naming convention fix
prudhvigodithi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
buildSrc/src/main/java/org/opensearch/gradle/pluginzip/Publish.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
| package org.opensearch.gradle.pluginzip; | ||
|
|
||
| import java.util.*; | ||
| import org.gradle.api.Plugin; | ||
| import org.gradle.api.Project; | ||
| 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; | ||
|
|
||
| public class Publish implements Plugin<Project> { | ||
| private Project project; | ||
|
|
||
| public final static String EXTENSION_NAME = "zipmavensettings"; | ||
| 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/"; | ||
|
|
||
| public static void configMaven(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); | ||
| }); | ||
| }); | ||
| publishing.publications(publications -> { | ||
| 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); | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| 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) { | ||
| this.project = project; | ||
| project.afterEvaluate(evaluatedProject -> { configMaven(project); }); | ||
| project.getGradle().getTaskGraph().whenReady(graph -> { | ||
| if (graph.hasTask(LOCALMAVEN)) { | ||
| project.getTasks().getByName(PLUGIN_ZIP_PUBLISH_POM_TASK).setEnabled(false); | ||
| } | ||
|
|
||
| }); | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
buildSrc/src/main/resources/META-INF/gradle-plugins/opensearch.pluginzip.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| implementation-class=org.opensearch.gradle.pluginzip.Publish |
104 changes: 104 additions & 0 deletions
104
buildSrc/src/test/java/org/opensearch/gradle/pluginzip/PublishTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.gradle.pluginzip; | ||
|
|
||
| import org.gradle.testkit.runner.BuildResult; | ||
| import org.gradle.testkit.runner.GradleRunner; | ||
| import org.gradle.testfixtures.ProjectBuilder; | ||
| import org.gradle.api.Project; | ||
| import org.opensearch.gradle.test.GradleUnitTestCase; | ||
| import org.junit.Test; | ||
| import java.io.IOException; | ||
| import org.gradle.api.publish.maven.tasks.PublishToMavenRepository; | ||
| import java.io.File; | ||
| import org.gradle.testkit.runner.BuildResult; | ||
| import java.io.FileWriter; | ||
| import java.io.Writer; | ||
| import static org.gradle.testkit.runner.TaskOutcome.SUCCESS; | ||
| import static org.junit.Assert.assertEquals; | ||
| import java.nio.file.Files; | ||
| import org.apache.maven.model.Model; | ||
| import org.apache.maven.model.io.xpp3.MavenXpp3Reader; | ||
| import org.codehaus.plexus.util.xml.pull.XmlPullParserException; | ||
| import java.io.FileReader; | ||
| import org.gradle.api.tasks.bundling.Zip; | ||
|
|
||
| public class PublishTests extends GradleUnitTestCase { | ||
|
|
||
| @Test | ||
| public void testZipPublish() throws IOException, XmlPullParserException { | ||
| Project project = ProjectBuilder.builder().build(); | ||
| String zipPublishTask = "publishPluginZipPublicationToZipStagingRepository"; | ||
| // Apply the opensearch.pluginzip plugin | ||
| project.getPluginManager().apply("opensearch.pluginzip"); | ||
| // Check if the plugin has been applied to the project | ||
| assertTrue(project.getPluginManager().hasPlugin("opensearch.pluginzip")); | ||
| // Check if the project has the task from class PublishToMavenRepository after plugin apply | ||
| assertNotNull(project.getTasks().withType(PublishToMavenRepository.class)); | ||
| // Create a mock bundlePlugin task | ||
| Zip task = project.getTasks().create("bundlePlugin", Zip.class); | ||
| Publish.configMaven(project); | ||
| // Check if the main task publishPluginZipPublicationToZipStagingRepository exists after plugin apply | ||
| assertTrue(project.getTasks().getNames().contains(zipPublishTask)); | ||
| assertNotNull("Task to generate: ", project.getTasks().getByName(zipPublishTask)); | ||
| // Run Gradle functional tests, but calling a build.gradle file, that resembles the plugin publish behavior | ||
| File projectDir = new File("build/functionalTest"); | ||
| // Create a sample plugin zip file | ||
| File sampleZip = new File("build/functionalTest/sample-plugin.zip"); | ||
| Files.createDirectories(projectDir.toPath()); | ||
| Files.createFile(sampleZip.toPath()); | ||
| writeString(new File(projectDir, "settings.gradle"), ""); | ||
| // Generate the build.gradle file | ||
| String buildFileContent = "apply plugin: 'maven-publish' \n" | ||
| + "publishing {\n" | ||
| + " repositories {\n" | ||
| + " maven {\n" | ||
| + " url = 'local-staging-repo/'\n" | ||
| + " name = 'zipStaging'\n" | ||
| + " }\n" | ||
| + " }\n" | ||
| + " publications {\n" | ||
| + " pluginZip(MavenPublication) {\n" | ||
| + " groupId = 'org.opensearch.plugin' \n" | ||
| + " artifactId = 'sample-plugin' \n" | ||
| + " version = '2.0.0.0' \n" | ||
| + " artifact('sample-plugin.zip') \n" | ||
| + " }\n" | ||
| + " }\n" | ||
| + "}"; | ||
| writeString(new File(projectDir, "build.gradle"), buildFileContent); | ||
| // Execute the task publishPluginZipPublicationToZipStagingRepository | ||
| GradleRunner runner = GradleRunner.create(); | ||
| runner.forwardOutput(); | ||
| runner.withPluginClasspath(); | ||
| runner.withArguments(zipPublishTask); | ||
| runner.withProjectDir(projectDir); | ||
| BuildResult result = runner.build(); | ||
| // Check if task publishMavenzipPublicationToZipstagingRepository has ran well | ||
| assertEquals(SUCCESS, result.task(":" + zipPublishTask).getOutcome()); | ||
| // check if the zip has been published to local staging repo | ||
| assertTrue( | ||
| new File("build/functionalTest/local-staging-repo/org/opensearch/plugin/sample-plugin/2.0.0.0/sample-plugin-2.0.0.0.zip") | ||
| .exists() | ||
| ); | ||
| // Parse the maven file and validate the groupID to org.opensearch.plugin | ||
| MavenXpp3Reader reader = new MavenXpp3Reader(); | ||
| Model model = reader.read( | ||
| new FileReader("build/functionalTest/local-staging-repo/org/opensearch/plugin/sample-plugin/2.0.0.0/sample-plugin-2.0.0.0.pom") | ||
| ); | ||
| assertEquals(model.getGroupId(), "org.opensearch.plugin"); | ||
| } | ||
|
|
||
| private void writeString(File file, String string) throws IOException { | ||
| try (Writer writer = new FileWriter(file)) { | ||
| writer.write(string); | ||
| } | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.