如何创建一个 assembly.xml 来执行具有依赖项的 jar(解压缩所有这些 jar 文件)并包含来自另一个项目的 war 文件(未解压缩)。
假设你有一个类似于下面的项目结构(我假设一个简单的结构,因为你没有提到任何特别的东西):
.
├── pom.xml
└── 源
├── 主要
│ ├── 组装
│ │ └── uberjar.xml
│ └── java
│ └── com
│ └── stackoverflow
│ └── App.java
└── 测试
└── java
└── com
└── 堆栈溢出
└── AppTest.java
使用以下 pom.xml:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow</groupId>
<artifactId>Q3762049</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- this is the war we want to include in the assembly -->
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>my-webapp</artifactId>
<version>1.0-SNAPSHOT</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
<!-- and below, the other dependencies -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
...
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/uberjar.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
如你看到的,
- 我们不会在
jar-with-dependencies
这里使用预定义的描述符,我们将在我们自己的自定义程序集描述符中重用它。
- 我们在战争上声明了一个具有
runtime
范围的依赖项,以便我们能够将其包含在程序集中。
现在,习惯uberjar.xml
:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>uberjar</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<unpack>true</unpack>
<scope>runtime</scope>
<useProjectArtifact>false</useProjectArtifact>
<excludes>
<exclude>*:war</exclude>
</excludes>
</dependencySet>
<dependencySet>
<unpack>false</unpack>
<scope>runtime</scope>
<useProjectArtifact>false</useProjectArtifact>
<includes>
<include>*:war</include>
</includes>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>
jar-with-dependencies
这是将创建一个 jar的描述符的一个小变体:
- 除了战争之外的依赖,解包
- webapp之战,未解包
- 项目本身的类
如下所示:
$ mvn 清洁包
[INFO] 正在扫描项目...
...
$ cd 目标;jar xvf Q3762049-1.0-SNAPSHOT-uberjar.jar
创建:META-INF/
膨胀:META-INF/MANIFEST.MF
创建:org/
创建:org/apache/
创建:org/apache/commons/
创建:org/apache/commons/lang/
创建:org/apache/commons/lang/builder/
创建:org/apache/commons/lang/enum/
创建:org/apache/commons/lang/enums/
创建:org/apache/commons/lang/exception/
创建:org/apache/commons/lang/math/
创建:org/apache/commons/lang/mutable/
创建:org/apache/commons/lang/text/
创建:org/apache/commons/lang/time/
膨胀:META-INF/LICENSE.txt
膨胀:META-INF/NOTICE.txt
膨胀:org/apache/commons/lang/ArrayUtils.class
...
创建:META-INF/maven/
创建:META-INF/maven/commons-lang/
创建:META-INF/maven/commons-lang/commons-lang/
膨胀:META-INF/maven/commons-lang/commons-lang/pom.xml
膨胀:META-INF/maven/commons-lang/commons-lang/pom.properties
膨胀:my-webapp-1.0-SNAPSHOT.war
创建:com/
创建:com/stackoverflow/
膨胀:com/stackoverflow/App.class