How to add all dependencies in single jar in maven and then apply assembly plugin? - maven-2

My project has one parent module which has 3 child modules. I am trying to create a fat jar using maven assembly plugin. However, what I want is to create a fat jar and then apply assembly.xml file to further bundle my project.
module1 pom.xml
<dependencies>
<dependency>
<groupId>common</groupId>
<artifactId>commonutils</artifactId>
<version>1.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptor>src/main/assembly/oozie.xml</descriptor>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
my assembly file
<assembly>
<id>bundle</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<baseDirectory>search</baseDirectory>
<fileSets>
<fileSet>
<directory>${project.basedir}/src/main/workflow</directory>
<outputDirectory>/</outputDirectory>
<excludes>
<exclude>*/**.properties</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
<files>
<file>
<source>${project.basedir}/src/main/workflow/job.properties</source>
<outputDirectory>/</outputDirectory>
<filtered>true</filtered>
</file>
</files>
<moduleSets>
<moduleSet>
<useAllReactorProjects>true</useAllReactorProjects>
<binaries>
<outputDirectory>/</outputDirectory>
<unpack>false</unpack>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</binaries>
</moduleSet>
</moduleSets>
Upon running
mvn package
Final jar contains only module code and not all its dependencies ?
What is wrong here ??

Use the pre defined assembly descriptor jar-with-dependencies which exactly fulfill this purpose.

Related

Mule: Maven excluding certain mule jars while creating mule runnable jar

Need to create a runnable jar of a mule service using maven-assembly plugin. Its creating a zip file but excluding certain mule jars. When trying to run it from console getting classnotfound exception for mule-core jar. How can i include these jars as well as create a jar rather than zip. Message on console while packaging using maven clean package command from anypoint studio maven plugin is
[INFO] Using exclusions definition for Mule ESB Server version 3.6.0
[INFO] Using as WTP server : null
[INFO] excluded: xerces:xercesImpl
[INFO] excluded: xml-apis:xml-apis
[INFO] excluded: commons-codec:commons-codec
[INFO] excluded: org.slf4j:slf4j-api
[INFO] excluded: org.mule:mule-core
[INFO] excluded: commons-cli:commons-cli
[INFO] excluded: com.lmax:disruptor
[INFO] excluded: org.apache.logging.log4j:log4j-api
[INFO] excluded: org.apache.logging.log4j:log4j-core
[INFO] excluded: org.apache.logging.log4j:log4j-slf4j-impl
[INFO] excluded: org.apache.logging.log4j:log4j-1.2-api
[INFO] excluded: org.apache.logging.log4j:log4j-jcl
[INFO] excluded: org.slf4j:jcl-over-slf4j
[INFO] excluded: org.apache.logging.log4j:log4j-jul
[INFO] excluded: org.mule.mvel:mule-mvel2
[INFO] excluded: org.mule.common:mule-common
[INFO] excluded: org.mule.modules:mule-module-client
[INFO] excluded: org.mule.modules:mule-module-spring-config
[INFO] excluded: org.mule.modules:mule-module-annotations
[INFO] excluded: org.mule.modules:mule-module-xml
[INFO] excluded: org.mule.modules:mule-module-cxf
[INFO] excluded: org.mule.modules:mule-module-spring-security
[INFO] excluded: org.mule.transports:mule-transport-http
[INFO] excluded: org.mule.transports:mule-transport-ssl
[INFO] excluded: org.mule.transports:mule-transport-tcp
[INFO] excluded: org.mule.modules:mule-module-http
[INFO] excluded: org.mule.modules:mule-module-spring-extras
[INFO] excluded: org.mule.modules:mule-module-builders
[INFO] excluded: org.mule.modules:mule-module-management
[INFO] excluded: org.mule.modules:mule-module-scripting
[INFO] excluded: org.mule.modules:mule-module-sxc
[INFO] excluded: org.mule.tests:mule-tests-functional
[INFO] excluded: org.mule:mule-core
[INFO] excluded: org.mule.transports:mule-transport-file
[INFO] excluded: org.mule.transports:mule-transport-jdbc
[INFO] excluded: org.mule.transports:mule-transport-jms
[INFO] excluded: org.mule.transports:mule-transport-vm
Attached is Pom.xml and assembly.xml used for creating runnable jar.
POM.XML
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>in.gov.uidai</groupId>
<artifactId>uidprnlettergenerator</artifactId>
<packaging>mule</packaging>
<version>1.0.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<mule.version>3.6.0</mule.version>
<eclipsePluginVersion>2.8</eclipsePluginVersion>
<jdkName>JavaSE-1.6</jdkName>
<jdk.version>1.6</jdk.version>
<junit.version>4.9</junit.version>
<uidpersistentmodel.version>3.5.14</uidpersistentmodel.version>
</properties>
<build>
<!-- Use a newer version of the install plugin than what your Maven uses
by default. The older version failed to install the project if there was
no target/classes folder. Since we use mule packaging on the project we actually
create and attach the zip that must be installed. -->
<pluginManagement>
<plugins>
<!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-install-plugin</artifactId>
<version>2.3.1</version> </plugin> This plugin's configuration is used to
store Eclipse m2e settings only. It has no influence on the Maven build itself.
<plugin> <groupId>org.eclipse.m2e</groupId> <artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version> <configuration> <lifecycleMappingMetadata> <pluginExecutions>
<pluginExecution> <pluginExecutionFilter> <groupId>org.mule.tools</groupId>
<artifactId>maven-mule-plugin</artifactId> <versionRange>[1.6,)</versionRange>
<goals> <goal>attach-test-resources</goal> <goal>filter-resources </goal>
</goals> </pluginExecutionFilter> <action> <ignore /> </action> </pluginExecution>
</pluginExecutions> </lifecycleMappingMetadata> </configuration> </plugin> -->
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.mule.tools</groupId>
<artifactId>maven-mule-plugin</artifactId>
<version>1.9</version>
<extensions>true</extensions>
<configuration>
<copyToAppsDirectory>true</copyToAppsDirectory>
<inclusions>
<inclusion>
<groupId>org.mule.transports</groupId>
<artifactId>mule-transport-amqp</artifactId>
</inclusion>
<inclusion>
<groupId>org.mule.modules</groupId>
<artifactId>mule-module-objectstore</artifactId>
</inclusion>
<inclusion>
<groupId>org.mule</groupId>
<artifactId>mule-core</artifactId>
</inclusion>
</inclusions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>ISO-8859-1</encoding>
</configuration>
</plugin>
<!-- <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2.1</version>
<configuration> <descriptorRefs> <descriptorRef>project</descriptorRef> </descriptorRefs>
</configuration> </plugin> -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>${eclipsePluginVersion}</version>
<configuration>
<!-- by default download all sources when generating project files -->
<downloadSources>true</downloadSources>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-resource</id>
<phase>generate-resources</phase>
<goals>
<goal>add-resource</goal>
</goals>
</execution>
</executions>
<configuration>
<resources>
<resource>
<directory>src/main/app/</directory>
</resource>
<resource>
<directory>src/main/api/</directory>
</resource>
</resources>
</configuration>
</plugin>
<!-- <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.mule.MuleServer</mainClass>
<arguments>
<argument>-config</argument>
<argument>src/main/app/mule-config.xml</argument>
</arguments>
<classpathScope>compile</classpathScope>
</configuration>
</plugin>-->
<!--Install clover-plugins -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<!-- <executions> <execution> <id>copy-clover-plugins</id> <phase>validate</phase>
<goals> <goal>copy</goal> </goals> <configuration> <overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer>
<stripVersion>true</stripVersion> <outputDirectory>${project.build.testOutputDirectory}</outputDirectory>
<artifactItems> <artifactItem> <groupId>com.cloveretl</groupId> <artifactId>cloveretl-engine</artifactId>
<version>${mule.version}</version> <type>zip</type> </artifactItem> </artifactItems>
</configuration> </execution> </executions> -->
</plugin>
<!-- <plugin> <artifactId>maven-antrun-plugin</artifactId> <version>1.7</version>
<executions> <execution> <phase>compile</phase> <configuration> <tasks> Using
an ant task to deflate cloveretl-engine.zip <unzip dest="${project.build.testOutputDirectory}"
src="${project.build.testOutputDirectory}/cloveretl-engine.zip" /> </tasks>
</configuration> <goals> <goal>run</goal> </goals> </execution> </executions>
</plugin> -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory default-value="${project.basedir}/src/main/resources/schema" />
<bindingDirectory>${project.basedir}/src/main/resources/schema</bindingDirectory>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.4</version>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>in.gov.uidai.loader.PrintLetterGeneratorLoader</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!-- plugins for creating site reports -->
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.4.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.5</version>
<configuration>
<configLocation>http://mulesoft.org/download/attachments/92/checkstyle.xml?version=1</configLocation>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>2.5</version>
<configuration>
<targetJdk>${jdk.version}</targetJdk>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>taglist-maven-plugin</artifactId>
<version>2.4</version>
<configuration>
<tags>
<tag>TODO</tag>
<tag>#todo</tag>
<tag>FIXME</tag>
<tag>#fixme</tag>
<tag>#deprecated</tag>
</tags>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.7</version>
<configuration>
<source>${jdk.version}</source>
<links>
<link>http://java.sun.com/j2ee/1.4/docs/api</link>
<link>http://java.sun.com/j2se/1.4.2/docs/api</link>
<link>http://java.sun.com/j2se/1.5.0/docs/api</link>
</links>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jdepend-maven-plugin</artifactId>
<version>2.0-beta-2</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.3.2</version>
</plugin>
</plugins>
</reporting>
<!-- Third Party dependency <dependencies> <dependency> <groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId> <version>1.19</version> </dependency>
</dependencies> -->
<dependencies>
<!-- Third Party dependency -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.mule.modules</groupId>
<artifactId>mule-module-objectstore</artifactId>
<version>1.3.1</version>
</dependency>
<!-- Mule Dependencies -->
<dependency>
<groupId>org.mule</groupId>
<artifactId>mule-core</artifactId>
<version>${mule.version}</version>
<scope>provided</scope>
</dependency>
<!-- Xml configuration <dependency> <groupId>com.mulesoft.muleesb.modules</groupId>
<artifactId>mule-module-boot-ee</artifactId> <version>${mule.version}</version>
<scope>provided</scope> </dependency> -->
<!-- Xml configuration -->
<dependency>
<groupId>org.mule.modules</groupId>
<artifactId>mule-module-spring-config</artifactId>
<version>${mule.version}</version>
<scope>provided</scope>
</dependency>
<!-- Xml configuration <dependency> <groupId>com.mulesoft.muleesb</groupId>
<artifactId>mule-core-ee</artifactId> <version>${mule.version}</version>
<scope>provided</scope> </dependency> <dependency> <groupId>com.mulesoft.muleesb.modules</groupId>
<artifactId>mule-module-data-mapper</artifactId> <version>${mule.version}</version>
</dependency> Xml configuration <dependency> <groupId>com.mulesoft.muleesb.modules</groupId>
<artifactId>mule-module-spring-config-ee</artifactId> <version>${mule.version}</version>
<scope>provided</scope> </dependency> -->
<!-- Mule Transports -->
<dependency>
<groupId>org.mule.transports</groupId>
<artifactId>mule-transport-file</artifactId>
<version>${mule.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mule.transports</groupId>
<artifactId>mule-transport-jdbc</artifactId>
<version>${mule.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mule.transports</groupId>
<artifactId>mule-transport-jms</artifactId>
<version>${mule.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mule.transports</groupId>
<artifactId>mule-transport-vm</artifactId>
<version>${mule.version}</version>
<scope>provided</scope>
</dependency>
<!-- Mule Modules -->
<dependency>
<groupId>org.mule.modules</groupId>
<artifactId>mule-module-client</artifactId>
<version>${mule.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mule.modules</groupId>
<artifactId>mule-module-cxf</artifactId>
<version>${mule.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mule.modules</groupId>
<artifactId>mule-module-management</artifactId>
<version>${mule.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mule.modules</groupId>
<artifactId>mule-module-scripting</artifactId>
<version>${mule.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mule.modules</groupId>
<artifactId>mule-module-sxc</artifactId>
<version>${mule.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mule.modules</groupId>
<artifactId>mule-module-xml</artifactId>
<version>${mule.version}</version>
<scope>provided</scope>
</dependency>
<!-- for testing -->
<dependency>
<groupId>org.mule.tests</groupId>
<artifactId>mule-tests-functional</artifactId>
<version>${mule.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mockobjects</groupId>
<artifactId>mockobjects-core</artifactId>
<version>0.09</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- <dependency> <groupId>com.cloveretl</groupId> <artifactId>cloveretl-engine</artifactId>
<version>3.5.2</version> <scope>test</scope> </dependency> -->
<dependency>
<groupId>org.mule.transports</groupId>
<artifactId>mule-transport-amqp</artifactId>
<version>3.4.4</version>
</dependency>
<!-- Other UIDAI project dependencies -->
<dependency>
<groupId>in.gov.uidai</groupId>
<artifactId>uidpersistentmodel</artifactId>
<version>${uidpersistentmodel.version}</version>
</dependency>
<dependency>
<groupId>in.gov.uidai</groupId>
<artifactId>uidplatform</artifactId>
<version>3.5.17</version>
</dependency>
<dependency>
<groupId>in.gov.uidai</groupId>
<artifactId>uidplatformmodel</artifactId>
<version>3.5.17</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>mulesoft-releases</id>
<name>MuleSoft Releases Repository</name>
<url>http://repository.mulesoft.org/releases/</url>
<layout>default</layout>
</repository>
<repository>
<id>mulesoft-snapshots</id>
<name>MuleSoft Snapshots Repository</name>
<url>http://repository.mulesoft.org/snapshots/</url>
<layout>default</layout>
</repository>
<repository>
<id>muleforge-releases</id>
<name>MuleForge Snapshot Repository</name>
<url>http://repository-master.mulesoft.org/releases/</url>
</repository>
<repository>
<id>Central</id>
<name>Central</name>
<url>http://repo1.maven.org/maven2/</url>
<layout>default</layout>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>mulesoft-release</id>
<name>mulesoft release repository</name>
<layout>default</layout>
<url>http://repository.mulesoft.org/releases/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
assembly.xml
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>jar-with-dependencies</id>
<formats>
<format>mule</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<outputDirectory>/</outputDirectory>
<directory>src/main/resources</directory>
<includes>
<include>uidprnlettergenerator.xml</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>false</unpack>
<scope>runtime</scope>
<useTransitiveDependencies>true</useTransitiveDependencies>
<!-- <excludes>
<exclude>org.mule:mule-core</exclude>
<exclude>org.mule.transports:mule-transport-*</exclude>
<exclude>org.mule.modules:mule-module-*</exclude>
<exclude>org.springframework:spring-*</exclude>
</excludes>-->
</dependencySet>
</dependencySets>
</assembly>
Mule applications are packaged as zip not jar archives. They are designed to be deployed on a Mule Standalone server, which provides all the Mule direct and transitive dependencies. This is why none of the Mule jars should go in the application zip. In fact, if you use only core transports and modules, an Mule application is very tiny: it contains only the configuration files and the Java classes, if any.
To produce an executable jar that doesn't need to be deployed on Mule Standalone, use the standard jar-with-dependencies in your maven-assembly-plugin config, ie. replace this:
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
with that:
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
You also need to remove the provided scope from the Mule dependencies in your pom.xml.

modelling XML dependencies between projects using maven

I'm building an application that is composed of two projects: common and theApp.
Now, theApp depends upon common (modelled using a dependency). The tricky thing i'm facing is:
There are XML files in common/src/main/env which will be needed while theApp is running.
To make things more fun, a distribution of run will have this structure:
bin/ - has scripts
lib/ - has the jars created by theApp and common
dependencies/ - has the library jars on which the app depends
config/ - has the xml files from theApp and common
I'm all done, except that I'm still looking for a way to write a maven goal in theApp's POM that can put common's XML files in config/.
Any ideas?
Thanks,
Harshath
You'll need the maven assembly and dependency plugins. Create an assembly descriptor in common/src/main/resources/assemblies/config.xml like this:
<assembly>
<id>config</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${basedir}/src/main/env</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveDependencies>true</useTransitiveDependencies>
<outputDirectory>config/</outputDirectory>
</dependencySet>
</dependencySets>
</assembly>
Then use it in your pom like:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>config</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>my.groupid</groupId>
<artifactId>common</artifactId>
<version>my.version</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
This'll create an attached artifact common-config.zip, which you can expand with the maven-dependency-plugin inside theApp's pom.xml, putting the files in theApp's target/config directory (which may not be suitable, depending on how you're creating your final distribution):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>my.groupid</groupId>
<artifactId>common</artifactId>
<version>my.version</version>
<type>zip</type>
<overWrite>true</overWrite>
<includes>*</includes>
<outputDirectory>${project.build.directory}/config</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
Some of this is documented in http://www.sonatype.com/books/mvnref-book/reference/assemblies.html ...

Maven, assembly plugin, multi-module project and module dependencies

I'm building a multi-module Maven project whose major product is a WAR file, and I'm trying to assemble that file together with associated release-related artifacts (README, release notes, license file, etc.) into a ZIP for distribution. But when I try to build a distribution file, it fails. What am I doing wrong? Note that I have already checked that the foo-bar.1.0.war artifact exists at the point that the foo-distribution module is asked to build the distribution file; I'm aware that putting everything in foo's POM won't work. (I have a workaround; I can use relative paths to point directly to the location of the files distributed relative to the foo-distribution module root. This isn't a solution I like as it has bad code-smell.)
Note also that I always build from the root project, and that I'm using assembly:single in the distribution builder (as recommended in the Maven documentation).
Project Layout
foo
+ src
| + README.txt
+ foo-bar
| + target
| + foo-bar.1.0.war
+ foo-distribution
+ src
+ assemble
+ dist.xml
foo and foo-distribution have pom packaging, and foo-bar has war packaging. (There are other modules in the real code, but they're also constructed correctly before the foo-distribution module.)
foo POM
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>foobar</groupId>
<artifactId>foo</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>foo-bar</module>
<module>foo-distribution</module>
</modules>
</project>
foo-distribution POM
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<version>0.0.1-SNAPSHOT</version>
<parent>
<artifactId>foo</artifactId>
<groupId>foobar</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<groupId>foobar</groupId>
<artifactId>foo-distribution</artifactId>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>foobar</groupId>
<artifactId>foo-bar</artifactId>
<version>1.0</version>
<type>war</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<configuration>
<finalName>foobar</finalName>
<descriptors>
<descriptor>src/assemble/dist.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-t2server-distribution</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
dist.xml
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0">
<id>distribution</id>
<formats>
<format>zip</format>
</formats>
<moduleSets>
<moduleSet>
<includes>
<include>foobar:foo</include>
</includes>
<sources>
<fileSets>
<fileSet>
<directory>src</directory>
<includes>
<include>*.txt</include>
</includes>
</fileSet>
</fileSets>
</sources>
</moduleSet>
<moduleSet>
<includes>
<include>foobar:foo-bar</include>
</includes>
<binaries />
</moduleSet>
</moduleSets>
</assembly>
Workaround dist.xml
This is the workaround that I mentioned above. It produces exactly the artifact that I want.
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0">
<id>distribution</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}/../src</directory>
<includes>
<include>*.txt</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.basedir}/../foo-bar/target</directory>
<includes>
<include>*.war</include>
</includes>
<outputDirectory>.</outputDirectory>
</fileSet>
</fileSets>
</assembly>
You should use a dependencySet in your assembly descriptor
<assembly>
<id>dist</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<dependencySets>
<dependencySet>
<useTransitiveDependencies>false</useTransitiveDependencies>
<useProjectArtifact>false</useProjectArtifact>
<unpack>false</unpack>
<scope>runtime</scope>
<fileMode>0644</fileMode>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
with all supplemental files you need
</fileSet>
</fileSets>
instead of referencing the files via file system (relative paths). And on the other hand a descriptor for source does exists already from the assembly plugin (take a look into the docs).
What order are the modules being build, I had a similar problem and declaring what the parent was in the child module helped with the build order. Not sure if using could help with this.

maven-assembly-plugin doesn't add dependencies with system scope

This is my pom file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sia.g7</groupId>
<artifactId>sia</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>sia</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>commons-math</groupId>
<artifactId>commons-math</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jmathplot</groupId>
<artifactId>jmathplot</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/jmathplot.jar</systemPath>
</dependency>
<dependency>
<groupId>jgraphx</groupId>
<artifactId>jgraphx</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/jgraphx.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.sia.g7.AbstractSimulation</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
And when I run the jar I get:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mxgraph/swing/mxGraphComponent
which is part of the jgraphx dependency.
What am I missing?
you can do it by adding this dependencySet to your assembly file descriptor
<dependencySet>
<outputDirectory>/</outputDirectory>
<unpack>true</unpack>
<scope>system</scope>
</dependencySet>
this assembly descriptor add all dependencies including system scoped
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>jar-with-all-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
<dependencySet>
<outputDirectory>/</outputDirectory>
<unpack>true</unpack>
<scope>system</scope>
</dependencySet>
</dependencySets>
</assembly>
Yes, and this is one of the reasons you shouldn't abuse system scope dependencies (which is globally a bad practice) and this problem has already been mentioned several times here on SO (here, here). I'm proposing a solution to deal with project relative dependencies in a "clean" way in this answer.
You should remove the <scope>system</scope> clauses from those dependencies.
When the scope is set to system that means the artifact is ALWAYS available, so jar-with-dependencies does not include it.
It must not be system scope in the first place. This is the source of your problem.
Install/deploy your dependency into the repository and make it a normal compile (or runtime) scope dependency.
There are 2 solutions:
Recommended: create a repository and then provide the dependency in the same way as you provide dependency from maven repository.
Though the above solution is the recommended one, but if you don't want to create the repository, then here is the workaround:
First in your pom.xml, remove the system scope and the systemPath. Please note that the commented lines were removed to resolve this issue.
<dependency>
<groupId>com.paymentus.timgroup.statsd</groupId>
<artifactId>paymentus-timgroup-statsd-client</artifactId>
<version>1.0-SNAPSHOT</version>
<!--<scope>system</scope>-->
<!--<systemPath>${basedir}/external_lib/xyz.jar</systemPath>-->
</dependency>
Use maven-install-plugin in the same pom.xml. You need to modify the details in bold, no need to change any other thing in this plugin:
<project> ....
<profiles>...<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>install-external</id>
<phase>clean</phase>
<configuration>
**<file>${basedir}/external_lib/xyz.jar</file>**
<repositoryLayout>default</repositoryLayout>
**<groupId>com.my.project</groupId>**
**<artifactId>my-project</artifactId>**
**<version>1.0-SNAPSHOT</version>**
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins></profiles>
</project>
Unpack your jar and add it to src/main/resources.

How can I include unit tests in maven assembly?

Reason: Our project is using Ant as commandline interface. After making a new assembly with maven's assembly plugin, I want to make initial tests to see if all has been properly assembled. Therefore I need to include the unit tests in the final assembly. After assembling, the initial tests would then be called sth like
> ant initTest
build.xml:
<target="initTest">
<junit>
<test class="MyTest" />
</junit>
</target>
Problem is:
I want to keep my Unit tests in src/test/java and not move them to src/main/java.
Is there a way to tell the assembly plugin to include my unit tests? A simple include in the assembly descriptor does not do it ...
There are two steps:
Package the tests into a jar as well as the main code.
Depend on that "-tests" jar in the module that makes the assembly.
To package up the tests, you need to bin the jar:test-jar goal. e.g.
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>test-jar</id>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then in the assembly module, you can depend on the resulting artifact.
<dependencies>
<dependency>
<groupid>${project.groupId}</groupId>
<artifactId>some-artifact</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
</dependency>
</dependencies>
The key bit is the "classifier".
The answer from #Dominic-Mitchell did not work for me at all. What finally worked was adding a fileSet for the test classes to my assembly xml. Note that the fileSet directories are different! That stumped me for the longest time.
Use ${project.build.directory} for the test classes and ${project.build.outputDirectory} for the main:
<?xml version='1.0' encoding='UTF-8'?>
<assembly>
<id>toolbar-test</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}/test-classes</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<scope>runtime</scope>
<unpack>true</unpack>
<unpackOptions>
<excludes>
<exclude>**/LICENSE*</exclude>
<exclude>**/README*</exclude>
</excludes>
</unpackOptions>
</dependencySet>
<dependencySet>
<scope>test</scope>
<unpack>true</unpack>
</dependencySet>
</dependencySets>
</assembly>
Credit where credit is due - I found this solution within this blog post here: http://alexgaddie.blogspot.com/2010/02/creating-uber-jar-with-maven.html
I did not need the profile part of the blog post.
The following works for us.
pom.xml snippet:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
assembly.xml snippet:
<dependencySets>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<useProjectAttachments>true</useProjectAttachments>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
The key is the useProjectAttachments tag.