我正在尝试为我的项目创建一个可分发的 zip,其中包含几个配置文件和目录,以及我的项目的可执行 jar。在 Maven 程序集插件中,我发现了如何制作具有完全依赖关系的可执行 jar。但是,我无法弄清楚如何在 jar 制作完成后在 jar 周围创建 zip 文件。理想情况下,我想将 jar 移动到已经包含正确文件和子目录的目录,然后一次压缩整个内容。有没有办法做到这一点?
编辑:
我现在有了 jar 建筑,还有一个基本的 zipper 。我的程序集文件如下所示:
<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 中创建空目录?另外,如何更改生成的文件的名称?
谢谢您的帮助!
请您参考如下方法:
首先,使用 Maven JAR 插件创建一个 executable JAR .
然后,配置您的程序集以包含所有需要的文件(最终您可以添加一个将启动应用程序的 run.bat
/run.sh
文件),以及 all dependencies .
最后,将程序集创建绑定(bind)到 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 包。
如果我的回答不符合您的需要,请随时更新您的问题,您也可以提供您当前的
assembly.xml
和
pom.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
在
assembly.xml
中定义文件,并使用适应指定格式的前缀。