使用 Maven 创建包含具有依赖项的可执行 jar 的 zip 文件

发布于 2024-09-19 11:32:58 字数 921 浏览 11 评论 0原文

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

编辑:

我现在有了 jar 构建,还有一个基本的 zip 。我的程序集文件如下所示:

<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'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?

edit:

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>

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?

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不弃不离 2024-09-26 11:32:58

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

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

最后,将程序集创建绑定到 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 包。

如果我的答案不符合您的需求,请随时更新您的问题,并且您还可以提供当前的 assemble.xmlpom.xml...


编辑

关于您的更新:

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

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

对于创建的 ZIP 的名称,您可以在 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>

如果我是正确的,程序集将使用此名称,添加 ID< /code> 在 assembly.xml 文件中定义,并使用适合指定格式的前缀。

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

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.

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>
      ...

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.

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...


Edit

Regarding your update:

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>

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>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文