gradle 混合 java ,gradle 问题

Gradle - Groovy and Java class dependency - Compile

发表于 2019-6-6 | | 暂无分类

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 (dependsOnfinalizedBymustRunAftershouldRunAfter).



采集自互联网,如有侵权请联系本人

转载于:https://www.cnblogs.com/whm-blog/p/11394001.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值