Skip to content

Commit

Permalink
Build: Fail if any libs depend on non-core libs (#29336)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nik9000 authored Apr 16, 2018
1 parent 62e33ee commit 69aabb7
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 23 deletions.
37 changes: 22 additions & 15 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,23 @@ subprojects {
}
}

/*
* Gradle only resolve project substitutions during dependency resolution but
* we sometimes want to do the resolution at other times. This creates a
* convenient method we can call to do it.
*/
ext.dependencyToProject = { Dependency dep ->
if (dep instanceof ProjectDependency) {
return dep.dependencyProject
} else {
String substitution = projectSubstitutions.get("${dep.group}:${dep.name}:${dep.version}")
if (substitution != null) {
return findProject(substitution)
}
return null
}
}

project.afterEvaluate {
configurations.all {
resolutionStrategy.dependencySubstitution { DependencySubstitutions subs ->
Expand All @@ -249,11 +266,11 @@ subprojects {
Closure sortClosure = { a, b -> b.group <=> a.group }
Closure depJavadocClosure = { dep ->
if (dep.group != null && dep.group.startsWith('org.elasticsearch')) {
String substitution = project.ext.projectSubstitutions.get("${dep.group}:${dep.name}:${dep.version}")
if (substitution != null) {
project.javadoc.dependsOn substitution + ':javadoc'
Project upstreamProject = dependencyToProject(dep)
if (upstreamProject != null) {
project.javadoc.dependsOn "${upstreamProject.path}:javadoc"
String artifactPath = dep.group.replaceAll('\\.', '/') + '/' + dep.name.replaceAll('\\.', '/') + '/' + dep.version
project.javadoc.options.linksOffline artifactsHost + "/javadoc/" + artifactPath, "${project.project(substitution).buildDir}/docs/javadoc/"
project.javadoc.options.linksOffline artifactsHost + "/javadoc/" + artifactPath, "${upstreamProject.buildDir}/docs/javadoc/"
}
}
}
Expand All @@ -275,17 +292,7 @@ gradle.projectsEvaluated {
}
configurations.all {
dependencies.all { Dependency dep ->
Project upstreamProject = null
if (dep instanceof ProjectDependency) {
upstreamProject = dep.dependencyProject
} else {
// gradle doesn't apply substitutions until resolve time, so they won't
// show up as a ProjectDependency above
String substitution = projectSubstitutions.get("${dep.group}:${dep.name}:${dep.version}")
if (substitution != null) {
upstreamProject = findProject(substitution)
}
}
Project upstreamProject = dependencyToProject(dep)
if (upstreamProject != null) {
if (project.path == upstreamProject.path) {
// TODO: distribution integ tests depend on themselves (!), fix that
Expand Down
46 changes: 46 additions & 0 deletions libs/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you 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.
*/

subprojects {
/*
* All subprojects are java projects using Elasticsearch's standard build
* tools.
*/
apply plugin: 'elasticsearch.build'

/*
* Subprojects may depend on the "core" lib but may not depend on any
* other libs. This keeps are dependencies simpler.
*/
project.afterEvaluate {
configurations.all { Configuration conf ->
dependencies.all { Dependency dep ->
Project depProject = dependencyToProject(dep)
if (depProject != null
&& false == depProject.path.equals(':libs:elasticsearch-core')
&& depProject.path.startsWith(':libs')) {
throw new InvalidUserDataException("projects in :libs "
+ "may not depend on other projects libs except "
+ ":libs:elasticsearch-core but "
+ "${project.path} depends on ${depProject.path}")
}
}
}
}
}
1 change: 0 additions & 1 deletion libs/elasticsearch-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import org.elasticsearch.gradle.precommit.PrecommitTasks
* under the License.
*/

apply plugin: 'elasticsearch.build'
apply plugin: 'nebula.optional-base'
apply plugin: 'nebula.maven-base-publish'
apply plugin: 'nebula.maven-scm'
Expand Down
3 changes: 1 addition & 2 deletions libs/elasticsearch-nio/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.elasticsearch.gradle.precommit.PrecommitTasks

apply plugin: 'elasticsearch.build'
apply plugin: 'nebula.maven-base-publish'
apply plugin: 'nebula.maven-scm'

Expand All @@ -39,7 +38,7 @@ dependencies {
testCompile "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}"
testCompile "junit:junit:${versions.junit}"
testCompile "org.hamcrest:hamcrest-all:${versions.hamcrest}"

if (isEclipse == false || project.path == ":libs:elasticsearch-nio-tests") {
testCompile("org.elasticsearch.test:framework:${version}") {
exclude group: 'org.elasticsearch', module: 'elasticsearch-nio'
Expand Down
2 changes: 0 additions & 2 deletions libs/grok/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import org.elasticsearch.gradle.precommit.PrecommitTasks
* under the License.
*/

apply plugin: 'elasticsearch.build'

archivesBaseName = 'elasticsearch-grok'

dependencies {
Expand Down
2 changes: 0 additions & 2 deletions libs/plugin-classloader/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* under the License.
*/

apply plugin: 'elasticsearch.build'

test.enabled = false

// test depend on ES core...
Expand Down
1 change: 0 additions & 1 deletion libs/secure-sm/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.elasticsearch.gradle.precommit.PrecommitTasks

apply plugin: 'elasticsearch.build'
apply plugin: 'nebula.maven-base-publish'
apply plugin: 'nebula.maven-scm'

Expand Down

0 comments on commit 69aabb7

Please sign in to comment.