【问题标题】:Insert a dependent jar into an installer jar将依赖 jar 插入安装程序 jar
【发布时间】:2010-09-24 06:53:54
【问题描述】:

我有一个带有安装程序子项目的多模块 maven 项目。安装程序将作为可执行 JAR 分发。它将设置数据库并将 WAR 文件提取到应用服务器。我想像这样使用 maven 来组装这个 jar:

/META-INF/MANIFEST.MF
/com/example/installer/Installer.class
/com/example/installer/...
/server.war

清单将有一个指向安装程序类的主类条目。我怎样才能让 maven 以这种方式构建 jar?

【问题讨论】:

    标签: java maven-2 jar


    【解决方案1】:

    您可以使用 Maven Assembly Plugin 构建 jar。

    首先,您需要向 pom.xml plugins 部分添加一些信息,以使生成的 jar 可执行:

    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>com.example.installer.Installer</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
    


    我建议使用单独的assembly descriptor 来构建实际的安装程序 jar。这是一个例子:

    <assembly>
      <id>installer</id>
    
      <formats>
        <format>jar</format>
      </formats>
    
      <baseDirectory></baseDirectory>
    
      <dependencySets>
        <dependencySet>
          <outputDirectory>/</outputDirectory>
          <includes>
            <!-- this references your installer sub-project -->
            <include>com.example:installer</include>
          </includes>
          <!-- must be unpacked inside the installer jar so it can be executed -->
          <unpack>true</unpack>
          <scope>runtime</scope>
        </dependencySet>
        <dependencySet>
          <outputDirectory>/</outputDirectory>
          <includes>
            <!-- this references your server.war and any other dependencies -->
            <include>com.example:server</include>
          </includes>
          <unpack>false</unpack>
          <scope>runtime</scope>
        </dependencySet>
      </dependencySets>
    </assembly>
    


    如果您已将程序集描述符保存为“installer.xml”,您可以通过运行程序集来构建您的 jar,如下所示:

    mvn clean package assembly:single -Ddescriptor=installer.xml


    希望这可以帮助。以下是一些您可能会觉得有用的附加链接:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-08
      • 1970-01-01
      • 2015-05-12
      • 2016-05-14
      • 2015-12-29
      • 1970-01-01
      • 2011-07-19
      相关资源
      最近更新 更多