Configuration to Create Java Project Jar with Dependencies

  • Create an assembly-descriptor.xml file and store it in src/main/resources folder.
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
  <!-- TODO: a jarjar format would be better -->
  <id>jar-with-dependencies</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <useProjectArtifact>true</useProjectArtifact>
      <unpack>true</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
</assembly>
  • Edit pom.xml, and add below code inside plugins tag
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.5.3</version>
				<configuration>
					<descriptor>src/main/resources/assembly-descriptor.xml</descriptor>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<!-- <classpathPrefix>lib/</classpathPrefix> -->
							<mainClass>com.gojek.parking.ParkingLauncher</mainClass>
						</manifest>
					</archive>
				</configuration>
				<executions>
					<execution>
						<id>create-archive</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.