使用 Maven 创建一个包含具有依赖项的可执行 jar 的 zip 文件 [英] Using Maven to create a zip file containing an executable jar with dependencies

查看:70
本文介绍了使用 Maven 创建一个包含具有依赖项的可执行 jar 的 zip 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的项目创建一个可分发的 zip,其中包含多个配置文件和目录,以及我的项目的可执行 jar.在 Maven 程序集插件中,我找到了如何制作具有完全依赖关系的可执行 jar.但是,我一直无法弄清楚如何在制作 jar 后在 jar 周围创建 zip 文件.理想情况下,我想将 jar 移动到已经具有正确文件和子目录的目录,然后立即压缩整个文件.有没有办法做到这一点?

I'm trying to create a distributable zip of my project that contains several configuration files and directories, as well as the executable jar of my project. In the Maven assembly plug-in, I've found how to make the executable jar with full dependencies. However, I haven't been able to figure out how to create the zip file around the jar after it has been made. Ideally, I'd like to move the jar to a directory which already has the correct files and sub-directories, then zip the whole thing at once. Is there any way to do this?

我现在有了罐子建筑,还有一个基本的拉链.我的程序集文件如下所示:

I now have the jar building, and a rudimentary zip as well. My assembly file looks like this:

<assembly>
   <id>financials-import-server</id>
   <formats>
      <format>zip</format>
   </formats>
   <dependencySets>
      <dependencySet>
      </dependencySet>
   </dependencySets>
    <files>
       <file>
          <source>target/import-server-1.0.0-SNAPSHOT.jar</source>
          <destName>service.jar</destName>
          <outputDirectory>/</outputDirectory>
       </file>
    </files>
</assembly>

我很乐意包含我需要的其他文件,例如配置文件或 shell 脚本.我还有几个问题.我将如何在 zip 中创建空目录?另外,如何更改生成的文件的名称?

I feel comfortable including the other files I would need, such as config files or shell scripts. I have a few questions remaining. How would I create empty directories within the zip? Also, how do I change the name of the file that is produced?

感谢您的帮助!

推荐答案

首先,使用 Maven JAR 插件创建一个 可执行 JAR.

First, use the Maven JAR plugin to create an executable JAR.

然后,配置您的程序集以包含所有需要的文件(最终您可以添加一个 run.bat/run.sh 文件来启动应用程序),并且还有 所有依赖项.

Then, configure your assembly to include all the files needed (eventually you can add a run.bat / run.sh file that will launches the application), and also all dependencies.

最后,将程序集创建绑定到 pom.xml 中的包目标:

Finally, bind the assembly creation to your package goal in the pom.xml:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>2.2-beta-5</version>
      <configuration>
        <descriptors>
          <descriptor>/path/to/my-assembly.xml</descriptor>
        </descriptors>
      </configuration>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
      ...

这样,您只需要运行一个 Maven 命令 mvn clean install 例如,来编译您的项目,然后创建 ZIP 包.

This way, you will simply need to run one Maven command mvn clean install for example, to compile your project, and then creates the ZIP package.

如果我的回答不符合您的需要,请随时更新您的问题,您也可以提供当前的 assembly.xmlpom.xml...

Do not hesitate to update your question if my answer doesn't fit your needs, and you can also give your current assembly.xml and pom.xml...

编辑

关于您的更新:

我认为程序集不会让您创建一个空目录.一个想法是放置一个空文件(例如,readme.txt)并将目录包含在最终的 ZIP 中.例如,在我的项目中,我想要一个 logs/ 目录,所以我的项目中有一个 logs/ 目录,它只包含一个 readme.txt 文件:

I don't think the assembly will let you create an empty directory. An idea is to put an empty file (or a readme.txt for example) and include the directory in the final ZIP. For example, in my project, I want to have a logs/ directory, so I have a logs/ directory in my project which only contains a readme.txt file:

<fileSets>
    <fileSet>
        <directory>../my-package/logs</directory>
        <outputDirectory>logs/</outputDirectory>
    </fileSet>
</fileSets>

对于创建的ZIP的名称,可以在pom.xml中指定:

For the name of the ZIP created, you can specify it in the pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2-beta-4</version>
            <configuration>
                <descriptors>
                    <descriptor>my-assembly.xml</descriptor>
                </descriptors>
                <finalName>TheName</finalName>

如果我正确,程序集将使用此名称,添加assembly.xml 文件中定义的ID,并使用适应指定格式的前缀.

If I am correct, the assembly will use this name, add the ID defined in the assembly.xml file, and uses the prefix adapted to the format specified.

这篇关于使用 Maven 创建一个包含具有依赖项的可执行 jar 的 zip 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆