【问题标题】:Does JPMS support module version?JPMS 是否支持模块版本?
【发布时间】:2018-01-08 13:19:04
【问题描述】:

我认为 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 吗?

【问题讨论】:

    标签: java java-9 java-module java-platform-module-system module-info


    【解决方案1】:

    我不明白这个@9 是什么。是这个版本还是什么?

    是的,就是模块的版本。

    如果JPMS支持模块版本,我可以在模块A的module-info中设置吗, 那个模块 A 需要某个版本的模块 B?

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

    中总是明确提到这一点

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

    【讨论】:

    • 但是,没有构建工具可以解决2个模块需要另一个模块的其他版本的问题...
    • @9ilsdx9rvj0lo:Java 的 OSGi 模块框架支持这一点!见this answer
    【解决方案2】:

    为了更清楚地了解现有的@9 信息:

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

    有关模块版本的当前状态(从 Java 9 GA 开始)的更多信息可以在Issue Summary 中找到。版本格式在ModuleDescriptor.Version API 中定义。

    【讨论】:

      【解决方案3】:

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

      但是,它确实支持将版本信息添加到可通过 Module 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>
      

      可以在

      上找到更详细的描述和显示此内容的代码

      【讨论】: