How to assemble an standalone application using Maven

I’m a newbie at Maven, but I’m becoming a fan. Today I’d like to talk about a simple task, which Netbeans performs automatically in Ant based standalone project but doesn’t in Maven ones, assembling the library dependencies in a lib folder along with the jar file:

JMSBasicClientScreenshot

First of all, I had to add the Maven Dependency plugin to my pom.xml file, this action created a folder called dependency in my Maven target directory when a built the project:


<plugin>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
    <execution>
      <phase>process-sources</phase>
      <goals>
        <goal>copy-dependencies</goal>
      </goals>
      <configuration>
        <outputDirectory>${targetdirectory}</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

Later on, I also put the Maven JAR plugin with a configuration in order to add the CLASSPATH to the manifest file, using the same prefix (lib) I wanted to use in the assembly:


<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <addClasspath>true</addClasspath>                            
        <classpathPrefix>lib/</classpathPrefix>                            
        <mainClass>com.wordpress.fcosfc.JMSBasicClient</mainClass>
      </manifest>
     </archive>
  </configuration>
</plugin>

The following task was writing a custom assembly descriptor for the Maven Assembly plugin and putting in the folder src/main/assembly:


...
<id>bin</id>
<formats>
  <format>zip</format>
</formats>
<fileSets>
  <fileSet>
    <directory>${project.build.directory}</directory>
    <outputDirectory>/</outputDirectory>
    <includes>
      <include>*.jar</include>
    </includes>
  </fileSet>
  <fileSet>
    <directory>${project.build.directory}/dependency</directory>
    <outputDirectory>lib</outputDirectory>
  </fileSet>
</fileSets>
...

Finally, I had to add the Maven Assembly plugin to my pom.xml file:


<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.4</version>
  <configuration>
    <descriptors>
      <descriptor>src/main/assembly/customAssembly.xml</descriptor>
    </descriptors>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id> 
      <phase>package</phase> 
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

References

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s