使用其中的Groovy扩展方法构建项目 [英] Building a project with Groovy extension methods in it

查看:124
本文介绍了使用其中的Groovy扩展方法构建项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个简单的Groovy项目,其中包含带有Groovy扩展方法的软件包.

I'm trying to build a simple Groovy project which contains a package with a Groovy extension method.

当我尝试编译项目时,使用扩展方法的类找不到它. IDE正确提取了描述符和类.

When I try to compile the project the classes using the extension method can't find it. The descriptor and the class are picked up correctly by the IDE.

我想问题是在编译时需要扩展方法,而扩展方法可能还没有被编译. 我尝试创建其他sourceSet,但没有成功.

I guess the problem is that the extension method is needed at compile time by which it probably hasn't been compiled yet. I've tried creating different sourceSets but with no success.

dependencies {
    compile 'org.codehaus.groovy:groovy:2.4.7'
    compile 'org.membrane-soa:service-proxy-core:4.2.2'
    testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
    extensionCompile 'org.codehaus.groovy:groovy:2.4.7'
}

sourceSets {
    extension {
        groovy {
            include '**/ClosureExtension.groovy'
        }
    }
    main {
        groovy {
            compileClasspath += extension.output
        }
    }
}

扩展方法位于src/main/groovy/com/predic8/membrane/dsl/extension中,而项目的其余部分位于src/main/groovy/com/predic8/membrane/dsl/中.

The extension method lives in src/main/groovy/com/predic8/membrane/dsl/extension and the rest of the project in src/main/groovy/com/predic8/membrane/dsl/.

P.S .:所有的项目类都用@CompileStatic注释.

P.S.: The project classes are all annotated with @CompileStatic.

推荐答案

一种方法:将扩展代码分离到其自己的项目中,并从该项目中生成一个jar.然后,在另一个项目中,DSL代码可以使用该jar并将扩展jar 指定为依赖项.

One approach: separate the extension code into its own project, and produce a jar from that project. Then, in another project, the DSL code can use that jar and specify the extension jar as a dependency.

这将是一个巨大的答案,因此我将其模拟并放在GitHub 此处(所有应归功于 mrhaki的帖子).有两个项目:extensiondsl.假设您了解Gradle中的多项目构建,则本质是dsl/build.gradle:

This would make a huge answer, so I mocked it up and placed it on GitHub here (with all due credit to mrhaki's post). There are two projects: extension and dsl. Assuming you understand multi-project builds in Gradle, the essence is this line from dsl/build.gradle:

compileGroovy.dependsOn tasks.getByPath(':extension:jar') 

这篇关于使用其中的Groovy扩展方法构建项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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