使用位于另一个文件夹(Gradle)中的.groovy文件的类。 [英] Using class from .groovy file located in another folder (Gradle)

查看:196
本文介绍了使用位于另一个文件夹(Gradle)中的.groovy文件的类。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class Utils {$ b 

build.gradle $ b def doit(){
printlnhi从utils
}
}

任务hello<< {
def utils = new Utils()
utils.doit();如果我将类/ .groovy文件移动到这样的子文件夹中,则可以使用这个子文件夹来创建一个子文件夹:



  test 
- > groovy
- > Utils.groovy
- > build.gradle

并将build.gradle文件修改为:

  dependencies {
runtime fileTree(dir:'groovy',include:'* .groovy')
}

任务hello<< {
def utils = new Utils()
utils.doit();
}

不能再找到Utils类:

  ...无法解决课程实用程序

我假设子文件夹 groovy 应作为import / entry添加到.gradle文件中的classpath / dependency中。我已阅读这些网页:



http://gradle.org/docs/current/userguide/dependency_management.html
http://gradle.org/docs/current/userguide/custom_plugins.html
http://gradle.org/docs/current/userguide/custom_tasks.html



,但一直未能找到任何描述如何在.gradle文件中导入.groovy文件的文档。我错过了什么页面来描述这个简单的功能?

解决方案

如果您将build.gradle更改为

 任务hello<< {
def utils = new Utils()
utils.doit();
}

然后而不是

  test 
- > groovy
- > Utils.groovy
- > build.gradle

移动 Utils.groovy 文件到 buildSrc / src / main / groovy 就像这样:

  test 
- > buildSrc
- > src
- >主
- > groovy
- > Utils.groovy
- > build.gradle

和gradle应该自动提取它。



请参阅文档中的41.4。在buildSrc项目中生成源代码一节以获取更多信息。


I have this build.gradle file which works fine:

class Utils  {
  def doit(){
    println "hi from utils"
  }
}

task hello << {
  def utils = new Utils()
  utils.doit();
}

If I move the class/.groovy file to a sub-folder like this:

test
 -> groovy
     -> Utils.groovy
 -> build.gradle

and modify the build.gradle file to this:

dependencies {
    runtime fileTree(dir: 'groovy', include: '*.groovy')
}

task hello << {
  def utils = new Utils()
  utils.doit();
}

The Utils class can no longer be found:

...unable to resolve class Utils

I assume that the subfolder groovy should be added as an import/entry on the classpath/dependency in the .gradle file. I have read these pages:

http://gradle.org/docs/current/userguide/dependency_management.html http://gradle.org/docs/current/userguide/custom_plugins.html http://gradle.org/docs/current/userguide/custom_tasks.html

but have not been able to find any docs that describes how to import .groovy files in the .gradle file. What page am I missing that describes this simple functionality?

解决方案

If you change your build.gradle to simply be

task hello << {
  def utils = new Utils()
  utils.doit();
}

Then instead of

test
 -> groovy
     -> Utils.groovy
 -> build.gradle

move the Utils.groovy file to buildSrc/src/main/groovy like so:

test
 -> buildSrc
     -> src
         -> main
             -> groovy
                 -> Utils.groovy
 -> build.gradle

and gradle should pick it up automatically.

See the section "41.4. Build sources in the buildSrc project" in the documentation for further information.

这篇关于使用位于另一个文件夹(Gradle)中的.groovy文件的类。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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