How to package a java standalone app using maven assembly

The sample codes below will assemble a zipped package of a java standalone app. Dependencies: maven-jar-plugin - to create the jar maven-ass...

The sample codes below will assemble a zipped package of a java standalone app.

Dependencies:
  • maven-jar-plugin - to create the jar
  • maven-assembly-plugin - to assemble the distribution package
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.broodcamp.App</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>${basedir}/src/main/resources/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

What we have in the code:
  1. In the jar plugin we define the main class and set the classpath to lib.
  2. We supply a descriptor on how we want to assemble the distribution package. We also bind the plugin to maven's package phase and single goal. So that we we execute maven install, the package will be automatically generated.
assembly.xml file
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>MyApp Scraper</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<filtered>true</filtered>
<directory>src/main/resources</directory>
<outputDirectory>./</outputDirectory>
<excludes>
<exclude>assembly.xml</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>./</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
<unpack>false</unpack>
<useTransitiveDependencies>true</useTransitiveDependencies>
<includes>
<include>*</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>

assembly is an xml file where we define how we want to package our distribution. What we have:

  1. format of the package is zip
  2. We package all the resources inside src/main/resources folder excluding assembly.xml
  3. We include all the jar files in the ${project.build.directory} folder which is basically the target.
  4. We save the maven dependencies in the lib folder. Note that we set the class path to the same directory. We set transitive dependencies to true and includes all. For some reason, without include=*, some transitive dependencies are not included.

COMMENTS

mas template
Name

amazon,1,angular,8,bigdata,2,business,1,course-spring,27,courses,6,database,4,docker,3,java,50,kafka,1,keycloak,4,microservices,5,mysql,1,neworking,1,nosql,2,php,1,pinned,2,react,3,server management,7,shared drive,1,spring,7,synology,1,troubleshooting,2,web,1,wordpress,1,
ltr
item
toztech: How to package a java standalone app using maven assembly
How to package a java standalone app using maven assembly
toztech
https://toztech.blogspot.com/2016/12/how-to-package-java-standalone-app.html
https://toztech.blogspot.com/
https://toztech.blogspot.com/
https://toztech.blogspot.com/2016/12/how-to-package-java-standalone-app.html
true
2554149350007112447
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content