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

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

问题描述

我的项目包含Java(N个文件)和Groovy代码(仅限1个文件)。 Java编译依赖于单个Groovy文件的Java编译类文件(compileJava任务成功)。



当我不使用src / java作为srcDir之一main> groovy> sourceSet部分,然后我得到一个错误,说在groovy文件/类中找不到类/符号。在ANT中,我们很容易首先调用compile-groovy目标,然后调用compile-java target,但Gradle中的相同是我试图找到的。



我读了一些帖子,发现如果我让main> java section NULL,并指定srcDir for main> java,这是src / java里面的main> groovy sourceSet部分,那么它编译好。



My?s:
1.是否有其他方法可以做?例如,以下应该工作:

  compileJava {
dependsOn compileGroovy
}

虽然这是一个无限循环。



OR



使用doFirst for compileJava任务如何:

  compileJava {
doFirst {
compileGroovy
}
}



无论如何。
$ b $









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

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

sourceSets {
main {
groovy {
srcDir'src / groovy'
srcDir'src / java'
}
java {
//以下需要注释掉或者Gradle会一直在compileGroovy之前选择compileJava
// srcDir'src / java'
// srcDir'src / java-test'
}
}
test {
groovy {
srcDir'test / groovy'
}
java {
srcDir'test / java'
}
资源{
srcDir' test / resources'
srcDir'conf'
}
}
integrationTest {
groovy {
srcDir'src / g roovy-test'
}
java {
srcDir'src / java-test'
}
资源{
srcDir'test / resources'
srcDir'conf'
}
}
}

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

解决方案

Groovy(base)插件使得 GroovyCompile 任务依赖于相应的 JavaCompile 任务,因为从Groovy调用Java到另一种方式比较常见。如果你需要它(或两种方式),联合编译是一个很好的解决方案。这是一个有点改进(通过您的版本)联合编译设置:

  sourceSets {
main {
groovy {
//覆盖默认位置,而不是添加额外的位置
srcDirs = ['src / groovy','src / java']
}
java {
srcDirs = [] //不要编译Java代码两次
}
}
}

如果您更喜欢单独编译Java-> Groovy依赖项,则应如下所示:

 <$ c 
main {
groovy {
srcDirs = ['src / groovy']
}因为您没有使用默认位置
sourceSets {
}
java {
srcDirs = ['src / java']
}
}
}

//删除GroovyCompile-> JavaCompile任务依赖关系
tasks.withType(GroovyCompile){
dependsOn = []
}

//添加JavaCompile-> GroovyCompile任务依赖关系
tasks.withType(JavaCompile){task - >
dependsOn task.name.replace(Java,Groovy)
}

因为一个 JavaCompile 任务及其相应的 GroovyCompile 任务写入同一个输出目录,所以Java编译现在会有在其编译类路径中编译的Groovy代码。



PS:不支持从其他任务调用任务,如果尝试,则会发生不好的事情。相反,您应该始终处理任务关系( dependsOn finalizedBy mustRunAfter shouldRunAfter )。


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天全站免登陆