如何从具有依赖项的 jar 创建 zip 文件?
How do I create a zip file from a jar with dependencies?
我想生成一个 zip 文件,其中包含:
- 一个胖罐子
- 其他一些静态 .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>
这会生成以下警告:
[WARNING] The following patterns were never triggered in this artifact inclusion filter:
o 'my.company:some-application:jar:jar-with-dependencies'
并且输出的 zip 文件包含 bat 文件但根本没有 jar。
让我相信这个配置应该有效的是 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.
更多细节:
- 我有两次执行,因为当我将一个
descriptorRef
和一个 descriptor
放在同一个 execution
中时,它们显然是无序的并且不能相互依赖
- 当我使用
<include>my.company:some-application</include>
时,常规 jar 会正确包含在输出 zip 中,但我需要 fat jar。
好的,我只是错过了 Assembly reference
中描述的 useProjectAttachments
选项
我想生成一个 zip 文件,其中包含:
- 一个胖罐子
- 其他一些静态 .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>
这会生成以下警告:
[WARNING] The following patterns were never triggered in this artifact inclusion filter: o 'my.company:some-application:jar:jar-with-dependencies'
并且输出的 zip 文件包含 bat 文件但根本没有 jar。
让我相信这个配置应该有效的是 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.
更多细节:
- 我有两次执行,因为当我将一个
descriptorRef
和一个descriptor
放在同一个execution
中时,它们显然是无序的并且不能相互依赖 - 当我使用
<include>my.company:some-application</include>
时,常规 jar 会正确包含在输出 zip 中,但我需要 fat jar。
好的,我只是错过了 Assembly reference
中描述的useProjectAttachments
选项