JPMS 是否支持模块版本?

Does JPMS support module version?

我以为JPMS不支持模块版本。但是,当我执行 java --list-modules 时,我得到以下输出:

java.activation@9
java.base@9
java.compiler@9
java.corba@9
java.datatransfer@9
java.desktop@9
java.instrument@9 
....

所以,我无法理解这个 @9 是什么。是这个版本还是什么?如果JPMS支持模块版本,我可以在模块A的module-info中设置,模块A需要某个版本的模块B吗?

I can't understand what this @9 is. Is this version or what?

是的,就是模块的版本。

If JPMS supports module version, can I set in module-info of module A, that module A requires module B of certain version?

不,您不能在另一个模块的声明中引用一个模块的特定版本。我相信 The State of the Module System#Module Declarations

中总是明确提到这一点

一个模块的声明不包括版本字符串,也不限制它所依赖的模块的版本字符串。这是有意为之,因为 模块系统的目标不是解决 version-selection 问题 ,最好留给构建工具和容器应用程序。

要进一步了解现有 @9 信息:

JVMS 9在Module_attribute结构中包含一个字段module_version_index,即class文件格式支持存储版本字符串一个模块,甚至 requires_version_index 已被定义,但我不知道任何与 评估 这个版本相关的规范,此时呈现此数据纯粹是提供信息。

有关模块版本的当前状态(截至 Java 9 GA)的更多信息,请参见 Issue Summary. The format of versions is defined in ModuleDescriptor.Version API。

Java模块系统无意解决版本选择或验证问题。

但是,它确实支持将版本信息添加到通过模块 API 可用的模块 jar。

类似地,另一个模块上的“requires”子句将包含编译它所针对的依赖项的版本。前提是其他模块需要包含版本号。

通过 API 提供这两个版本使得其他框架可以验证由不同模块组成的应用程序是否具有兼容版本(例如基于语义版本控制)。

在 Maven 构建中,java“jar”工具的以下用法允许将 project.version 添加到模块化 jar:

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
                <execution>
                    <id>add-version-to-jar</id>
                    <phase>package</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <executable>jar</executable>
                        <workingDirectory>${project.build.directory}</workingDirectory>
                        <arguments>
                            <argument>--update</argument>
                            <argument>--verbose</argument>
                            <argument>--module-version</argument>
                            <argument>${project.version}</argument>
                            <argument>--file</argument>
                            <argument>${project.build.finalName}.jar</argument>
                            <argument>-C</argument>
                            <argument>${project.build.outputDirectory}</argument>
                            <argument>.</argument>
                        </arguments>
                    </configuration>
                </execution>
            </executions>
</plugin>

可以在

上找到更详细的说明和代码