3.3. 使用 Maven 生成 MANIFEST.MF 条目

使用 Maven JAR、EJB 或 WAR 包装插件的 Maven 项目可以生成具有 依赖性 条目的 MANIFEST.MF 文件。这不会自动生成依赖项列表,而是使用 pom .xml 中指定的详细信息创建 MANIFEST. MF 文件。

在使用 Maven 生成 MANIFEST.MF 条目前,您需要:

生成 MANIFEST.MF 文件包含模块依赖项

  1. 将以下配置添加到项目的 pom.xml 文件中的打包插件配置:

    <configuration>
       <archive>
          <manifestEntries>
             <Dependencies></Dependencies>
          </manifestEntries>
       </archive>
    </configuration>
  2. 将模块依赖项列表添加到 <Dependencies> 元素中。使用 MANIFEST.MF 文件中添加依赖项时使用的相同格式:

    <Dependencies>org.javassist, org.apache.velocity</Dependencies>

    可选导出 属性也可以在这里使用:

    <Dependencies>org.javassist optional, org.apache.velocity export</Dependencies>
  3. 使用 Maven 装配目标构建项目:

    [Localhost ]$ mvn assembly:single

    使用 assemble 目标构建项目时,最终的存档包含包含指定模块依赖项的 MANIFEST.MF 文件。

    示例: pom.xml 中的配置模块依赖项

    注意

    此处的示例显示了 WAR 插件,但它也与 JAR 和 EJB 插件(maven-jar-plugin 和 maven-ejb-plugin)搭配使用。

    <plugins>
       <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-war-plugin</artifactId>
          <configuration>
             <archive>
                <manifestEntries>
                   <Dependencies>org.javassist, org.apache.velocity</Dependencies>
                </manifestEntries>
             </archive>
          </configuration>
       </plugin>
    </plugins>