Groovy Java 9模块支持 [英] Groovy Java 9 modules support

查看:176
本文介绍了Groovy Java 9模块支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了一些时间将用Groovy编写的项目迁移到Java10.现在可以编译并运行它了.但是它仍然没有利用Java 9模块化的任何好处.
探究Groovy和Java 9模块几乎没有任何效果.

I've spent some time to migrate my project written in Groovy to Java 10. Now it's possible to compile and run it. But still it doesn't use any benefits of Java 9 modularity.
Googling about Groovy and Java 9 modules gives almost nothing.

那么是否可以迁移Groovy项目以将JDK 10与Project Jigsaw模块一起使用?

So is it possible to migrate Groovy project to use JDK 10 with Project Jigsaw modules?

推荐答案

好吧,经过几天的实验,我得出了答案-是的,可以将Groovy与Project Jigsaw模块一起使用.
但这需要一些额外的努力.

Well, after a few days of experiments I come up with the answer - yes, it is possible to use Groovy with Project Jigsaw modules.
But it needs some additional effort.

假设我们具有以下文件结构:

Let's say we have following file structure:

├── build
├── jigsaw
│   └── module
│       └── test
│           └── Application.groovy
├── lib
│   └── groovy.all.jar
└── module-info.java  

module-info.java

module main {
    requires groovy.all;
}

Application.groovy

package jigsaw.module.test

class Application {
    static void main(String[] args) {
        println "Hello module!"
    }
}

首先,我们需要使用javac编译module-info.java文件,而不是使用groovyc编译所有文件,因为groovy将模块文件视为闭包.

First of all we need to compile module-info.java file with javac instead of compiling all files using groovyc because groovy treats module file as closure.

让我们这样做:

javac -d build --module-path lib/ module-info.java

-module-path 将包含我们的groovy.all.jar作为自动模块,其名称源自JAR文件名.

--module-path will include our groovy.all.jar as automatic module with name derived from JAR-file name.

接下来,我们需要编译Application.groovy

Next we need to compile Application.groovy

groovyc -d build jigsaw/module/test/Application.groovy

进展顺利.
编译后,我们有 module-info.class (也称为模块描述符)和 Application.class .

It goes smoothly.
After compilation we have module-info.class (aka module descriptor) and Application.class.

├── build
│   ├── jigsaw
│   │   └── module
│   │       └── test
│   │           └── Application.class
│   └── module-info.class
├── jigsaw
│   └── module
│       └── test
│           └── Application.groovy
├── lib
│   └── groovy.all.jar
└── module-info.java

现在让我们尝试运行已编译的模块.

Now let's try to run our compiled module.

java --module-path build:lib --module main/jigsaw.module.test.Application

这就是我们得到的

Error occurred during initialization of boot layer
java.lang.module.FindException: Unable to derive module descriptor for lib/groovy.all.jar
Caused by: java.lang.module.InvalidModuleDescriptorException: Provider class moduleName=groovy-all not in module

这是什么意思?我不知道.经过大量的搜寻后,我发现

And what does it mean? I don't know. After a lot of googling I found something similar.

因此,我们需要从JAR中手动删除以下文件:

So we need to manually remove from JAR these files:

  • /META-INF/services/org.codehaus.groovy.source.Extensions
  • /META-INF/services/org.codehaus.groovy.runtime.ExtensionModule

最后,我们的Java模块能够启动

Finally our Java module is able to start

java --module-path build:lib --module main/jigsaw.module.test.Application
Hello module!


所有操作都是使用Oracle JDK 10和Groovy 2.4.15完成的.


All manipulations were done using Oracle JDK 10 and Groovy 2.4.15.

这篇关于Groovy Java 9模块支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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