Skip to content

Commit 69aabb7

Browse files
authored
Build: Fail if any libs depend on non-core libs (#29336)
Fails the build if any subprojects of `:libs` have dependencies in `:libs` except for `:libs:elasticsearch-core`. Since we now have three places where we resolve project substitutions I've added `dependencyToProject` to `project.ext` in all projects. It resolves both `project` style dependencies and "external" style (like "org.elasticsearch:elasticsearch-core:${version}") dependencies to `Project`s using the `projectSubstitutions`. I use this new function all three places where resovle project substitutions. Finally this pulls `apply plugin: 'elasticsearch.build'` out of `libs/*/build.gradle` and into a subprojects clause in `libs/build.gradle`. I do this entirely so that I can call `tasks.precommit.dependsOn checkDependencies` without waiting for the subprojects to be evaluated or worrying about whether or not they have `precommit` set up in a normal way.
1 parent 62e33ee commit 69aabb7

File tree

7 files changed

+69
-23
lines changed

7 files changed

+69
-23
lines changed

build.gradle

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,23 @@ subprojects {
231231
}
232232
}
233233

234+
/*
235+
* Gradle only resolve project substitutions during dependency resolution but
236+
* we sometimes want to do the resolution at other times. This creates a
237+
* convenient method we can call to do it.
238+
*/
239+
ext.dependencyToProject = { Dependency dep ->
240+
if (dep instanceof ProjectDependency) {
241+
return dep.dependencyProject
242+
} else {
243+
String substitution = projectSubstitutions.get("${dep.group}:${dep.name}:${dep.version}")
244+
if (substitution != null) {
245+
return findProject(substitution)
246+
}
247+
return null
248+
}
249+
}
250+
234251
project.afterEvaluate {
235252
configurations.all {
236253
resolutionStrategy.dependencySubstitution { DependencySubstitutions subs ->
@@ -249,11 +266,11 @@ subprojects {
249266
Closure sortClosure = { a, b -> b.group <=> a.group }
250267
Closure depJavadocClosure = { dep ->
251268
if (dep.group != null && dep.group.startsWith('org.elasticsearch')) {
252-
String substitution = project.ext.projectSubstitutions.get("${dep.group}:${dep.name}:${dep.version}")
253-
if (substitution != null) {
254-
project.javadoc.dependsOn substitution + ':javadoc'
269+
Project upstreamProject = dependencyToProject(dep)
270+
if (upstreamProject != null) {
271+
project.javadoc.dependsOn "${upstreamProject.path}:javadoc"
255272
String artifactPath = dep.group.replaceAll('\\.', '/') + '/' + dep.name.replaceAll('\\.', '/') + '/' + dep.version
256-
project.javadoc.options.linksOffline artifactsHost + "/javadoc/" + artifactPath, "${project.project(substitution).buildDir}/docs/javadoc/"
273+
project.javadoc.options.linksOffline artifactsHost + "/javadoc/" + artifactPath, "${upstreamProject.buildDir}/docs/javadoc/"
257274
}
258275
}
259276
}
@@ -275,17 +292,7 @@ gradle.projectsEvaluated {
275292
}
276293
configurations.all {
277294
dependencies.all { Dependency dep ->
278-
Project upstreamProject = null
279-
if (dep instanceof ProjectDependency) {
280-
upstreamProject = dep.dependencyProject
281-
} else {
282-
// gradle doesn't apply substitutions until resolve time, so they won't
283-
// show up as a ProjectDependency above
284-
String substitution = projectSubstitutions.get("${dep.group}:${dep.name}:${dep.version}")
285-
if (substitution != null) {
286-
upstreamProject = findProject(substitution)
287-
}
288-
}
295+
Project upstreamProject = dependencyToProject(dep)
289296
if (upstreamProject != null) {
290297
if (project.path == upstreamProject.path) {
291298
// TODO: distribution integ tests depend on themselves (!), fix that

libs/build.gradle

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
subprojects {
21+
/*
22+
* All subprojects are java projects using Elasticsearch's standard build
23+
* tools.
24+
*/
25+
apply plugin: 'elasticsearch.build'
26+
27+
/*
28+
* Subprojects may depend on the "core" lib but may not depend on any
29+
* other libs. This keeps are dependencies simpler.
30+
*/
31+
project.afterEvaluate {
32+
configurations.all { Configuration conf ->
33+
dependencies.all { Dependency dep ->
34+
Project depProject = dependencyToProject(dep)
35+
if (depProject != null
36+
&& false == depProject.path.equals(':libs:elasticsearch-core')
37+
&& depProject.path.startsWith(':libs')) {
38+
throw new InvalidUserDataException("projects in :libs "
39+
+ "may not depend on other projects libs except "
40+
+ ":libs:elasticsearch-core but "
41+
+ "${project.path} depends on ${depProject.path}")
42+
}
43+
}
44+
}
45+
}
46+
}

libs/elasticsearch-core/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import org.elasticsearch.gradle.precommit.PrecommitTasks
1919
* under the License.
2020
*/
2121

22-
apply plugin: 'elasticsearch.build'
2322
apply plugin: 'nebula.optional-base'
2423
apply plugin: 'nebula.maven-base-publish'
2524
apply plugin: 'nebula.maven-scm'

libs/elasticsearch-nio/build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import org.elasticsearch.gradle.precommit.PrecommitTasks
2121

22-
apply plugin: 'elasticsearch.build'
2322
apply plugin: 'nebula.maven-base-publish'
2423
apply plugin: 'nebula.maven-scm'
2524

@@ -39,7 +38,7 @@ dependencies {
3938
testCompile "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}"
4039
testCompile "junit:junit:${versions.junit}"
4140
testCompile "org.hamcrest:hamcrest-all:${versions.hamcrest}"
42-
41+
4342
if (isEclipse == false || project.path == ":libs:elasticsearch-nio-tests") {
4443
testCompile("org.elasticsearch.test:framework:${version}") {
4544
exclude group: 'org.elasticsearch', module: 'elasticsearch-nio'

libs/grok/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import org.elasticsearch.gradle.precommit.PrecommitTasks
1919
* under the License.
2020
*/
2121

22-
apply plugin: 'elasticsearch.build'
23-
2422
archivesBaseName = 'elasticsearch-grok'
2523

2624
dependencies {

libs/plugin-classloader/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
* under the License.
1818
*/
1919

20-
apply plugin: 'elasticsearch.build'
21-
2220
test.enabled = false
2321

2422
// test depend on ES core...

libs/secure-sm/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import org.elasticsearch.gradle.precommit.PrecommitTasks
2121

22-
apply plugin: 'elasticsearch.build'
2322
apply plugin: 'nebula.maven-base-publish'
2423
apply plugin: 'nebula.maven-scm'
2524

0 commit comments

Comments
 (0)