no

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

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.

Related

source-code-management 7019939575872994510

Post a Comment Default Comments

item