11/07/2012

Maven package jar file, include all the dependencies.

1. If we do not have dependencies with scope other than "runtime". We can config pom.xml simply in this way:

01 <plugin>
02                 <groupId>org.apache.maven.plugins</groupId>
03                 <artifactId>maven-assembly-plugin</artifactId>
04                 <version>2.2.1</version>
05 
06                 <executions>
07 
08                     <execution>
09                         <id>package-jar-with-dependencies</id>
10                         <phase>package</phase>
11                         <goals>
12                             <goal>single</goal>
13                         </goals>
14                         <configuration>
15                             <appendAssemblyId>false</appendAssemblyId>
16                             <descriptorRefs>
17                                 <descriptorRef>jar-with-dependencies</descriptorRef>
18                             </descriptorRefs>
19                         </configuration>
20                     </execution>
21 
22                     <execution>
23                         <id>make-assembly</id>
24                         <phase>package</phase>
25                         <goals>
26                             <goal>single</goal>
27                         </goals>
28                         <configuration>
29                             <descriptors>
30                                 <descriptor>assembly/assembly.xml</descriptor>
31                             </descriptors>
32                         </configuration>
33                     </execution>
34                 </executions>
35 </plugin>

Now all the dependencies will be added into the jar file. If we want to assembly the project, we can add another assembly.xml to control the assemble.

"assembly.xml":

<assembly>
02   <id>bin</id>
03   <formats>
04     <format>zip</format>
05   </formats>
06   <dependencySets>
07     <dependencySet>
08       <useProjectArtifact>true</useProjectArtifact>
09       <excludes>
10         <exclude>log4j:log4j</exclude>
11       </excludes>
12       <outputDirectory>lib</outputDirectory>
13     </dependencySet>
14   </dependencySets>
15   <fileSets>
16     <fileSet>
17       <directory>docs</directory>
18       <outputDirectory>/docs</outputDirectory>
19       <includes>
20         <include>*.*</include>
21       </includes>
22     </fileSet>
23     <fileSet>
24       <directory>logs</directory>
25       <outputDirectory>/logs</outputDirectory>
26       <includes>
27         <include>*.*</include>
28       </includes>
29     </fileSet>
30     <fileSet>
31       <directory>scripts</directory>
32       <outputDirectory>/bin</outputDirectory>
33       <includes>
34         <include>*.*</include>
35       </includes>
36     </fileSet>
37      <fileSet>
38       <directory>config</directory>
39       <outputDirectory>/conf</outputDirectory>
40       <includes>
41         <include>*.*</include>
42       </includes>
43     </fileSet>
44   </fileSets>
45 </assembly>

Reference:

2. If we have dependencies with scope other than "runtime", e.g. "system".

<dependency>
  <groupId>sample-commons</groupId>
  <artifactId>security</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${basedir}/lib/sample-commons-security-1.0.0.jar</systemPath>
</dependency>

If we use this way, jar files with scope "system" will be ignored when assembly into jar file. We can config like this to include all the jar files. Write jar-with-dependencies Descriptor ourselves:
<plugin>
02                 <groupId>org.apache.maven.plugins</groupId>
03                 <artifactId>maven-assembly-plugin</artifactId>
04                 <version>2.2.1</version>
05 
06                 <executions>
07 
08                     <execution>
09                         <id>package-jar-with-dependencies</id>
10                         <phase>package</phase>
11                         <goals>
12                             <goal>single</goal>
13                         </goals>
14                         <configuration>
15                             <appendAssemblyId>false</appendAssemblyId>
16                             <descriptors>
17                                 <descriptor>assembly/jar-with-dependencies.xml</descriptor>
18                             </descriptors>
19 
20                         </configuration>
21                     </execution>
22 
23                     <execution>
24                         <id>make-assembly</id>
25                         <phase>package</phase>
26                         <goals>
27                             <goal>single</goal>
28                         </goals>
29                         <configuration>
30                             <descriptors>
31                                 <descriptor>assembly/assembly.xml</descriptor>
32                             </descriptors>
33                         </configuration>
34                     </execution>
35                 </executions>
36 </plugin>


jar-with-dependencies.xml:

01 <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" 
02   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
03   xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
04   
05   <id>lib</id>
06   <formats>
07     <format>jar</format>
08   </formats>
09   <includeBaseDirectory>false</includeBaseDirectory>
10   <dependencySets>
11     <dependencySet>
12       <outputDirectory>/</outputDirectory>
13       <useProjectArtifact>true</useProjectArtifact>
14       <unpack>true</unpack>
15       <scope>runtime</scope>
16     </dependencySet>
17     <dependencySet>
18       <outputDirectory>/</outputDirectory>
19       <useProjectArtifact>true</useProjectArtifact>
20       <unpack>true</unpack>
21       <scope>system</scope>
22     </dependencySet>
23   </dependencySets>
24 </assembly>

In this way, we can assemble all the dependencies into the jar file packaged.