Browse Source

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.
Nik Everett 7 years ago
parent
commit
69aabb7e40

+ 22 - 15
build.gradle

@@ -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 ->
@@ -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/"
           }
         }
       }
@@ -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

+ 46 - 0
libs/build.gradle

@@ -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}")
+                }
+            }
+        }
+    }
+}

+ 0 - 1
libs/elasticsearch-core/build.gradle

@@ -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'

+ 1 - 2
libs/elasticsearch-nio/build.gradle

@@ -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'
 
@@ -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'

+ 0 - 2
libs/grok/build.gradle

@@ -19,8 +19,6 @@ import org.elasticsearch.gradle.precommit.PrecommitTasks
  * under the License.
  */
 
-apply plugin: 'elasticsearch.build'
-
 archivesBaseName = 'elasticsearch-grok'
 
 dependencies {

+ 0 - 2
libs/plugin-classloader/build.gradle

@@ -17,8 +17,6 @@
  * under the License.
  */
 
-apply plugin: 'elasticsearch.build'
-
 test.enabled = false
 
 // test depend on ES core...

+ 0 - 1
libs/secure-sm/build.gradle

@@ -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'