Gradle:Groovy构建源代码目录

时间:2017-02-27 21:08:56

标签: gradle groovy build.gradle

我在class文件中创建了一个groovy build.gradle来自定义构建。

    buildscript {
       repositories {
          mavenLocal()
          mavenCentral()
       }
       dependencies {
         // dependencies here.
       }
    }

    import com.fasterxml.jackson.core.JsonFactory
    import com.fasterxml.jackson.databind.JsonNode
    import com.fasterxml.jackson.databind.ObjectMapper
    import com.fasterxml.jackson.dataformat.yaml.YAMLFactory

    class YamlToJsonFilter extends FilterReader {

    // my class definition here.
    .....
    ...
    }
task yamlToJson(type: Copy) {
   from 'src/main/yaml'
   into 'build/jsonschema'
   filter(YamlToJsonFilter)
   rename('(.*).yml', '$1.json')
}

在构建文件中使用groovy类看起来并不好看。我想将此类移动到独立文件并在此处使用它。我不知道这个类应该存在的目录以及如何在build.gradle文件中使用它的引用。

Edit1 正如Jeff Scott Brown在评论部分中所建议的那样,我在项目根目录下创建了buildSrc目录,并在新目录中创建了Jeff YamlToJsonFilter.groovy文件。这是目录结构

├── build.gradle
├── buildSrc
│   ├── YamlToJsonFilter.groovy
│   ├── build
│   │   ├── libs
│   │   │   └── buildSrc.jar
│   │   └── tmp
│   │       └── jar
│   │           └── MANIFEST.MF
│   └── build.gradle

我收到此错误:

:buildSrc:compileJava UP-TO-DATE
:buildSrc:compileGroovy UP-TO-DATE
:buildSrc:processResources UP-TO-DATE
:buildSrc:classes UP-TO-DATE
:buildSrc:jar UP-TO-DATE
:buildSrc:assemble UP-TO-DATE
:buildSrc:compileTestJava UP-TO-DATE
:buildSrc:compileTestGroovy UP-TO-DATE
:buildSrc:processTestResources UP-TO-DATE
:buildSrc:testClasses UP-TO-DATE
:buildSrc:test UP-TO-DATE
:buildSrc:check UP-TO-DATE
:buildSrc:build UP-TO-DATE

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/rakesh.kumar/ll/reference-project/event_schema/build.gradle' line: 43

* What went wrong:
A problem occurred evaluating root project 'sfl2_event_schema'.
> Could not get unknown property 'YamlToJsonFilter' for task ':yamlToJson' of type org.gradle.api.tasks.Copy.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

构建脚本无法识别YamlToJsonFilter并抱怨它。我还注意到与buildSrc相关的所有任务始终显示UP-TO-DATE,这让我觉得它甚至都没有检测到YamlToJsonFilter.groovy文件。

EDIT2 我在main/src/groovy/目录下创建了buildSrc并将文件移到了groovy目录下。现在它能够编译任务,我可以从build.gradle文件中引用该类。

0 个答案:

没有答案