java - 将依赖 jar 插入安装程序 jar

标签 java maven-2 jar

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

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

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

最佳答案

您可以使用 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


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

关于java - 将依赖 jar 插入安装程序 jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/322907/

相关文章:

java - Maven jetty 插件和资源目录

java - 如何在文件关联 Windows 8 中指定 -jar 选项

Java float 到 long Typecast

scala - Sbt 替代 "mvn install -DskipTests"

java - 用Java编写线程

java - Sonar jacoco报告未执行

android - gradle 如何选择某些 url 来查找存储库?我如何将它指向正确的方向以便我可以导入这个 JAR?

hadoop - 从HDFS将Jar文件添加到Hive

java - 选择查询的最大日期

java - Android 在 Carousel CoverFlow (moondroid) 中停止无限循环/重复