Gradle - Groovy 和 Java 类依赖 - 编译 [英] Gradle - Groovy and Java class dependency - Compile

查看:23
本文介绍了Gradle - Groovy 和 Java 类依赖 - 编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目同时包含 Java(N 个文件)和 Groovy 代码(仅 1 个文件).Java 编译依赖于这个单独的 Groovy 文件的类文件进行 Java 编译(compileJava 任务成功).

当我不使用 src/java 作为 main>groovy> sourceSet 部分中的 srcDir 之一时,我收到一条错误消息,指出在 groovy 文件/类中找不到类/符号.在 ANT 中,我们很容易先调用 compile-groovy 目标,然后再调用 compile-java 目标,但在 Gradle 中我试图找到相同的内容.

我阅读了一些帖子,发现如果我将 main>java 部分设为 NULL 并为 main>java 指定 srcDir,它是 main>groovy sourceSet 部分中的 src/java,那么它编译得很好.

我的?s:1. 有没有其他办法?例如,以下应该有效:

 compileJava {依赖 compileGroovy}

不过,这会进入一个无限循环.

在 compileJava 任务中使用 doFirst 怎么样:

compileJava {先做{编译Groovy}}

这也不起作用.






build.gradle 这是有效的,但 compileJava 在某种意义上在这里变得毫无用处,即使源代码有 N 没有.src/java 或 src/java-test 等树中的 java 文件.我知道这个构建脚本正在运行,但从逻辑上讲,如果他/她不熟悉为什么 Groovy 的 sourceSet 必须将src/java"作为其 srcDir 值,那么从逻辑上讲,它可能会给开发人员带来一些困惑.

应用插件:'java'应用插件:'groovy'源集{主要的 {时髦的{srcDir 'src/groovy'srcDir 'src/java'}爪哇{//以下需要注释掉 OR Gradle will always pick compileJava before compileGroovy//srcDir 'src/java'//srcDir 'src/java-test'}}测试 {时髦的{srcDir '测试/常规'}爪哇{srcDir '测试/java'}资源 {srcDir '测试/资源'srcDir 'conf'}}集成测试{时髦的{srcDir 'src/groovy-test'}爪哇{srcDir 'src/java-test'}资源 {srcDir '测试/资源'srcDir 'conf'}}}

其他链接:如何让 Gradle 在 Java 测试之前编译 Groovy 测试

Groovy(基础)插件使得 GroovyCompile 任务依赖于相应的 JavaCompile 任务,因为它更常见从 Groovy 调用到 Java 而不是相反.如果您需要反过来(或双向),联合编译是一个很好的解决方案.这是一个稍微改进的(在您的版本上)联合编译设置:

sourceSets {主要的 {时髦的{//覆盖默认位置,而不是添加其他位置srcDirs = ['src/groovy', 'src/java']}爪哇{srcDirs = []//不要编译 Java 代码两次}}}

如果您更喜欢仅使用 Java->Groovy 依赖项单独编译,则应该可以使用以下内容:

//因为您没有使用默认位置源集{主要的 {时髦的{srcDirs = ['src/groovy']}爪哇{srcDirs = ['src/java']}}}//移除 GroovyCompile->JavaCompile 任务依赖任务.withType(GroovyCompile){依赖 = []}//添加 JavaCompile->GroovyCompile 任务依赖tasks.withType(JavaCompile) { 任务 ->dependsOn task.name.replace("Java", "Groovy")}

因为 JavaCompile 任务及其对应的 GroovyCompile 任务写入相同的输出目录,所以 Java 编译现在将在其编译类路径上包含已编译的 Groovy 代码.

PS:不支持从另一个任务调用一个任务,如果你尝试,可能会发生不好的事情.相反,您应该始终处理任务关系(dependsOnfinalizedBymustRunAftershouldRunAfter).

My project has both Java (N files) and Groovy code (1 file only). Java compile depends upon this single Groovy file's class file for Java compilation (compileJava task to succeed).

When I don't use src/java as one of the srcDir within main>groovy> sourceSet section, then I get an error saying class/symbol not found which is in the groovy file/class. In ANT, it's easy that we are calling compile-groovy target first, before calling compile-java target but the same in Gradle is what I'm trying to find.

I read some posts and found that if I make main>java section NULL and specify srcDir for main>java which is src/java inside main>groovy sourceSet section, then it compiles fine.

My ?s: 1. Is there any other way to do? for ex, the following should work:

   compileJava {
     dependsOn compileGroovy
   }

though, this goes to an infinte loop.

OR

what about using doFirst for compileJava task:

compileJava {
      doFirst {
            compileGroovy
      }
}

this doesn't work either.






build.gradle This works, but compileJava in one sense becomes useless here even though the source code has N no. of java files in the src/java or src/java-test etc tree. I know this build script is working but logically it might bring some confusion to the developer if s/he is not familiar why sourceSet for Groovy MUST have "src/java" as its srcDir value.

apply plugin: 'java'
apply plugin: 'groovy'

sourceSets {
   main {
      groovy {
         srcDir 'src/groovy'
         srcDir 'src/java'
      }
      java {
       //The following needs to be commented out OR Gradle will always pick compileJava before compileGroovy
       //srcDir 'src/java'
       //srcDir 'src/java-test'
      }
   }
   test {
      groovy {
         srcDir 'test/groovy'
      }
      java {
         srcDir 'test/java'
      }
      resources {
         srcDir 'test/resources'
         srcDir 'conf'
      }
   }
   integrationTest {
      groovy {
         srcDir 'src/groovy-test'
      }
      java {
         srcDir 'src/java-test'
      }
      resources {
         srcDir 'test/resources'
         srcDir 'conf'
      }
   }
}

Other links: How to make Gradle compile Groovy tests before Java tests

解决方案

The Groovy (base) plugin makes GroovyCompile tasks depend on the corresponding JavaCompile tasks because it's more common to call from Groovy into Java than the other way around. If you need it the other way around (or both ways), joint compilation is a good solution. Here is a somewhat improved (over your version) joint compilation setup:

sourceSets {
    main {
        groovy {
            // override the default locations, rather than adding additional ones
            srcDirs = ['src/groovy', 'src/java'] 
        }
        java {
            srcDirs = [] // don't compile Java code twice 
        }
    }
}

If you prefer separate compilation with Java->Groovy dependencies only, something like the following should work:

// since you aren't using the default locations
sourceSets {
    main {
        groovy {
            srcDirs = ['src/groovy']
        }
        java {
            srcDirs = ['src/java']
        }
    }
}

// remove GroovyCompile->JavaCompile task dependencies
tasks.withType(GroovyCompile) {
    dependsOn = [] 
}

// add JavaCompile->GroovyCompile task dependencies
tasks.withType(JavaCompile) { task ->  
    dependsOn task.name.replace("Java", "Groovy")
}

Because a JavaCompile task and its corresponding GroovyCompile task write to the same output directory, Java compilation will now have the compiled Groovy code on its compile class path.

PS: Calling a task from another task is not supported, and bad things can happen if you try. Instead, you should always work with task relationships (dependsOn, finalizedBy, mustRunAfter, shouldRunAfter).

这篇关于Gradle - Groovy 和 Java 类依赖 - 编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆