简体   繁体   English

我有一个使用多个.groovy文件的大型Groovy应用程序(~2000行)。 我该如何处理生成的类文件?

[英]I've got a large Groovy application (~2000 lines) using multiple .groovy files. How should I handle the generated class files?

When the application is completely self contained within one .groovy file there are no .class files generated. 当应用程序完全独立于一个.groovy文件中时,不会生成.class文件。 However, when I separate out the classes into separate files and place them in packages, I end up with hundreds of generated .class files (most are from all of the closures compiling to bytecode separately). 但是,当我将类分离成单独的文件并将它们放在包中时,我最终得到了数百个生成的.class文件(大多数是从所有的闭包编译到字节码分别)。

What is the best approach for managing the file system when all of these files are generated and end up cluttering the folder? 在生成所有这些文件并最终使文件夹混乱时,管理文件系统的最佳方法是什么?

  1. Should I leave everything in one file? 我应该把所有东西都放在一个档案里 That's the cleanest approach to the file system but I end up with a large .groovy file and no separation of logic. 这是文件系统最干净的方法,但我最终得到一个大的.groovy文件,没有逻辑分离。
  2. Should I package everything into a jar? 我应该把所有东西都装进罐子里吗? That would work but it cuts down on the flexibility of having a script run directly with small changes as necessary. 这样可行,但它可以减少在必要时使用小更改直接运行脚本的灵活性。
  3. Do I just ignore the .class files? 我只是忽略.class文件?

Option #2 is best, provided you use a build tool like Gradle . 如果您使用像Gradle这样的构建工具,则选项#2是最好的。 Our DevOps team uses Groovy, and delivers self-contained zip files. 我们的DevOps团队使用Groovy,并提供自包含的zip文件。

As an simple example, look at this project . 举个简单的例子,看看这个项目

It delivers warO.zip , which unzips to: 它提供warO.zip ,解压缩到:

warO.jar
jars/groovy-all-1.6.4.jar
jars/guava-collections-r03.jar
jars/guava-base-r03.jar

Because the manifest is set with main class and classpath, the user can then execute with: 因为清单是使用主类和类路径设置的,所以用户可以执行:

java -jar warO.jar

Your user audience will love it. 您的用户将会喜欢它。 They don't even need Groovy installed, just the JRE. 他们甚至不需要安装Groovy,只需安装JRE。

Regarding the dev cycle, it is true that edits to the script are not quite as fast, but by adding Gradle tasks (or simple scripts), it is easy to automate the steps to build, deploy locally, and execute. 关于开发周期,对脚本的编辑确实不是那么快,但通过添加Gradle任务(或简单脚本),可以轻松地自动执行构建,本地部署和执行的步骤。

Building an executable jar with your scripts and embedded Groovy is simple with Gradle . 使用Gradle构建一个带有脚本和嵌入式Groovy的可执行jar很简单。

The advantage is verification that the scripts are compilable and unit tested before deployment to a production environment. 优点是验证脚本在部署到生产环境之前是可编译和单元测试的。

Here's an example: 这是一个例子:

scripts/src/main/groovy/script1.groovy 脚本/ src目录/主/常规/ script1.groovy

println "Hello World!"

def aTestableMethod() {
    1 + 1
}

scripts/src/test/groovy/script1Test.groovy 脚本/ src目录/测试/常规/ script1Test.groovy

import org.junit.Test
import static org.junit.Assert.*

class Script1Test {
    @Test
    public void checkATestableMethod() {
        def script1 = new script1()
        assertEquals 2, script1.aTestableMethod()
    }
}

scripts/build.gradle 脚本/的build.gradle

apply plugin: 'groovy'
repositories {
    mavenCentral()
}
dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.2.2'
    testCompile 'junit:junit:4.11'
}
jar {
    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
        configurations.runtime.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
    manifest {
        attributes 'Main-Class': 'script1'
    }
}


To build and execute: 构建和执行:

 $ gradle build $ java -jar build/libs/scripts/scripts.jar Hello World! 


The Groovy Environment Manager (GVM) makes it easy to install and manage Groovy and related tools including Gradle . Groovy环境管理器(GVM)使安装和管理Groovy及相关工具(包括Gradle 变得简单。

First of all, if you compile the .groovy file, you would get the same .class files. 首先,如果编译.groovy文件,您将获得相同的.class文件。

That out of the way, I want to mention an alternative, but I don't advise using that. 除此之外,我想提一个替代方案,但我不建议使用它。

Groovy is very well able to compile dependent .groovy files at runtime. Groovy能够在运行时编译依赖的.groovy文件。 The first requirement is that the files follow the java conventions for packages. 第一个要求是文件遵循包的java约定。 For example if you have the class Foo in package bar in a .groovy file the file must be in ./bar/Foo.groovy and the root directory "." 例如,如果.groovy文件中的包条中有Foo类,则该文件必须位于./bar/Foo.groovy和根目录“。”中。 must be on the classpath. 必须在类路径上。

The second requirement is that Groovy must be able to clearly recognize the class as class and that it cannot be a property. 第二个要求是Groovy必须能够清楚地将类识别为类,并且它不能是属性。 So for example "def a = Foo.class" alone, is not clear, since Foo could be a property. 所以例如单独“def a = Foo.class”,不清楚,因为Foo可能是一个属性。 A "import bar.Foo" makes things clear. “import bar.Foo”让事情变得清晰。 Same for usage of the class in class-, interface-, field- or method-headers, as well as in catch clauses and new-instance expressions. 对于类,接口,字段或方法标头中的类以及catch子句和新实例表达式的使用也是如此。

The third requirement is that a GroovyClassLoader is used for looking up the class. 第三个要求是使用GroovyClassLoader查找类。

If you use the groovy command from the command line, then this is the case. 如果从命令行使用groovy命令,则情况就是这样。 The groovy command will put the current directory on the classpath as well. groovy命令也会将当前目录放在类路径上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM