如何从具有依赖项的 jar 创建 zip 文件?
我想生成一个 zip 文件,其中包含:
- 一个 fat jar
- 一些其他静态 .bat 文件
我尝试使用 maven- assembly-plugin ,因为它似乎能够做到这一点。
这是我的尝试:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.company</groupId>
<artifactId>some-application</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>some-application</name>
<!-- ... -->
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-fat-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>my.company.some-application.SomeApplication</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
assembly.xml
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
<id>data-initialization-batch</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<files>
<file>
<source>compileDataInit.bat</source>
</file>
<file>
<source>dataInit.bat</source>
</file>
</files>
<dependencySets>
<dependencySet>
<includes>
<include>my.company:some-application:jar:jar-with-dependencies</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
这会生成以下警告:
[警告] 在此工件包含过滤器中从未触发以下模式: o 'my.company:some-application:jar:jar-with-dependencies'
并且输出 zip 文件包含 bat 文件,但根本没有 jar。
让我相信这个配置应该起作用的是 docs< 中的这句话< /a>:
创建程序集时,它将使用 assemblyId 作为 工件的分类器,并将创建的程序集附加到 项目,以便在安装时将其上传到存储库中 和部署阶段。
更多详细信息:
- 我有两次执行,因为当我将一个
descriptorRef
和一个descriptor
放在同一个execution
中时,它们显然是无序的并且可以'相互依赖 - 当我使用
时,常规 jar 会正确包含在输出 zip 中,但我需要 fat jar。my.company:some-application
I want to generate a zip file which contains:
- A fat jar
- Some other static .bat files
I tried to use maven-assembly-plugin
because it seems to be able to do that.
Here is my attempt:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.company</groupId>
<artifactId>some-application</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>some-application</name>
<!-- ... -->
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-fat-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>my.company.some-application.SomeApplication</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
assembly.xml
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
<id>data-initialization-batch</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<files>
<file>
<source>compileDataInit.bat</source>
</file>
<file>
<source>dataInit.bat</source>
</file>
</files>
<dependencySets>
<dependencySet>
<includes>
<include>my.company:some-application:jar:jar-with-dependencies</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
This generates the following warning:
[WARNING] The following patterns were never triggered in this artifact inclusion filter:
o 'my.company:some-application:jar:jar-with-dependencies'
And the output zip file contains the bat files but no jar at all.
What makes me believe that this configuration should work is this sentence in the docs:
When the assembly is created it will use the assemblyId as the
artifact's classifier and will attach the created assembly to the
project so that it will be uploaded into the repository in the install
and deploy phase.
Some more details:
- I have two executions because when I put one
descriptorRef
and onedescriptor
in the sameexecution
, they are apparently unordered and can't depend on each other - When I use
<include>my.company:some-application</include>
the regular jar gets properly included in the output zip, but I need the fat jar instead.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我只是缺少 组装参考
Ok I was just missing the
useProjectAttachments
option which is described in the Assembly reference