1
0
Эх сурвалжийг харах

Support project dependencies from included builds in java modules (#89781)

Mark Vieira 3 жил өмнө
parent
commit
a82a93ce9c

+ 85 - 7
build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/ElasticsearchJavaModulePathPluginFuncTest.groovy

@@ -12,6 +12,8 @@ import org.elasticsearch.gradle.VersionProperties
 import org.elasticsearch.gradle.fixtures.AbstractJavaGradleFuncTest
 import org.gradle.internal.os.OperatingSystem
 import org.gradle.testkit.runner.TaskOutcome
+import org.junit.Rule
+import org.junit.rules.TemporaryFolder
 import org.objectweb.asm.ClassReader
 import org.objectweb.asm.tree.ClassNode
 
@@ -23,6 +25,19 @@ class ElasticsearchJavaModulePathPluginFuncTest extends AbstractJavaGradleFuncTe
 
     public static final String ES_VERSION = VersionProperties.getElasticsearch()
 
+    public static final String COMPILE_JAVA_CONFIG = """
+        tasks.named('compileJava').configure {
+            doLast {
+                def sep = org.elasticsearch.gradle.OS.current() == org.elasticsearch.gradle.OS.WINDOWS ? ':' : ';'
+                println "COMPILE_JAVA_COMPILER_ARGS " + options.allCompilerArgs.join(sep)
+                println "COMPILE_JAVA_CLASSPATH "  + classpath.asPath
+            }
+        }
+    """
+
+    @Rule
+    TemporaryFolder rootBuild = new TemporaryFolder()
+
     def setup() {
         clazz("org.acme.JavaMainClass")
         subProject("some-lib") << """
@@ -49,19 +64,14 @@ class ElasticsearchJavaModulePathPluginFuncTest extends AbstractJavaGradleFuncTe
 
             allprojects {
                 version = '1.2.3'
+                group = 'test'
             }
 
             dependencies {
                 implementation project('some-lib')
             }
 
-            tasks.named('compileJava').configure {
-                doLast {
-                    def sep = org.gradle.internal.os.OperatingSystem.current().isWindows() ? ':' : ';'
-                    println "COMPILE_JAVA_COMPILER_ARGS " + options.allCompilerArgs.join(sep)
-                    println "COMPILE_JAVA_CLASSPATH "  + classpath.asPath
-                }
-            }
+            $COMPILE_JAVA_CONFIG
         """
     }
 
@@ -159,6 +169,74 @@ class ElasticsearchJavaModulePathPluginFuncTest extends AbstractJavaGradleFuncTe
         assertModuleInfo(file('build/classes/java/main/module-info.class'), 'rootModule', [JAVA_BASE_MODULE])
     }
 
+    def "included build with non module dependencies"() {
+        given:
+        file(rootBuild.root, 'settings.gradle') << """
+        includeBuild '$projectDir'
+        """
+
+        file(rootBuild.root, 'build.gradle') << """
+            plugins {
+                id 'java'
+                id 'elasticsearch.java-module'
+            }
+
+            dependencies {
+                implementation 'test:some-lib:1.2.3'
+            }
+
+            $COMPILE_JAVA_CONFIG
+        """
+
+        and:
+        writeClazz('org.parent.Main', null, file(rootBuild.root, "src/main/java/org/parent/Main.java"), null)
+
+        when:
+        def result = gradleRunner(rootBuild.root, "compileJava").build()
+
+        then:
+        result.task(":compileJava").outcome == TaskOutcome.SUCCESS
+
+        assertModulePathClasspath([], normalized(result.output))
+        assertCompileClasspath(['./some-lib/build/classes/java/main', './some-other-lib/build/classes/java/main'], normalized(result.output))
+    }
+
+    def "included build with module dependencies"() {
+        given:
+        file(rootBuild.root, 'settings.gradle') << """
+        includeBuild '$projectDir'
+        """
+
+        file(rootBuild.root, 'build.gradle') << """
+            plugins {
+                id 'java'
+                id 'elasticsearch.java-module'
+            }
+
+            dependencies {
+                implementation 'test:some-lib:1.2.3'
+            }
+
+            $COMPILE_JAVA_CONFIG
+        """
+
+        and:
+        file('some-lib/src/main/java/module-info.java') << """
+        module someLibModule {
+        }
+        """
+        writeClazz('org.parent.Main', null, file(rootBuild.root, "src/main/java/org/parent/Main.java"), null)
+
+        when:
+        def result = gradleRunner(rootBuild.root, "compileJava").build()
+
+        then:
+        result.task(":compileJava").outcome == TaskOutcome.SUCCESS
+
+        assertModulePathClasspath(['./some-lib/build/classes/java/main', './some-other-lib/build/classes/java/main'], normalized(result.output))
+        assertCompileClasspath([], normalized(result.output))
+    }
+
     private def assertModulePathClasspath(List<String> expectedEntries, String output) {
         def allArgs = output.find(/(?<=COMPILE_JAVA_COMPILER_ARGS ).*\n/).trim()
         if(allArgs.isEmpty()) {

+ 18 - 1
build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/ElasticsearchJavaModulePathPlugin.java

@@ -186,7 +186,7 @@ public class ElasticsearchJavaModulePathPlugin implements Plugin<Project> {
     }
 
     static boolean hasModuleInfoDotJava(Project project, ProjectComponentIdentifier id) {
-        return project.findProject(id.getProjectPath()).file("src/main/java/module-info.java").exists();
+        return new File(findProjectIdPath(project, id), "src/main/java/module-info.java").exists();
     }
 
     static SourceSet getJavaMainSourceSet(Project project) {
@@ -209,4 +209,21 @@ public class ElasticsearchJavaModulePathPlugin implements Plugin<Project> {
     static boolean isIdea() {
         return System.getProperty("idea.sync.active", "false").equals("true");
     }
+
+    static File findProjectIdPath(Project project, ProjectComponentIdentifier id) {
+        if (id.getBuild().isCurrentBuild()) {
+            return project.findProject(id.getProjectPath()).getProjectDir();
+        } else {
+            // For project dependencies sourced from an included build we have to infer the source project path
+            File includedBuildDir = project.getGradle().includedBuild(id.getBuild().getName()).getProjectDir();
+
+            // We have to account for us renaming the :libs projects here
+            String[] pathSegments = id.getProjectPath().split(":");
+            if (pathSegments[1].equals("libs")) {
+                pathSegments[2] = pathSegments[2].replaceFirst("elasticsearch-", "");
+            }
+
+            return new File(includedBuildDir, String.join(File.separator, List.of(pathSegments)));
+        }
+    }
 }