如何創建一個assembly.xml,它將執行jar和dependencies(解包所有這些jar文件)幷包含另一個項目(未解壓縮)的war文件。
假設你有類似下面的一個項目結構(我假設一個簡單的結構,因爲你沒有提到什麼都講究吧):
.
├── pom.xml
└── src
├── main
│ ├── assembly
│ │ └── uberjar.xml
│ └── java
│ └── com
│ └── stackoverflow
│ └── App.java
└── test
└── java
└── com
└── stackoverflow
└── 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的戰爭,未解壓
- 來自項目i的類自行宣佈
如下圖所示:
$ mvn clean package
[INFO] Scanning for projects...
...
$ cd target; jar xvf Q3762049-1.0-SNAPSHOT-uberjar.jar
created: META-INF/
inflated: META-INF/MANIFEST.MF
created: org/
created: org/apache/
created: org/apache/commons/
created: org/apache/commons/lang/
created: org/apache/commons/lang/builder/
created: org/apache/commons/lang/enum/
created: org/apache/commons/lang/enums/
created: org/apache/commons/lang/exception/
created: org/apache/commons/lang/math/
created: org/apache/commons/lang/mutable/
created: org/apache/commons/lang/text/
created: org/apache/commons/lang/time/
inflated: META-INF/LICENSE.txt
inflated: META-INF/NOTICE.txt
inflated: org/apache/commons/lang/ArrayUtils.class
...
created: META-INF/maven/
created: META-INF/maven/commons-lang/
created: META-INF/maven/commons-lang/commons-lang/
inflated: META-INF/maven/commons-lang/commons-lang/pom.xml
inflated: META-INF/maven/commons-lang/commons-lang/pom.properties
inflated: my-webapp-1.0-SNAPSHOT.war
created: com/
created: com/stackoverflow/
inflated: com/stackoverflow/App.class
你想在一個大的Jar文件的一切嗎?你看過Uber-Jar嗎? – Thomas 2010-09-21 20:11:17