Skip to content

Commit 2451094

Browse files
committed
Move common repository configuration to java plugin (#57057)
This commit moves the common maven repository configuration to the ES java plugin. All java projects need this common set of repos. Note that the Elastic download and maven repos are removed, as they are not necessary anymore since distribution download was split into the DistributionDownloadPlugin.
1 parent 9358a61 commit 2451094

File tree

6 files changed

+83
-174
lines changed

6 files changed

+83
-174
lines changed

build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ allprojects {
5454
description = "Elasticsearch subproject ${project.path}"
5555
}
5656

57-
BuildPlugin.configureRepositories(project)
58-
5957
String licenseCommit
6058
if (VersionProperties.elasticsearch.toString().endsWith('-SNAPSHOT')) {
6159
licenseCommit = BuildParams.gitRevision ?: "master" // leniency for non git builds

buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy

Lines changed: 0 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.elasticsearch.gradle
2020

21-
2221
import groovy.transform.CompileStatic
2322
import org.apache.commons.io.IOUtils
2423
import org.elasticsearch.gradle.info.BuildParams
@@ -51,7 +50,6 @@ import org.gradle.api.execution.TaskActionListener
5150
import org.gradle.api.plugins.ExtraPropertiesExtension
5251
import org.gradle.api.plugins.JavaPlugin
5352
import org.gradle.api.tasks.testing.Test
54-
import org.gradle.authentication.http.HttpHeaderAuthentication
5553
import org.gradle.util.GradleVersion
5654

5755
import java.nio.charset.StandardCharsets
@@ -93,7 +91,6 @@ class BuildPlugin implements Plugin<Project> {
9391

9492
project.getTasks().register("buildResources", ExportElasticsearchBuildResourcesTask)
9593

96-
configureRepositories(project)
9794
project.extensions.getByType(ExtraPropertiesExtension).set('versions', VersionProperties.versions)
9895
PrecommitTasks.create(project, true)
9996
configureFips140(project)
@@ -159,122 +156,6 @@ class BuildPlugin implements Plugin<Project> {
159156
}
160157
}
161158

162-
/**
163-
* Makes dependencies non-transitive.
164-
*
165-
* Gradle allows setting all dependencies as non-transitive very easily.
166-
* Sadly this mechanism does not translate into maven pom generation. In order
167-
* to effectively make the pom act as if it has no transitive dependencies,
168-
* we must exclude each transitive dependency of each direct dependency.
169-
*
170-
* Determining the transitive deps of a dependency which has been resolved as
171-
* non-transitive is difficult because the process of resolving removes the
172-
* transitive deps. To sidestep this issue, we create a configuration per
173-
* direct dependency version. This specially named and unique configuration
174-
* will contain all of the transitive dependencies of this particular
175-
* dependency. We can then use this configuration during pom generation
176-
* to iterate the transitive dependencies and add excludes.
177-
*/
178-
static void configureConfigurations(Project project) {
179-
// we want to test compileOnly deps!
180-
project.configurations.getByName(JavaPlugin.TEST_COMPILE_CONFIGURATION_NAME).extendsFrom(project.configurations.getByName(JavaPlugin.COMPILE_ONLY_CONFIGURATION_NAME))
181-
182-
// we are not shipping these jars, we act like dumb consumers of these things
183-
if (project.path.startsWith(':test:fixtures') || project.path == ':build-tools') {
184-
return
185-
}
186-
// fail on any conflicting dependency versions
187-
project.configurations.all({ Configuration configuration ->
188-
if (configuration.name.endsWith('Fixture')) {
189-
// just a self contained test-fixture configuration, likely transitive and hellacious
190-
return
191-
}
192-
configuration.resolutionStrategy {
193-
failOnVersionConflict()
194-
}
195-
})
196-
197-
// force all dependencies added directly to compile/testCompile to be non-transitive, except for ES itself
198-
Closure disableTransitiveDeps = { Dependency dep ->
199-
if (dep instanceof ModuleDependency && !(dep instanceof ProjectDependency)
200-
&& dep.group.startsWith('org.elasticsearch') == false) {
201-
dep.transitive = false
202-
}
203-
}
204-
205-
project.configurations.getByName(JavaPlugin.COMPILE_CONFIGURATION_NAME).dependencies.all(disableTransitiveDeps)
206-
project.configurations.getByName(JavaPlugin.TEST_COMPILE_CONFIGURATION_NAME).dependencies.all(disableTransitiveDeps)
207-
project.configurations.getByName(JavaPlugin.COMPILE_ONLY_CONFIGURATION_NAME).dependencies.all(disableTransitiveDeps)
208-
project.configurations.getByName(JavaPlugin.RUNTIME_ONLY_CONFIGURATION_NAME).dependencies.all(disableTransitiveDeps)
209-
}
210-
211-
/** Adds repositories used by ES dependencies */
212-
static void configureRepositories(Project project) {
213-
project.getRepositories().all { repository ->
214-
if (repository instanceof MavenArtifactRepository) {
215-
final MavenArtifactRepository maven = (MavenArtifactRepository) repository
216-
assertRepositoryURIIsSecure(maven.name, project.path, maven.getUrl())
217-
repository.getArtifactUrls().each { uri -> assertRepositoryURIIsSecure(maven.name, project.path, uri) }
218-
} else if (repository instanceof IvyArtifactRepository) {
219-
final IvyArtifactRepository ivy = (IvyArtifactRepository) repository
220-
assertRepositoryURIIsSecure(ivy.name, project.path, ivy.getUrl())
221-
}
222-
}
223-
RepositoryHandler repos = project.repositories
224-
if (System.getProperty('repos.mavenLocal') != null) {
225-
// with -Drepos.mavenLocal=true we can force checking the local .m2 repo which is
226-
// useful for development ie. bwc tests where we install stuff in the local repository
227-
// such that we don't have to pass hardcoded files to gradle
228-
repos.mavenLocal()
229-
}
230-
repos.jcenter()
231-
repos.ivy { IvyArtifactRepository repo ->
232-
repo.name = 'elasticsearch'
233-
repo.url = 'https://artifacts.elastic.co/downloads'
234-
repo.patternLayout { IvyPatternRepositoryLayout layout ->
235-
layout.artifact 'elasticsearch/[module]-[revision](-[classifier]).[ext]'
236-
}
237-
// this header is not a credential but we hack the capability to send this header to avoid polluting our download stats
238-
repo.credentials(HttpHeaderCredentials, { HttpHeaderCredentials creds ->
239-
creds.name = 'X-Elastic-No-KPI'
240-
creds.value = '1'
241-
} as Action<HttpHeaderCredentials>)
242-
repo.authentication.create('header', HttpHeaderAuthentication)
243-
}
244-
repos.maven { MavenArtifactRepository repo ->
245-
repo.name = 'elastic'
246-
repo.url = 'https://artifacts.elastic.co/maven'
247-
}
248-
String luceneVersion = VersionProperties.lucene
249-
if (luceneVersion.contains('-snapshot')) {
250-
// extract the revision number from the version with a regex matcher
251-
List<String> matches = (luceneVersion =~ /\w+-snapshot-([a-z0-9]+)/).getAt(0) as List<String>
252-
String revision = matches.get(1)
253-
MavenArtifactRepository luceneRepo = repos.maven { MavenArtifactRepository repo ->
254-
repo.name = 'lucene-snapshots'
255-
repo.url = "https://s3.amazonaws.com/download.elasticsearch.org/lucenesnapshots/${revision}"
256-
}
257-
repos.exclusiveContent { ExclusiveContentRepository exclusiveRepo ->
258-
exclusiveRepo.filter {
259-
it.includeVersionByRegex(/org\.apache\.lucene/, '.*', ".*-snapshot-${revision}")
260-
}
261-
exclusiveRepo.forRepositories(luceneRepo)
262-
}
263-
}
264-
}
265-
266-
static void assertRepositoryURIIsSecure(final String repositoryName, final String projectPath, final URI uri) {
267-
if (uri != null && ["file", "https", "s3"].contains(uri.getScheme()) == false) {
268-
final String message = String.format(
269-
Locale.ROOT,
270-
"repository [%s] on project with path [%s] is not using a secure protocol for artifacts on [%s]",
271-
repositoryName,
272-
projectPath,
273-
uri.toURL())
274-
throw new GradleException(message)
275-
}
276-
}
277-
278159
private static class TestFailureReportingPlugin implements Plugin<Project> {
279160
@Override
280161
void apply(Project project) {

buildSrc/src/main/groovy/org/elasticsearch/gradle/test/StandaloneRestTestPlugin.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class StandaloneRestTestPlugin implements Plugin<Project> {
6262
project.pluginManager.apply(TestClustersPlugin)
6363

6464
project.getTasks().create("buildResources", ExportElasticsearchBuildResourcesTask)
65-
BuildPlugin.configureRepositories(project)
65+
ElasticsearchJavaPlugin.configureRepositories(project)
6666
ElasticsearchJavaPlugin.configureTestTasks(project)
6767
ElasticsearchJavaPlugin.configureInputNormalization(project)
6868
BuildPlugin.configureFips140(project)

buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
import org.gradle.api.artifacts.ModuleDependency;
3737
import org.gradle.api.artifacts.ProjectDependency;
3838
import org.gradle.api.artifacts.ResolutionStrategy;
39+
import org.gradle.api.artifacts.dsl.RepositoryHandler;
40+
import org.gradle.api.artifacts.repositories.IvyArtifactRepository;
41+
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
3942
import org.gradle.api.file.FileCollection;
4043
import org.gradle.api.plugins.BasePlugin;
4144
import org.gradle.api.plugins.ExtraPropertiesExtension;
@@ -56,12 +59,18 @@
5659
import java.io.File;
5760
import java.io.IOException;
5861
import java.io.UncheckedIOException;
62+
import java.net.MalformedURLException;
63+
import java.net.URI;
5964
import java.nio.file.Files;
6065
import java.nio.file.Path;
66+
import java.util.Arrays;
6167
import java.util.List;
68+
import java.util.Locale;
6269
import java.util.Map;
6370
import java.util.function.Consumer;
6471
import java.util.function.Function;
72+
import java.util.regex.Matcher;
73+
import java.util.regex.Pattern;
6574

6675
import static org.elasticsearch.gradle.util.GradleUtils.maybeConfigure;
6776
import static org.elasticsearch.gradle.util.Util.toStringable;
@@ -77,6 +86,7 @@ public void apply(Project project) {
7786

7887
project.getPluginManager().apply(JavaPlugin.class);
7988
configureConfigurations(project);
89+
configureRepositories(project);
8090
configureCompile(project);
8191
configureInputNormalization(project);
8292
configureTestTasks(project);
@@ -137,6 +147,75 @@ public static void configureConfigurations(Project project) {
137147
disableTransitiveDeps.accept(JavaPlugin.RUNTIME_ONLY_CONFIGURATION_NAME);
138148
}
139149

150+
private static final Pattern LUCENE_SNAPSHOT_REGEX = Pattern.compile("\\w+-snapshot-([a-z0-9]+)");
151+
152+
/** Adds repositories used by ES dependencies */
153+
public static void configureRepositories(Project project) {
154+
// ensure all repositories use secure urls
155+
// TODO: remove this with gradle 7.0, which no longer allows insecure urls
156+
project.getRepositories().all(repository -> {
157+
if (repository instanceof MavenArtifactRepository) {
158+
final MavenArtifactRepository maven = (MavenArtifactRepository) repository;
159+
assertRepositoryURIIsSecure(maven.getName(), project.getPath(), maven.getUrl());
160+
for (URI uri : maven.getArtifactUrls()) {
161+
assertRepositoryURIIsSecure(maven.getName(), project.getPath(), uri);
162+
}
163+
} else if (repository instanceof IvyArtifactRepository) {
164+
final IvyArtifactRepository ivy = (IvyArtifactRepository) repository;
165+
assertRepositoryURIIsSecure(ivy.getName(), project.getPath(), ivy.getUrl());
166+
}
167+
});
168+
RepositoryHandler repos = project.getRepositories();
169+
if (System.getProperty("repos.mavenLocal") != null) {
170+
// with -Drepos.mavenLocal=true we can force checking the local .m2 repo which is
171+
// useful for development ie. bwc tests where we install stuff in the local repository
172+
// such that we don't have to pass hardcoded files to gradle
173+
repos.mavenLocal();
174+
}
175+
repos.jcenter();
176+
177+
String luceneVersion = VersionProperties.getLucene();
178+
if (luceneVersion.contains("-snapshot")) {
179+
// extract the revision number from the version with a regex matcher
180+
Matcher matcher = LUCENE_SNAPSHOT_REGEX.matcher(luceneVersion);
181+
if (matcher.find() == false) {
182+
throw new GradleException("Malformed lucene snapshot version: " + luceneVersion);
183+
}
184+
String revision = matcher.group(1);
185+
MavenArtifactRepository luceneRepo = repos.maven(repo -> {
186+
repo.setName("lucene-snapshots");
187+
repo.setUrl("https://s3.amazonaws.com/download.elasticsearch.org/lucenesnapshots/" + revision);
188+
});
189+
repos.exclusiveContent(exclusiveRepo -> {
190+
exclusiveRepo.filter(
191+
descriptor -> descriptor.includeVersionByRegex("org\\.apache\\.lucene", ".*", ".*-snapshot-" + revision)
192+
);
193+
exclusiveRepo.forRepositories(luceneRepo);
194+
});
195+
}
196+
}
197+
198+
private static final List<String> SECURE_URL_SCHEMES = Arrays.asList("file", "https", "s3");
199+
200+
private static void assertRepositoryURIIsSecure(final String repositoryName, final String projectPath, final URI uri) {
201+
if (uri != null && SECURE_URL_SCHEMES.contains(uri.getScheme()) == false) {
202+
String url;
203+
try {
204+
url = uri.toURL().toString();
205+
} catch (MalformedURLException e) {
206+
throw new IllegalStateException(e);
207+
}
208+
final String message = String.format(
209+
Locale.ROOT,
210+
"repository [%s] on project with path [%s] is not using a secure protocol for artifacts on [%s]",
211+
repositoryName,
212+
projectPath,
213+
url
214+
);
215+
throw new GradleException(message);
216+
}
217+
}
218+
140219
/** Adds compiler settings to the project */
141220
public static void configureCompile(Project project) {
142221
project.getExtensions().getExtraProperties().set("compactProfile", "full");

buildSrc/src/test/java/org/elasticsearch/gradle/BuildPluginTests.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

buildSrc/src/test/java/org/elasticsearch/gradle/test/GradleIntegrationTestCase.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.util.stream.Collectors;
1919
import java.util.stream.Stream;
2020

21+
import static org.hamcrest.CoreMatchers.containsString;
22+
2123
public abstract class GradleIntegrationTestCase extends GradleUnitTestCase {
2224

2325
@Rule
@@ -71,7 +73,7 @@ protected void assertOutputContains(String output, Set<String> lines) {
7173
}
7274

7375
protected void assertOutputContains(String output, String line) {
74-
assertTrue("Expected the following line in output:\n\n" + line + "\n\nOutput is:\n" + output, output.contains(line));
76+
assertThat("Expected the following line in output:\n\n" + line + "\n\nOutput is:\n" + output, output, containsString(line));
7577
}
7678

7779
protected void assertOutputDoesNotContain(String output, String line) {

0 commit comments

Comments
 (0)