模块描述文件是一个XML文件,用来描述模块的结构,内容,依赖关系,过滤器,以及其它属性。它的格式是富有表现力的,用于使基于文件系统的模块装载器允许描述文件和模块内容放在一起,而不需要在模块内容之中。特别是,它在文件系统中的位置是将模块名转成以点号分割的目录,并加上该模块的version slot。然后以模块根路径附上该目录就可以找到 module.xml 文件。
下面弄是一个被用于 JBoss Application Server 的模块描述文件:
模块描述文件示例:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="org.jboss.msc">
<main-class name="org.jboss.msc.Version"/>
<resources>
<resource-root path="jboss-msc-1.0.1.GA.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="org.jboss.logging"/>
<module name="org.jboss.modules"/>
<!-- Optional deps -->
<module name="javax.inject.api" optional="true"/>
<module name="org.jboss.threads" optional="true"/>
<module name="org.jboss.vfs" optional="true"/>
</dependencies>
</module>
module 元素
模块描述XML有一个根元素module 应用于 "urn:jboss:module:1.0 " 命名空间,它支持一下属性:
| Attribute | Type | Required? | Description |
|---|---|---|---|
| name | string | Yes | 该模块的名字。必须与要装载的模块名称相符。 |
| slot | string | No | The version slot. 如果没指定,默认为 "main ". |
"module " 元素可以包含下列子元素:
| Element | Required? | Description |
|---|---|---|
| main-class | No | 该模块的main类,main类在模块装载的时候,会执行其main方法 |
| resources | No | 组成该模块的资源 |
| dependencies | No | 模块的依赖 |
| exports | No | 定于用于过滤该模块export资源的过滤器 |
main-class 元素
定义了"main-class " 元素的模块意味着该模块是可执行的。也就是说,模块名能够被列在命令行中,然后"main-class"中的标准静态方法 "main(String[])"会被执行。
"main-class " 元素支持以下属性:
| Attribute | Type | Required? | Description |
|---|---|---|---|
| name | string | Yes | main class 的名称 |
这个元素不包含任何子元素。
| Note main class 不需要是该模块的实际资源,也不需要被exported。任何对于该模块可见的public class(包括所有imported的依赖或者资源)都可以作为main class, 只要它有方法"public static void main(String[] args) "。 |
resources 元素
为了让一个模块具有实际的内容,你必须为其定义至少一个"resources"元素。
"resources"元素不支持任何属性;它包含0至多个"resource-root"元素。"resource-root"元素支持下面的属性:
| Attribute | Type | Required? | Description |
|---|---|---|---|
| path | string | Yes | 该resource的路径,相对于module.xml位置 |
| name | string | No | 该resource的名称。如果未指定,则为resource的path |
另外,"resource-root " 元素可能包含一个子元素:
| Element | Required? | Description |
|---|---|---|
| filter | No | 作用于resource的目录过滤器。如果未指定,则接受所有路径。 |
参考过滤器定义一节获得更多信息。
dependencies 元素
一个模块可能通过"dependencies"元素描述一个或多个对于其它模块的依赖。"dependencies"元素不支持任何属性,它包含一个或多个"dependency"元素,支持一下属性:
| Attribute | Type | Required? | Description |
|---|---|---|---|
| name | string | Yes | 依赖的模块的名称 |
| slot | string | No | 依赖的模块的 version slot,默认为 "main ". |
| export | boolean | No | 指定这个依赖是否被再次export,默认为 "false" |
| services | enum | No | 指定定义在依赖模块中META-INF/services的 services是否被 import/export。可能的值有:"none", "import", "export",默认为 "none" |
| optional | boolean | No | 指定这个依赖是否是可选的,默认为 "false" |
* 对于Java service provider接口机制的介绍,可以参考: http://download.oracle.com/javase/tutorial/sound/SPI-intro.html
另外,"dependencies " 元素支持一下子元素:
| Element | Required? | Description |
|---|---|---|
| imports | No | 路径过滤器用于限定依赖模块中哪些路径被 import |
| exports | No | 路径过滤器用于限定依赖模块中哪些被 import 的路径可以被再次 export |
<dependencies>
<module name="org.jboss.example">
<imports>
<exclude-set>
<path name="org.jboss.example.tests"/>
</exclude-set>
</imports>
</module>
</dependencies>
参考过滤器定义获得关于过滤器更多的信息。
本文深入解析了模块描述文件的概念及其在JBossApplicationServer中的应用,包括XML格式、关键元素如'module'、'main-class'、'resources'、'dependencies'等的详细解释,以及如何配置模块的版本槽、依赖关系和资源路径。
7163

被折叠的 条评论
为什么被折叠?



