Skip to content

Commit ca9d1b4

Browse files
committed
Build: Fail if any libs depend on non-core libs (elastic#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 d0706dc commit ca9d1b4

File tree

6 files changed

+68
-21
lines changed

6 files changed

+68
-21
lines changed

build.gradle

+22-15
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,23 @@ subprojects {
235235
}
236236
}
237237

238+
/*
239+
* Gradle only resolve project substitutions during dependency resolution but
240+
* we sometimes want to do the resolution at other times. This creates a
241+
* convenient method we can call to do it.
242+
*/
243+
ext.dependencyToProject = { Dependency dep ->
244+
if (dep instanceof ProjectDependency) {
245+
return dep.dependencyProject
246+
} else {
247+
String substitution = projectSubstitutions.get("${dep.group}:${dep.name}:${dep.version}")
248+
if (substitution != null) {
249+
return findProject(substitution)
250+
}
251+
return null
252+
}
253+
}
254+
238255
project.afterEvaluate {
239256
configurations.all {
240257
resolutionStrategy.dependencySubstitution { DependencySubstitutions subs ->
@@ -253,11 +270,11 @@ subprojects {
253270
Closure sortClosure = { a, b -> b.group <=> a.group }
254271
Closure depJavadocClosure = { dep ->
255272
if (dep.group != null && dep.group.startsWith('org.elasticsearch')) {
256-
String substitution = project.ext.projectSubstitutions.get("${dep.group}:${dep.name}:${dep.version}")
257-
if (substitution != null) {
258-
project.javadoc.dependsOn substitution + ':javadoc'
273+
Project upstreamProject = dependencyToProject(dep)
274+
if (upstreamProject != null) {
275+
project.javadoc.dependsOn "${upstreamProject.path}:javadoc"
259276
String artifactPath = dep.group.replaceAll('\\.', '/') + '/' + dep.name.replaceAll('\\.', '/') + '/' + dep.version
260-
project.javadoc.options.linksOffline artifactsHost + "/javadoc/" + artifactPath, "${project.project(substitution).buildDir}/docs/javadoc/"
277+
project.javadoc.options.linksOffline artifactsHost + "/javadoc/" + artifactPath, "${upstreamProject.buildDir}/docs/javadoc/"
261278
}
262279
}
263280
}
@@ -279,17 +296,7 @@ gradle.projectsEvaluated {
279296
}
280297
configurations.all {
281298
dependencies.all { Dependency dep ->
282-
Project upstreamProject = null
283-
if (dep instanceof ProjectDependency) {
284-
upstreamProject = dep.dependencyProject
285-
} else {
286-
// gradle doesn't apply substitutions until resolve time, so they won't
287-
// show up as a ProjectDependency above
288-
String substitution = projectSubstitutions.get("${dep.group}:${dep.name}:${dep.version}")
289-
if (substitution != null) {
290-
upstreamProject = findProject(substitution)
291-
}
292-
}
299+
Project upstreamProject = dependencyToProject(dep)
293300
if (upstreamProject != null) {
294301
if (project.path == upstreamProject.path) {
295302
// TODO: distribution integ tests depend on themselves (!), fix that

libs/build.gradle

+46
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

-1
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/grok/build.gradle

-2
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

-2
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

-1
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)