How to package a java standalone app using maven assembly
Tech-Today

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:
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.




- How To Create A Modularized Ear Project In Maven
This post is one way of creating a typical javaee6 maven project that contains ear, web, ejb and api. The output of course is an ear file that contains (web, ejb and api). How it looks like (assuming our top project is named ipiel): +ipiel  +ipiel-ear...

- How To Get Git Build Id Using Maven
There are times when git's build number is important for a release. Specially in development mode, when there are frequent releases. So if we want to append the build number on our page, how do we automate it? For us to achieve this we will need 2...

- A Maven Project That Have Multiple Property Files Via Maven Antrun Plugin
This tutorial will answer several questions (below) within a maven project that has several configuration files (dataset, property, persistence). 1.) How to have multiple persistence, datasource and property files depending on the profile or environment...

- How To Ignore Maven's Plugin Execution Not Covered By Lifecycle Configuration Warning
There are times when you want to ignore some of maven warnings or errors. To do so, normally we add the ff code in the parent project's pom.xml. For example to ignore liferay's theme-merge and build-thumbnail goal warning. <pluginManagement>...

- How To Add Pmd Reporting To Maven
Before proceeding to this exercise, you may want to try my tutorial on how to setup a maven project to eclipse: http://czetsuya-tech.blogspot.com/2012/04/how-to-create-modularized-maven-project.html. Assuming you have follow the tutorial above, you should...



Tech-Today








.