Skip to content

Commit a286419

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 8106082 commit a286419

File tree

6 files changed

+83
-175
lines changed

6 files changed

+83
-175
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 & 120 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
@@ -47,7 +46,6 @@ import org.gradle.api.plugins.ExtraPropertiesExtension
4746
import org.gradle.api.plugins.JavaPlugin
4847
import org.gradle.api.tasks.bundling.Jar
4948
import org.gradle.api.tasks.testing.Test
50-
import org.gradle.authentication.http.HttpHeaderAuthentication
5149
import org.gradle.util.GradleVersion
5250

5351
import java.nio.charset.StandardCharsets
@@ -90,7 +88,6 @@ class BuildPlugin implements Plugin<Project> {
9088

9189
project.getTasks().register("buildResources", ExportElasticsearchBuildResourcesTask)
9290

93-
configureRepositories(project)
9491
project.extensions.getByType(ExtraPropertiesExtension).set('versions', VersionProperties.versions)
9592
PrecommitTasks.create(project, true)
9693
configureFips140(project)
@@ -154,123 +151,6 @@ class BuildPlugin implements Plugin<Project> {
154151
}
155152
}
156153

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

6574
import static org.elasticsearch.gradle.util.GradleUtils.maybeConfigure;
6675
import static org.elasticsearch.gradle.util.Util.toStringable;
@@ -76,6 +85,7 @@ public void apply(Project project) {
7685

7786
project.getPluginManager().apply(JavaPlugin.class);
7887
configureConfigurations(project);
88+
configureRepositories(project);
7989
configureCompile(project);
8090
configureInputNormalization(project);
8191
configureTestTasks(project);
@@ -136,6 +146,75 @@ public static void configureConfigurations(Project project) {
136146
disableTransitiveDeps.accept(JavaPlugin.RUNTIME_ONLY_CONFIGURATION_NAME);
137147
}
138148

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