-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'opensearch-project:main' into main
- Loading branch information
Showing
75 changed files
with
2,921 additions
and
63 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
124 changes: 124 additions & 0 deletions
124
buildSrc/src/main/groovy/org/opensearch/gradle/plugin/OptionalDependenciesPlugin.groovy
This file contains 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,124 @@ | ||
/* | ||
* 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 | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
/* | ||
* Copyright 2014-2016 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.opensearch.gradle.plugin | ||
|
||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.api.publish.PublishingExtension | ||
import org.gradle.api.publish.ivy.IvyPublication | ||
import org.gradle.api.publish.maven.MavenPublication | ||
import org.gradle.api.publish.plugins.PublishingPlugin | ||
|
||
/** | ||
* Including the support of `optional` dependencies: https://github.com/nebula-plugins/gradle-extra-configurations-plugin/blob/v9.0.0/src/main/groovy/nebula/plugin/extraconfigurations/OptionalBasePlugin.groovy | ||
*/ | ||
class OptionalDependenciesPlugin implements Plugin<Project> { | ||
static final String OPTIONAL_IDENTIFIER = 'optional' | ||
|
||
@Override | ||
void apply(Project project) { | ||
enhanceProjectModel(project) | ||
configureMavenPublishPlugin(project) | ||
configureIvyPublishPlugin(project) | ||
} | ||
|
||
/** | ||
* Enhances the Project domain object by adding | ||
* | ||
* a) a extra property List that holds optional dependencies | ||
* b) a extra method that can be executed as parameter when declaring dependencies | ||
* | ||
* @param project Project | ||
*/ | ||
private void enhanceProjectModel(Project project) { | ||
project.ext.optionalDeps = [] | ||
|
||
project.ext.optional = { dep -> | ||
project.ext.optionalDeps << dep | ||
} | ||
} | ||
|
||
/** | ||
* Configures Maven Publishing plugin to ensure that published dependencies receive the optional element. | ||
* | ||
* @param project Project | ||
*/ | ||
private void configureMavenPublishPlugin(Project project) { | ||
project.plugins.withType(PublishingPlugin) { | ||
project.publishing { | ||
publications { | ||
project.extensions.findByType(PublishingExtension)?.publications?.withType(MavenPublication) { MavenPublication pub -> | ||
pub.pom.withXml { | ||
project.ext.optionalDeps.each { dep -> | ||
def foundDep = asNode().dependencies.dependency.find { | ||
it.groupId.text() == dep.group && it.artifactId.text() == dep.name | ||
} | ||
|
||
if (foundDep) { | ||
if (foundDep.optional) { | ||
foundDep.optional.value = 'true' | ||
} else { | ||
foundDep.appendNode(OPTIONAL_IDENTIFIER, 'true') | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Configures Ivy Publishing plugin to ensure that published dependencies receive the correct conf attribute value. | ||
* | ||
* @param project Project | ||
*/ | ||
private void configureIvyPublishPlugin(Project project) { | ||
project.plugins.withType(PublishingPlugin) { | ||
project.publishing { | ||
publications { | ||
project.extensions.findByType(PublishingExtension)?.publications?.withType(IvyPublication) { IvyPublication pub -> | ||
pub.descriptor.withXml { | ||
def rootNode = asNode() | ||
|
||
// Add optional configuration if it doesn't exist yet | ||
if (!rootNode.configurations.find { it.@name == OPTIONAL_IDENTIFIER }) { | ||
rootNode.configurations[0].appendNode('conf', [name: OPTIONAL_IDENTIFIER, visibility: 'public']) | ||
} | ||
|
||
// Replace dependency "runtime->default" conf attribute value with "optional" | ||
project.ext.optionalDeps.each { dep -> | ||
def foundDep = rootNode.dependencies.dependency.find { it.@name == dep.name } | ||
foundDep?.@conf = OPTIONAL_IDENTIFIER | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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
12 changes: 12 additions & 0 deletions
12
...rc/src/main/resources/META-INF/gradle-plugins/opensearch.optional-dependencies.properties
This file contains 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,12 @@ | ||
# | ||
# 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. | ||
# | ||
# Modifications Copyright OpenSearch Contributors. See | ||
# GitHub history for details. | ||
# | ||
|
||
implementation-class=org.opensearch.gradle.plugin.OptionalDependenciesPlugin |
85 changes: 85 additions & 0 deletions
85
buildSrc/src/test/java/org/opensearch/gradle/plugin/OptionalDependenciesPluginTests.java
This file contains 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,85 @@ | ||
/* | ||
* 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.plugin; | ||
|
||
import org.apache.maven.model.Dependency; | ||
import org.apache.maven.model.Model; | ||
import org.apache.maven.model.io.xpp3.MavenXpp3Reader; | ||
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; | ||
import org.gradle.testkit.runner.BuildResult; | ||
import org.gradle.testkit.runner.GradleRunner; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.rules.TemporaryFolder; | ||
import org.opensearch.gradle.test.GradleUnitTestCase; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.util.Optional; | ||
|
||
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS; | ||
import static org.hamcrest.CoreMatchers.is; | ||
|
||
public class OptionalDependenciesPluginTests extends GradleUnitTestCase { | ||
private TemporaryFolder projectDir; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
projectDir = new TemporaryFolder(); | ||
projectDir.create(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
projectDir.delete(); | ||
} | ||
|
||
public void testApply() throws FileNotFoundException, IOException, XmlPullParserException { | ||
final File mavenRepoDir = new File(projectDir.getRoot(), "mavenrepo"); | ||
|
||
try (InputStream in = getClass().getClassLoader().getResourceAsStream("plugin/optional-dependencies.gradle")) { | ||
try (OutputStream out = new FileOutputStream(projectDir.newFile("build.gradle"))) { | ||
in.transferTo(out); | ||
} | ||
} | ||
|
||
GradleRunner runner = GradleRunner.create() | ||
.forwardOutput() | ||
.withPluginClasspath() | ||
.withArguments("publish", "-PrepoUrl=" + mavenRepoDir.toURI().toURL()) | ||
.withProjectDir(projectDir.getRoot()); | ||
|
||
BuildResult result = runner.build(); | ||
assertEquals(SUCCESS, result.task(":" + "publish").getOutcome()); | ||
|
||
final String name = projectDir.getRoot().getName(); | ||
assertDependency(mavenRepoDir, name); | ||
} | ||
|
||
private void assertDependency(File repoUrl, String name) throws FileNotFoundException, IOException, XmlPullParserException { | ||
final File pom = new File(repoUrl, "org/custom/group/" + name + "/1.0.0/" + name + "-1.0.0.pom"); | ||
assertThat(pom.exists(), is(true)); | ||
|
||
final MavenXpp3Reader reader = new MavenXpp3Reader(); | ||
final Model model = reader.read(new FileReader(pom)); | ||
|
||
final Optional<Dependency> dependecyOpt = model.getDependencies() | ||
.stream() | ||
.filter(d -> d.getArtifactId().equals("commons-lang3")) | ||
.findAny(); | ||
|
||
assertThat(dependecyOpt.isPresent(), is(true)); | ||
assertThat(dependecyOpt.get().isOptional(), is(true)); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
buildSrc/src/test/resources/plugin/optional-dependencies.gradle
This file contains 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,39 @@ | ||
/* | ||
* 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. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
plugins { | ||
id 'java' | ||
id 'maven-publish' | ||
id 'opensearch.optional-dependencies' | ||
} | ||
group = "org.custom.group" | ||
version = '1.0.0' | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation 'org.apache.commons:commons-lang3:3.3.2', optional | ||
} | ||
|
||
publishing { | ||
publications { | ||
mavenJava(MavenPublication) { | ||
from components.java | ||
} | ||
} | ||
repositories { | ||
maven { | ||
url "${repoUrl}" | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -62,3 +62,5 @@ jmh = 1.35 | |
|
||
# compression | ||
zstd = 1.5.5-3 | ||
|
||
jzlib = 1.1.3 |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.